検索
ホームページシステムチュートリアルLinuxLinuxで特定の承認されたコマンドを実行するようにSudoユーザーを制限する方法

The sudo command allows users to run commands with root privileges. This can be a powerful tool, but it can also be a security risk if not used carefully. One way to mitigate this risk is to allow sudo users to run particular authorized commands. In this guide, we will show you how to restrict sudo users to run specific commands with sudo privileges in Linux. We will also show you how to revert sudoers file back to the original configuration.

Table of Contents

Restrict Sudo Users to Run Authorized Commands

To restrict sudo users to ONLY run authorized commands, you can use the sudoers configuration file. On most Linux distributions, the sudoers file is located at /etc/sudoers file or /etc/sudoers.d/ directory.

Heads Up: Before making changes to the sudoers file, it's crucial to use caution, as incorrect configurations can lead to system issues. Always use the visudo command to edit the sudoers file, as it performs syntax checks before saving changes.

Here's how you can restrict sudo users to run specific commands:

1. It's highly recommended to backup the sudoers file before making any changes or edits to it. To backup sudoers file, run:

$ sudo cp /etc/sudoers /etc/sudoers.bak

By backing up the sudoers file, you can easily revert to a known-working configuration if errors occur during editing or in case of security incidents.

2. Open the sudoers file for editing using visudo command:

$ sudo visudo

3. Scroll down to the line where it says:

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

The above line means that members of the "sudo" group are allowed to execute any command with sudo privileges on any host and as any user or group. Essentially, it grants full sudo access to the users in the "sudo" group.

4. To allow the sudo users to execute only a specific command, for example apt, modify the line as shown below.

%sudo   ALL=(ALL:ALL) /bin/apt

Linuxで特定の承認されたコマンドを実行するようにSudoユーザーを制限する方法

You can also specify multiple allowed commands for a user by separating them with commas:

%sudo   ALL=(ALL:ALL) /path/to/allowed/command1,/path/to/allowed/command2

5. If you want to allow the user to run the allowed commands without entering a password, you can append NOPASSWD: before the command path. However, be cautious when using this option, as it might reduce the security of your system.

%sudo  ALL=(ALL)  NOPASSWD: /path/to/allowed/command

6. Once you've made the necessary changes, save and close the sudoers file.

7. Verify the syntax of your sudoers file before exiting visudo. If there are any syntax errors, visudo will prompt you to correct them.

After following these steps, all the members of the sudo group will only be able to execute the allowed commands with sudo privileges. Running all other commands with sudo privilege will be denied, even if the user is a member of sudo group.

Let us verify it by running the cat command with sudo privilege.

$ sudo cat /etc/sudoers

Sample Output:

[sudo] password for ostechnix: 
Sorry, user ostechnix is not allowed to execute '/usr/bin/cat /etc/sudoers' as root on debian12.ostechnix.lan.

Linuxで特定の承認されたコマンドを実行するようにSudoユーザーを制限する方法

Even though, the user 'ostechnix' is a member of sudo group, he can't run sudo cat /etc/sudoers command. Because, we restricted him to run only the apt command with sudo privilege.

Let us list all of the commands that the user ostechnix is allowed to run with sudo privileges.

$ sudo -lU ostechnix
[sudo] password for ostechnix: 
Matching Defaults entries for ostechnix on debian12:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty

User ostechnix may run the following commands on debian12:
    <strong><mark>(ALL : ALL) /bin/apt</mark></strong>

Linuxで特定の承認されたコマンドを実行するようにSudoユーザーを制限する方法

As you can see in the above output, the user ostechnix can run only apt command with sudo privilege.

Let us check if he can able to the apt command with sudo privilege.

$ sudo apt update

Linuxで特定の承認されたコマンドを実行するようにSudoユーザーを制限する方法

Yes, he has no problem on running the allowed command, which is apt in this case, with sudo rights. The user can also run all the sub-commands of apt, for example apt upgrade, apt full-upgrade etc.

Please note that this is applicable only for the commands run with sudo privilege. Executing any other commands without sudo will normally work.

Restoring Original sudoers File Configuration

If you want to revert the sudoers file back to the original configuration, you need to change it to the correct syntax that was originally present in the file. To do that, follow these steps:

1. Login as root user or switch to another sudo user who has full sudo privilege.

2. If you already have the backup, restore the sudoers file from the backup using the following command (assuming the backup file is in /etc directory).

$ sudo cp /etc/sudoers.bak /etc/sudoers

If you don't have backup, follow the subsequent steps.

3. Open the sudoers file for editing using visudo command. Make sure you're logged in as root or other sudo user.

$ sudo visudo

4. Locate the line that you want to modify. In our case, it's the line that grants sudo privileges to the sudo group and allows them to run /bin/apt.

5. Replace the current line with the original configuration that you want to restore. For example, if the current line is:

%sudo   ALL=(ALL:ALL) /bin/apt

and you want to revert it back to the default configuration that grants full sudo privileges to the sudo group, it should be:

%sudo   ALL=(ALL:ALL) ALL

6. Save and close the sudoers file.

7. Verify the syntax of your sudoers file before exiting visudo. If there are no syntax errors, the changes will be applied.

