suchen
HeimSystem-TutorialLINUXSo beschränken Sie Sudo -Benutzer so, dass bestimmte autorisierte Befehle unter Linux ausgeführt werden

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

So beschränken Sie Sudo -Benutzer so, dass bestimmte autorisierte Befehle unter Linux ausgeführt werden

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.

So beschränken Sie Sudo -Benutzer so, dass bestimmte autorisierte Befehle unter Linux ausgeführt werden

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>

So beschränken Sie Sudo -Benutzer so, dass bestimmte autorisierte Befehle unter Linux ausgeführt werden

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

So beschränken Sie Sudo -Benutzer so, dass bestimmte autorisierte Befehle unter Linux ausgeführt werden

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

Das obige ist der detaillierte Inhalt vonSo beschränken Sie Sudo -Benutzer so, dass bestimmte autorisierte Befehle unter Linux ausgeführt werden. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Wie unterscheidet sich die Leistung zwischen Linux und Windows für verschiedene Aufgaben?Wie unterscheidet sich die Leistung zwischen Linux und Windows für verschiedene Aufgaben?May 14, 2025 am 12:03 AM

Linux spielt eine gute Leistung in Servern und Entwicklungsumgebungen, während Windows in Desktop und Gaming besser abschneidet. 1) Das Dateisystem von Linux funktioniert gut, wenn es sich um eine große Anzahl kleiner Dateien handelt. 2) Linux führt hervorragend in hoher Parallelitäts- und Hochdurchsatz -Netzwerkszenarien durch. 3) Linux Memory Management hat mehr Vorteile in Serverumgebungen. 4) Linux ist bei der Ausführung von Befehlszeile und Skriptaufgaben effizient, während Windows an grafischen Schnittstellen und Multimedia -Anwendungen besser ausgeführt wird.

So erstellen Sie GUI -Anwendungen unter Linux mit PygobjectSo erstellen Sie GUI -Anwendungen unter Linux mit PygobjectMay 13, 2025 am 11:09 AM

Das Erstellen von GUI-Anwendungen (Grafical User Interface) ist eine fantastische Möglichkeit, Ihre Ideen zum Leben zu erwecken und Ihre Programme benutzerfreundlicher zu gestalten. PygObject ist eine Python -Bibliothek, mit der Entwickler GUI -Anwendungen auf Linux -Desktops erstellen können

So installieren Sie Lampenstapel mit PhpMyAdmin in Arch LinuxSo installieren Sie Lampenstapel mit PhpMyAdmin in Arch LinuxMay 13, 2025 am 11:01 AM

Arch Linux bietet eine flexible Spitzensystemumgebung und ist eine mächtig geeignete Lösung für die Entwicklung von Webanwendungen in kleinen nicht kritischen Systemen, da es sich um eine vollständig Open Source handelt und die neuesten aktuellen Versionen für Kernel bietet

So installieren Sie LEMP (Nginx, PHP, Mariadb) unter Arch LinuxSo installieren Sie LEMP (Nginx, PHP, Mariadb) unter Arch LinuxMay 13, 2025 am 10:43 AM

Aufgrund seines Rolling-Release-Modells, das umfassende Software-Software Arch Linux umfasst, wurde nicht als Server ausgelegt und entwickelt, um zuverlässige Netzwerkdienste anzubieten

12 Must-Have Linux Console [Terminal] Dateimanager12 Must-Have Linux Console [Terminal] DateimanagerMay 13, 2025 am 10:14 AM

Linux-Konsolen-Dateimanager können bei täglichen Aufgaben sehr hilfreich sein, bei der Verwaltung von Dateien auf einem lokalen Computer oder bei verbundenen Remote-Dateien. Die visuelle Konsolenrepräsentation des Verzeichnisse

QBITTORRENT: Ein leistungsstarker Open-Source-Bittorrent-ClientQBITTORRENT: Ein leistungsstarker Open-Source-Bittorrent-ClientMay 13, 2025 am 10:12 AM

QBITTORRENT ist ein beliebter Open-Source-BitTorrent-Client, mit dem Benutzer Dateien über das Internet herunterladen und freigeben können. Die neueste Version, QBITTorrent 5.0, wurde kürzlich veröffentlicht und ist voller neuer Funktionen und Verbesserungen. Dieser Artikel wird

Setup Nginx Virtual Hosts, Phpmyadmin und SSL auf Arch LinuxSetup Nginx Virtual Hosts, Phpmyadmin und SSL auf Arch LinuxMay 13, 2025 am 10:03 AM

Der vorherige Artikel von Arch Linux LEMP umfasste gerade grundlegende Dinge, aus der Installation von Netzwerkdiensten (NGINX, PHP, MySQL und PHPMYADMIN) und die Konfiguration der für MySQL Server und PHPMYADMin erforderlichen minimalen Sicherheit. Dieses Thema hängt streng mit der Form in Bezug auf die Form zu

Zenity: Erstellen von GTK -Dialogen in Shell -SkriptenZenity: Erstellen von GTK -Dialogen in Shell -SkriptenMay 13, 2025 am 09:38 AM

Zenity ist ein Tool, mit dem Sie grafische Dialogfelder unter Linux über die Befehlszeile erstellen können. Es verwendet GTK, ein Toolkit zum Erstellen grafischer Benutzeroberflächen (GUIs), mit denen Sie Ihren Skripten visuelle Elemente hinzufügen können. Zenity kann extrem u sein

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Nordhold: Fusionssystem, erklärt
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Flüstern des Hexenbaum
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SecLists

SecLists

SecLists ist der ultimative Begleiter für Sicherheitstester. Dabei handelt es sich um eine Sammlung verschiedener Arten von Listen, die häufig bei Sicherheitsbewertungen verwendet werden, an einem Ort. SecLists trägt dazu bei, Sicherheitstests effizienter und produktiver zu gestalten, indem es bequem alle Listen bereitstellt, die ein Sicherheitstester benötigen könnte. Zu den Listentypen gehören Benutzernamen, Passwörter, URLs, Fuzzing-Payloads, Muster für vertrauliche Daten, Web-Shells und mehr. Der Tester kann dieses Repository einfach auf einen neuen Testcomputer übertragen und hat dann Zugriff auf alle Arten von Listen, die er benötigt.

MantisBT

MantisBT

Mantis ist ein einfach zu implementierendes webbasiertes Tool zur Fehlerverfolgung, das die Fehlerverfolgung von Produkten unterstützen soll. Es erfordert PHP, MySQL und einen Webserver. Schauen Sie sich unsere Demo- und Hosting-Services an.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen