TL;DR
This guide shows you how to make sudo more secure on your Linux system. We’ll cover restricting user access, logging, and preventing common mistakes.
1. Restrict User Access with /etc/sudoers
The /etc/sudoers file controls who can use sudo and what commands they can run. Never edit this file directly with a text editor! Use the visudo command instead. This provides syntax checking to prevent locking yourself out.
- Open /etc/sudoers safely:
- Grant specific commands: Instead of giving users full root access, allow only the commands they need. For example, to let a user restart Apache:
username ALL=(root) /usr/sbin/service apache2 restart - Use groups: Add users to a group and grant permissions to the group instead of individual users. This is easier to manage.
%admin ALL=(ALL) ALL(This allows all members of the ‘admin’ group full sudo access – use with caution!)
- NOPASSWD: Avoid using
NOPASSWDunless absolutely necessary. It removes the password prompt, reducing security.
sudo visudo
2. Logging and Auditing
Good logging is essential for detecting misuse of sudo.
- Check syslog:
sudologs to your system’s syslog (usually in/var/log/auth.logor/var/log/syslog). Review these logs regularly for suspicious activity. - Configure logging options: You can adjust the level of detail logged in
/etc/sudoersusing thelog_outputoption.Defaults log_output=SYSLOG,PRIVILEGED(This logs all privileged commands to syslog.)
- Consider auditd: For more advanced auditing, use the
auditdsystem. This allows you to track specific sudo events in detail.
3. Prevent Common Mistakes
These steps help avoid common security pitfalls.
- Disable root login: Prevent direct root logins via SSH. Use
sudoinstead. - Secure tty settings: Restrict access to the root account through tty devices in
/etc/securetty`. - Avoid wildcard characters: Don't use wildcards (*) in
/etc/sudoersunless you fully understand the implications. They can easily grant unintended permissions. - Regularly review /etc/sudoers: Periodically check your
/etc/sudoersfile to ensure it still reflects your security needs and that no unauthorized changes have been made.
4. Using `sudo -l` for Verification
The sudo -l command is a useful tool for checking what commands a user can run with sudo.
- Check user permissions: Run
sudo -l -U usernameto see the list of allowed commands for that user.
5. Cyber security Best Practice: Principle of Least Privilege
Always grant users only the minimum necessary privileges they need to perform their tasks. This limits the potential damage from compromised accounts.

