Difference between su and sudo command in Ubuntu

In this article, we would cover the difference between su and sudo command in Ubuntu. Every user in a Linux distribution has certain privileges. A system administrator gets the maximum number of privileges, this helps him/her manage the distribution better. To make sure the system is secure enough, we can’t provide superuser privileges to every user. Users who don’t have enough experience in handling systems may inadvertently harm the system. Therefore, we draw a line on who gets what while working on the system.

Most of us who have access to superuser privileges would usually log in as regular user and then, use either su or sudo commands to execute administrative tasks.

Note: Following operations would require you to have superuser privileges. In case you don’t have one then, we advise you to contact your System Administrator for assistance.

Difference between su and sudo command in Ubuntu

We first start with su command. su (or substitute user) allow us to log in a user through a shell.

By default it logs in as root user. But, we can also use the command to log in with other users as well. So, for example – if we want to log in as root user then, we would use –

su -l

For other users, following would work –

su -l <user_name>

It is worth mentioning here that, Ubuntu may not allow you to log in through this method. It would throw an error if we try to access the superuser through this method – su: Authentication failure. Reason: root password is not set during installation setup. Therefore, we need to provide root password first in order to make it work. This is a one time exercise. Once, we have set the root password – we are good to go.

sudo passwd root

We can now have access to su -l.

Next, we move our focus on sudo command. sudo command is mainly used to grant temporary privileges to the regular user. With su command, we were provided root’s shell which had all the privileges. But, when we run a command through sudo, it runs the command as superuser. The privileges won’t be permanent with sudo. If we run another command in same shell without sudo, it would run as regular user. No specific shell is provided here.

So, through sudo command we can have superuser privileges only when we require. Otherwise, things would work just like it would for a regular user.

We would like to add here that, not all users have access to sudo command in Ubuntu. Only the user we have created at the time of installation can have the access to sudo command. If you want to let another user have access to sudo command then, follow the article.

Conclusion

A specific shell is provided with the su command, whereas only temporary privileges are granted through sudo command. We also saw here, how we can enable access to these commands for different users.