Switching to root

Switching to root

Occasionally you may encounter a situation where you have to switch to the root user to perform a certain command. Before doing so you should be aware that, while working as the root user you are able to do anything. You can install files to the system areas, or delete all the data on your hard disk. Be careful what you do while logged in as root.

The command to switch from one user to another is su - which stands for "set user". When you enter this command without any parameters it will switch to the root user. Alternatively, you can follow it with the username to which you want to switch. In either case you will be prompted for the password for that user. When you enter the password you will be working as that user - you will be able to access the files that that user can access, perform tasks he is entitled to perform, etc. In the case of the root user, that means all files and all tasks.

Try it first with your own normal username. In this example, I am using ’ramon’ as the other username.

sandbox@laptop:~ > ls /home/ramon
Access denied.
sandbox@laptop:~ > ls 
file1.txt   file2.txt
sandbox@laptop:~ > su ramon
Password: (enter ramon’s password)
ramon@laptop:/home/sandbox > ls
Access denied.
ramon@laptop:/home/sandbox > ls /home/ramon
Desktop  Mail  Documents
ramon@laptop:/home/sandbox > exit
sandbox@laptop:~ > 

Note that, while logged in as "sandbox" I could list the contents of sandbox’s home directory but was denied access to ramon’s home directory, and vice versa. Note also that I used the exit command to exit from the "ramon" user back to the sandbox user. This is because the su command creates a new shell process using the "ramon" user.

If we use the su command without parameters we are prompted for root’s password. Once logged in as root we can access all areas.

sandbox@laptop:~ > su 
Password: (enter root’s password)
root@laptop:/home/sandbox > ls
file1.txt   file2.txt
root@laptop:/home/sandbox > ls /home/ramon
Desktop  Mail  Documents
root@laptop:/home/sandbox > exit
sandbox@laptop:~ > 

There is a difference between logging in as root (or any user) then loading the terminal, and starting off as another user, loading the terminal and then using su to switch to root. This difference lies in the startup-scripts that get loaded. When you use the "su" command as shown above, it inherits all the settings from the previous shell. Thus, for instance, if you have set up some environment variables they will be carried over and are still accessible as root. This is useful in some compilations. If you use your regular user to compile a program and then switch to root to perform the final installation step, it may be useful to carry over any environment variables you may have set up.

However this has the disadvantage that any commands in root’s startup scripts will not get executed. If you ever need to switch to root (or some other user) and ensure that the startup scripts DO get executed, add a hyphen (-) between the su command and the username parameter, if any. Thus, the commands used above would become:

su - ramon
su -

You will still be prompted for the password, but this time the startup scripts will be executed and any settings from your own shell session will be forgotten - it will be as if you had logged in as that user in the login screen.