Becoming root with su
If you're already logged in as a regular user and want to make a quick admin change, you don't have to go through the hassle of logging out and logging back in as root. This could also be the case if you're trying to get into a Linux system over the network, and it doesn't let root users log in remotely for security reasons.
That's where the su (Substitute User) command comes in handy. It lets you "switch over" to another user account on the fly if you know the password.
If you try to use su on a system where a password has not been set for root (typical in default Ubuntu installs), you will notice that you receive an authentication error:

You can overcome this by using the sudo command, assuming that the user you are logged in with is a member of the sudo group.
Therefore, to authenticate as root, a sudo user can preface su with sudo, and when authenticating with their own password, the user will then become root:

Using this type of configuration adds security to the use of the root account, and is considered best practice.
When the root account DOES have a password set, then using su is super simple:
suWhen prompted, simply type the root user’s password. The prompt for the regular user ($) changes to the superuser prompt (#):

At this point, you have full permission to run any command and use any file on the system.
su Limitation: root User Environment
one thing that the su command doesn’t do when used this way is read in the root user’s environment. As a result, you may type a command that you know is available and get the message Command Not Found.
To fix this problem, use the su command with the dash (-) option instead like this:
su -
You still need to type the password as before, but after that everything that normally happens at login for the root user happens after the su command is completed. Your current directory will be the root home directory - probably /root, and things such as the root user’s PATH variable are used. If you become the root user by just typing su, rather than su -, you don’t change directories or the environment of the current login session.
Last updated
Was this helpful?