After making these changes, the sudo configuration will be modified back to the original settings, and the users will have the sudo privileges as they had before the changes were made.

Remember to be careful when modifying the sudoers file, as incorrect configurations can lead to issues with sudo access on your system. Always use visudo to edit the file to avoid syntax errors.

Conclusion

Restricting sudo users to run specific commands is a good way to improve the security of your Linux system. By limiting the commands that sudo users can run, you can reduce the risk of unauthorized access and system damage.

Related Read:

  • How To Run Particular Commands Without Sudo Password In Linux
  • How To Allow Or Deny Sudo Access To A Group In Linux
  • How To Restrict Su Command To Authorized Users In Linux
  • Run Commands As Another User Via Sudo In Linux
  • How To Prevent Command Arguments With Sudo In Linux
  • How To Run All Programs In A Directory Via Sudo In Linux
  • How To Restore Sudo Privileges To A User

以上がLinuxで特定の承認されたコマンドを実行するようにSudoユーザーを制限する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
使用方法&#039;次の&#039; Linuxでawkを使用したコマンド-Part 6使用方法&#039;次の&#039; Linuxでawkを使用したコマンド-Part 6May 15, 2025 am 10:43 AM

awkシリーズのこの6回目の記事では、次のコマンドを探索します。これは、冗長処理ステップをスキップすることでスクリプト実行の効率を向上させるのに役立ちます。次のコマンドは何ですか?

Linuxでファイルを効率的に転送する方法Linuxでファイルを効率的に転送する方法May 15, 2025 am 10:42 AM

Linuxシステムでファイルを転送することは、特にローカルシステム間またはリモートシステム間のネットワーク伝送に関しては、すべてのシステム管理者が習得する一般的なタスクです。 Linuxは、このタスクを達成するために2つの一般的に使用されるツールを提供します:SCP(セキュアレプリケーション)とRSYNC。どちらも、ローカルマシンまたはリモートマシン間でファイルを転送するための安全で便利な方法を提供します。この記事では、SCPおよびRSYNCコマンドを使用して、ローカルおよびリモートファイルの転送を含むファイルを転送する方法を詳細に説明します。 Linux SCPコマンドのSCP(Secure Copy Protocol)を理解することは、SSH(Secure Shell)を介して2つのホスト間でファイルとディレクトリを安全にコピーするために使用されるコマンドラインプログラムです。つまり、ファイルがインターネットを介して転送されると、

史上最も人気のあるLinuxデスクトップ環境史上最も人気のあるLinuxデスクトップ環境May 15, 2025 am 10:35 AM

Linuxの魅力的な機能の1つは、WindowsやMac OS Xとは対照的に、さまざまなデスクトップ環境に対するサポートです。これにより、デスクトップユーザーは、コンピューティング要件に基づいて、最も適切で適合するデスクトップ環境を選択できます。

LinuxデスクトップにLibreOffice 24.8をインストールする方法LinuxデスクトップにLibreOffice 24.8をインストールする方法May 15, 2025 am 10:15 AM

Libreofficeは、Linux、Windows、Macのプラットフォームに合わせて、堅牢でオープンソースのオフィススイートとして際立っています。ワードドキュメント、スプレッドシート、プレゼンテーション、図面、計算、および数学を処理するための一連の高度な機能を誇っています

LinuxのOnlyOffice Docsを使用してPDFファイルの操作方法LinuxのOnlyOffice Docsを使用してPDFファイルの操作方法May 15, 2025 am 09:58 AM

PDFファイルを管理するLinuxユーザーには、幅広いプログラムが自由に使用されています。具体的には、さまざまな機能用に設計された多数の専門的なPDFツールがあります。たとえば、ファイルを読むためのPDFビューアーまたはPDF EDIをインストールすることを選択できます。

awkとstdinを使用してコマンド出力をフィルタリングする方法awkとstdinを使用してコマンド出力をフィルタリングする方法May 15, 2025 am 09:53 AM

AWWコマンドシリーズの以前のセグメントでは、主にファイルからの入力を読み取ることに焦点を当てていました。ただし、Stdinから入力を読む必要がある場合はどうなりますか?awkシリーズのパート7では、oの出力を使用できるいくつかの例を調べます

CLIFM- LinuxのLightning -FastターミナルファイルマネージャーCLIFM- LinuxのLightning -FastターミナルファイルマネージャーMay 15, 2025 am 09:45 AM

CLIFMは、シェルのようなインターフェイスの基礎に基づいて設計された、独特で信じられないほど迅速なコマンドラインファイルマネージャーとして際立っています。これは、ユーザーがすでによく知っているコマンドを使用してファイルシステムに関与できることを意味します。選択o

Linux Mint 21.3からLinux Mint 22へのアップグレード方法Linux Mint 21.3からLinux Mint 22へのアップグレード方法May 15, 2025 am 09:44 AM

Linux Mint 22 Wilmaの新しいインストールを実行したくない場合は、以前のバージョンからアップグレードするオプションがあります。このガイドでは、Linux Mint 21.3(21.Xシリーズの最新のマイナーリリースからアップグレードするプロセスを詳しく説明します。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター