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
Mastering Textmanipulation mit dem SED -BefehlMastering Textmanipulation mit dem SED -BefehlMar 16, 2025 am 09:48 AM

Die Linux -Befehlszeilenschnittstelle bietet eine Fülle von Textverarbeitungswerkzeugen, eines der leistungsstärksten Tools ist der SED -Befehl. SED ist die Abkürzung von Stream Editor, einem multifunktionalen Tool, das eine komplexe Verarbeitung von Textdateien und Streams ermöglicht. Was ist SED? SED ist ein nicht interaktiver Texteditor, der auf Pipeline-Eingaben oder Textdateien arbeitet. Durch die Bereitstellung von Anweisungen können Sie Text in einer Datei oder einem Stream ändern und verarbeiten. Zu den häufigsten Anwendungsfällen von SED gehört das Auswahl von Text, das Ersetzen von Text, das Ändern von Originaldateien, das Hinzufügen von Zeilen zum Text oder das Entfernen von Zeilen aus Text. Es kann aus der Befehlszeile in Bash und anderen Befehlszeilenschalen verwendet werden. SED -Befehlssyntax sed

PILET: Ein modularer, tragbarer Mini-Computer von Raspberry Pi angetriebenPILET: Ein modularer, tragbarer Mini-Computer von Raspberry Pi angetriebenMar 06, 2025 am 10:11 AM

Discover Pilet: Ein retro-futuristischer Open-Source-Mini-Computer Suchen Sie nach einem Mini-Computer, der klassischen Stil mit modernster Technologie kombiniert? Treffen Sie Pilet, ein modulares Open-Source-Marvel-Mund, das von der Raspberry Pi 5. mit einer 7-Stunden-Akkulaufzeit verfügt

Linux -Kernel -Quellcode übertrifft 40 Millionen ZeilenLinux -Kernel -Quellcode übertrifft 40 Millionen ZeilenMar 05, 2025 am 09:35 AM

Linux: Der Eckpfeiler des modernen Computers, von Smartphones bis zu Supercomputern, kann alles tun. Im Laufe der Jahre hat sich die Größe und Komplexität des Linux -Kernels erheblich zugenommen. Ab Januar 2025 enthält der Linux -Kernel -Quellcode ungefähr 40 Millionen Codezeilen! Dies ist eine der größten Errungenschaften in der Geschichte von Open-Source-Projekten. In diesem Artikel wird das exponentielle Wachstum der Anzahl der Zeilen im Linux -Kernel -Quellcode, die Gründe und die Überprüfung der aktuellen Anzahl der Zeilen selbst erläutert. Verzeichnis -Linux -Kernel -Geschichte Zähl die Anzahl der Zeilen des Linux -Kernel -Quellcode Code Nur Zähldateien C- und Header -Dateien Exponentieller Trend des Kernel -Wachstums überprüfen Sie die historischen Linux -Kernel -Linien Zusammenfassung Linux Kernel -Geschichte seit 1991 Linus Tor

So zählen Sie Dateien und Verzeichnisse unter Linux: Ein AnfängerhandbuchSo zählen Sie Dateien und Verzeichnisse unter Linux: Ein AnfängerhandbuchMar 19, 2025 am 10:48 AM

Effizientes Zählen von Dateien und Ordnern unter Linux: Eine umfassende Anleitung Zu wissen, wie Sie Dateien und Verzeichnisse in Linux schnell zählen, ist für Systemadministratoren und alle, die große Datensätze verwalten, von entscheidender Bedeutung. Diese Anleitung zeigt die Verwendung von Simple Command-L

Die geheime Waffe, um Ihr Linux -System mit Liquorix -Kernel zu beendenDie geheime Waffe, um Ihr Linux -System mit Liquorix -Kernel zu beendenMar 08, 2025 pm 12:12 PM

Liquorix -Kernel: Ein leistungsstarkes Tool zur Verbesserung der Leistung des Linux -Systems Linux ist bekannt für seine Flexibilität, Sicherheit und hohe Leistung und wird für Entwickler, Systemadministratoren und fortschrittliche Benutzer zum Betriebssystem der Wahl. Der Universal Linux -Kernel erfüllt jedoch nicht immer die Bedürfnisse der Benutzer, die maximale Leistung und Reaktionsfähigkeit suchen. Hier kommt der Liquorix-Kernel ins Spiel-eine leistungsoptimierte Alternative, die verspricht, Ihr Linux-System zu verbessern. In diesem Artikel wird untersucht, was der Liquorix -Kernel ist, warum Sie ihn vielleicht verwenden möchten und wie Sie ihn installieren und konfigurieren, um das Beste aus Ihrem System herauszuholen. Liquorix Kernel Detaillierte Erklärung Liquorix -Kernel ist ein vorkompilierter Linux -Kernel für

System76 führt Meerkat Mini PC ein: Große Leistung in einem winzigen PaketSystem76 führt Meerkat Mini PC ein: Große Leistung in einem winzigen PaketMar 05, 2025 am 10:28 AM

Das System76 Meerkat: ein mächtiger Mini -PC Suchen Sie nach einem leistungsstarken und dennoch platzsparenden Computer? Treffen Sie den Meerkat Mini PC von System76! Dieses kompakte Kraftpaket eignet sich perfekt für ordentliche Desktops und anspruchsvolle Aufgaben. Inhaltsverzeichnis - kompaktes Design, beeindruckend

So fügen Sie einen Benutzer zu mehreren Gruppen unter Linux hinzuSo fügen Sie einen Benutzer zu mehreren Gruppen unter Linux hinzuMar 18, 2025 am 11:44 AM

Effizientes Verwalten von Benutzerkonten und Gruppenmitgliedschaften ist für die Linux/UNIX -Systemverwaltung von entscheidender Bedeutung. Dadurch wird die richtige Ressourcen- und Datenzugriffskontrolle gewährleistet. In diesem Tutorial wird beschrieben, wie Sie einen Benutzer zu mehreren Gruppen in Linux- und UNIX -Systemen hinzufügen. Wir

Erstellen Sie Ihre eigene Ubuntu Personal Cloud: eine Schritt-für-Schritt-Anleitung zum Erstellen eines sicheren DatenhaussErstellen Sie Ihre eigene Ubuntu Personal Cloud: eine Schritt-für-Schritt-Anleitung zum Erstellen eines sicheren DatenhaussMar 05, 2025 am 11:02 AM

Im heutigen digitalen Zeitalter sind Daten nicht nur Informationen, sondern auch ein Teil unseres Lebens. Von Fotos und Dokumenten bis hin zu sensiblen persönlichen Informationen stellen unsere Daten unsere Erinnerungen, Arbeiten und Interessen dar. Obwohl Cloud -Speicherdienste weit verbreitet sind, werden sie häufig von Datenschutzbedenken, Abonnementgebühren und Anpassungsbeschränkungen begleitet. Das ist es, worauf es sich um eine persönliche Cloud auf Ubuntu handelt, als eine leistungsstarke Alternative, die Ihnen die vollständige Kontrolle über Ihre Daten und die Flexibilität zum Anpassen und Skalieren bei Bedarf bietet. Mit diesem Handbuch werden Sie eine Ubuntu-basierte persönliche Cloud einrichten, Nextcloud als primäre Anwendung verwenden und sicherstellen, dass Ihre Einstellungen sicher und zuverlässig sind. Warum eine persönliche Cloud auf Ubuntu erstellen? Ubuntu ist das beliebteste Linux

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

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

WebStorm-Mac-Version

WebStorm-Mac-Version

Nützliche JavaScript-Entwicklungstools

MinGW – Minimalistisches GNU für Windows

MinGW – Minimalistisches GNU für Windows

Dieses Projekt wird derzeit auf osdn.net/projects/mingw migriert. Sie können uns dort weiterhin folgen. MinGW: Eine native Windows-Portierung der GNU Compiler Collection (GCC), frei verteilbare Importbibliotheken und Header-Dateien zum Erstellen nativer Windows-Anwendungen, einschließlich Erweiterungen der MSVC-Laufzeit zur Unterstützung der C99-Funktionalität. Die gesamte MinGW-Software kann auf 64-Bit-Windows-Plattformen ausgeführt werden.

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

Herunterladen der Mac-Version des Atom-Editors

Herunterladen der Mac-Version des Atom-Editors

Der beliebteste Open-Source-Editor

SublimeText3 Englische Version

SublimeText3 Englische Version

Empfohlen: Win-Version, unterstützt Code-Eingabeaufforderungen!