Working with Sudo and Su Commands in Ubuntu Linux
The sudo command lets you execute specific commands with elevated privileges, while the su command switches your entire Linux session to another user, typically root.
This distinction is vital for managing your Ubuntu system securely. Sudo allows temporary, command-specific elevated access, like when you run apt-get update.
In contrast, su opens a new shell session logged in as the target user, meaning all commands run with that userās permissions until you exit the session.
Directly using the root account can be dangerous; a mistake could easily damage your system. Sudo, first widely adopted around Ubuntu 8.04 LTS, provides a safer way to perform administrative tasks by granting temporary root access only for the command you execute.
Sudo executes a single command as another user, typically root, while su switches your entire session to that user. Use sudo command for specific elevated tasks, and su ā to log in as root for an extended session.
Sudo
The sudo command in Ubuntu lets you run other commands with special admin powers, like being the superuser. Itās a way to do things that normally require root access without logging out and back in. You control who can use sudo and what they can do through a special file.
With `sudo`, you can run commands with root power, as described above. This command lets you do practically anything in Ubuntu Linux, and itās controlled by definitions in the /etc/sudoers file.
The `sudo` command also provides safeguards before you make mistakes. It prompts you for confirmation before executing actions, making you think twice.
sudo apt update
su
The su command in Ubuntu lets you switch to a different user account, including the main administrator (root) account. To switch, youāll need to know the password for the account you want to switch to. For example, typing `su -` lets you become the root user.
The su command lets you switch to other accounts on the system, but youāll need to know the account password youāre switching to.
Example:
su -
The above command lets you switch to the root account. However, youāll need to know the root password to run the root user shell and environment.
Another example:
If you want to switch to a user on the system called john, you run the commands below:
su john
To switch to Johnās account and load his environment variables (like his home directory path), use the ā switch. This switch has the same effect as logging into the system directly as the user youāre switching to.
su - john
Thatās it!
This should help clear up some differences between the two commands. Remember, `sudo` lets you run commands as another user without changing your identity.
`su` allows you to switch to a user and change your identity.
Conclusion:
Weāve covered the main differences between sudo and su. If you spot any errors, please let us know using the form below.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, heās developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
[ā¦] Another way to access the PostgreSQL prompt without switching users is to use theĀ sudoĀ command. [ā¦]
[ā¦] What is the sudo or su command on Ubuntu Linux [ā¦]