Jadual Kandungan
Restrict Sudo Users to Run Authorized Commands
Restoring Original sudoers File Configuration
Conclusion
Rumah Tutorial sistem LINUX Cara Menghadkan Pengguna Sudo Untuk Menjalankan Perintah Dibenarkan Di Linux

Cara Menghadkan Pengguna Sudo Untuk Menjalankan Perintah Dibenarkan Di Linux

Mar 23, 2025 am 09:29 AM

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
Salin selepas log masuk

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
Salin selepas log masuk
Salin selepas log masuk

3. Scroll down to the line where it says:

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

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
Salin selepas log masuk
Salin selepas log masuk

Cara Menghadkan Pengguna Sudo Untuk Menjalankan Perintah Dibenarkan Di Linux

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
Salin selepas log masuk

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
Salin selepas log masuk

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
Salin selepas log masuk

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.
Salin selepas log masuk

Cara Menghadkan Pengguna Sudo Untuk Menjalankan Perintah Dibenarkan Di Linux

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>
Salin selepas log masuk

Cara Menghadkan Pengguna Sudo Untuk Menjalankan Perintah Dibenarkan Di Linux

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
Salin selepas log masuk

Cara Menghadkan Pengguna Sudo Untuk Menjalankan Perintah Dibenarkan Di Linux

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
Salin selepas log masuk

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
Salin selepas log masuk
Salin selepas log masuk

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
Salin selepas log masuk
Salin selepas log masuk

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
Salin selepas log masuk

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

Atas ialah kandungan terperinci Cara Menghadkan Pengguna Sudo Untuk Menjalankan Perintah Dibenarkan Di Linux. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

Tutorial Java
1655
14
Tutorial PHP
1252
29
Tutorial C#
1225
24
Apakah 5 komponen asas Linux? Apakah 5 komponen asas Linux? Apr 06, 2025 am 12:05 AM

Lima komponen asas Linux adalah: 1. Kernel, menguruskan sumber perkakasan; 2. Perpustakaan sistem, menyediakan fungsi dan perkhidmatan; 3. Shell, antara muka pengguna untuk berinteraksi dengan sistem; 4. Sistem fail, menyimpan dan menganjurkan data; 5. Aplikasi, menggunakan sumber sistem untuk melaksanakan fungsi.

Apakah yang paling banyak digunakan Linux? Apakah yang paling banyak digunakan Linux? Apr 09, 2025 am 12:02 AM

Linux digunakan secara meluas dalam pelayan, sistem tertanam dan persekitaran desktop. 1) Dalam bidang pelayan, Linux telah menjadi pilihan yang ideal untuk menganjurkan laman web, pangkalan data dan aplikasi kerana kestabilan dan keselamatannya. 2) Dalam sistem tertanam, Linux popular untuk penyesuaian dan kecekapan yang tinggi. 3) Di persekitaran desktop, Linux menyediakan pelbagai persekitaran desktop untuk memenuhi keperluan pengguna yang berbeza.

Bagaimana untuk belajar asas Linux? Bagaimana untuk belajar asas Linux? Apr 10, 2025 am 09:32 AM

Kaedah untuk pembelajaran asas Linux dari awal termasuk: 1. Memahami sistem fail dan antara muka baris perintah, 2. Master arahan asas seperti LS, CD, MKDIR, 3.

Apakah peranti Linux? Apakah peranti Linux? Apr 05, 2025 am 12:04 AM

Peranti Linux adalah peranti perkakasan yang menjalankan sistem operasi Linux, termasuk pelayan, komputer peribadi, telefon pintar dan sistem tertanam. Mereka mengambil kesempatan daripada kuasa Linux untuk melaksanakan pelbagai tugas seperti hosting laman web dan analisis data besar.

Adakah Internet berjalan di Linux? Adakah Internet berjalan di Linux? Apr 14, 2025 am 12:03 AM

Internet tidak bergantung pada sistem operasi tunggal, tetapi Linux memainkan peranan penting di dalamnya. Linux digunakan secara meluas dalam pelayan dan peranti rangkaian dan popular untuk kestabilan, keselamatan dan skalabiliti.

Apakah kelemahan Linux? Apakah kelemahan Linux? Apr 08, 2025 am 12:01 AM

Kelemahan Linux termasuk pengalaman pengguna, keserasian perisian, sokongan perkakasan, dan keluk pembelajaran. 1. Pengalaman pengguna tidak mesra seperti Windows atau MacOS, dan ia bergantung pada antara muka baris arahan. 2. Keserasian perisian tidak sebaik sistem lain dan tidak mempunyai versi asli banyak perisian komersial. 3. Sokongan perkakasan tidak begitu komprehensif seperti Windows, dan pemandu boleh dikumpulkan secara manual. 4. Keluk pembelajaran adalah curam, dan menguasai operasi baris arahan memerlukan masa dan kesabaran.

Apakah operasi Linux? Apakah operasi Linux? Apr 13, 2025 am 12:20 AM

Inti sistem pengendalian Linux adalah antara muka baris arahannya, yang boleh melakukan pelbagai operasi melalui baris arahan. 1. Operasi Fail dan Direktori Gunakan LS, CD, MKDIR, RM dan arahan lain untuk menguruskan fail dan direktori. 2. Pengguna dan Pengurusan Kebenaran Memastikan keselamatan sistem dan peruntukan sumber melalui UserAdd, Passwd, CHMOD dan arahan lain. 3. Pengurusan proses menggunakan PS, membunuh dan arahan lain untuk memantau dan mengawal proses sistem. 4. Operasi rangkaian termasuk PING, IFCONFIG, SSH dan arahan lain untuk mengkonfigurasi dan menguruskan sambungan rangkaian. 5. Pemantauan sistem dan penyelenggaraan sistem seperti TOP, DF, DU untuk memahami status operasi sistem dan penggunaan sumber.

Apakah gaji Pentadbir Linux? Apakah gaji Pentadbir Linux? Apr 17, 2025 am 12:24 AM

Purata gaji tahunan pentadbir Linux ialah $ 75,000 hingga $ 95,000 di Amerika Syarikat dan € 40,000 hingga € 60,000 di Eropah. Untuk meningkatkan gaji, anda boleh: 1. Secara berterusan mempelajari teknologi baru, seperti pengkomputeran awan dan teknologi kontena; 2. Mengumpulkan pengalaman projek dan menubuhkan portfolio; 3. Mewujudkan rangkaian profesional dan mengembangkan rangkaian anda.

See all articles