Sudo Command - Execute Commands as Another User | Online Free DevTools by Hexmos

Learn to execute commands as another user with the sudo command. Master sudo usage for elevated privileges, file editing, system commands, and more.

Sudo Command Guide

The sudo command in Linux and Unix-like systems is a powerful utility that allows a permitted user to execute a command as another user, typically the superuser (root). This is crucial for system administration tasks that require elevated privileges.

Understanding Sudo Command Usage

sudo stands for "superuser do" or "substitute user do". It enables users to run commands with the security privileges of another user (by default, root). This is a more secure alternative to logging in directly as root, as it allows for fine-grained control over which users can execute which commands.

Executing Commands with Sudo

Here are common ways to use the sudo command:

# sudo
# Execute a command as another user

# List contents of directory to which the user otherwise wouldn't have access.
sudo ls /usr/local/scrt

# Edit the given file as the `www` user. This is a great example of why sudo(8)
# is or was often, and more accurately, referred to as "substitute user do".
sudo -u www vi /var/www/index.html

# Shut down (halt) the machine when 10 minutes have passed. The quoted text is
# messaged to the terminal of all applicable users, known as a 'wall message'.
sudo shutdown -h +10 "Cya soon!"
# Note, that the above is the old method. On machines with SystemD, the below
# command can instead be used.
sudo systemctl reboot

# In Bash, `!!` (bang, bang) is an event designator, as described in bash(1), -
# and is used to refer to the previous command, synonymous for `!-1`.
#
# In this case, the user is able to prefix the entirety of the previous command
# with `sudo`, being most useful when forgetting that `root` access is needed.
sudo !!

# For use in the vim(1) modal text editor, this command allows the user to save
# the currently opened file as the `root` user, despite having not previously
# opened it with such privileges.
:w !sudo tee > /dev/null %

# Reset the current user's sudo(8) timestamp, resulting in the user having to
# once again enter his or her password when next using sudo(8). Use of this
# flag does not actually require `root` privileges.
sudo -K

# List the current user's sudo(8) privileges.
sudo -l

# Add a line to a file using sudo(8). This is especially useful when making
# changes to a kernel parameter file, like the `/proc/sys/vm/swappiness` file.
echo "foo bar" | sudo tee -a /path/to/some/file

# Begin a shell session as the system's `root` user.
sudo -i

# To disable password for sudo(8) for the `superuser` user, add the below line
# to the `/etc/sudoers` file, preferably by using the visudo(8) executable.
#
#     superuser ALL=(ALL) NOPASSWD: ALL
#
# This would result in the aforementioned user not needing to enter in a
# password when using `sudo`, otherwise he or she would be required to do so.
#
# Likewise, the below can be entered if this is wished for an entire group, -
# which in this case would be the `special` group.
#
#     %special ALL=(ALL) NOPASSWD: ALL
#
# Do note that neither of these configurations are at all recommended and can
# pose a massive security risk.

# Run `CMD` as the `root` user, but maintain the current user's environment. In
# systems like Ubuntu, this is assumed, but systems like Debian would require
# that the user make use of this flag when wanting to keep their environment.
sudo -E [CMD]

Advanced Sudo Options

The sudo command offers various options to control its behavior. For instance, sudo -u <user> allows you to run a command as a specific user, not just root. The sudo -i command starts an interactive shell as the root user, effectively logging you in as root.

Security Best Practices with Sudo

While powerful, sudo must be used responsibly. Misconfiguration can lead to significant security vulnerabilities. It's generally recommended to grant specific command privileges rather than blanket root access. Always use the visudo command to edit the /etc/sudoers file, as it performs syntax checking and prevents errors that could lock you out of your system.

Further Resources