Why is a user with root privileges not in ~ anymore?

Just out of curiosity, I would like to know why, when I log in as root, I am not in /home/user anymore. What's the reason and what does /root directory do exactly?

I'm not an advanced user. Please answer in simple words or give a link that provides clear-cut, simple explanations. I use Ubuntu 16.04 and I log in as root by sudo -i. As it's explained here, sudo -i has /root as home. I want to know what the reason is; is there any advantage to be there? And not in ~ like the user of sudo -s.


Solution 1:

The reason why the home of the root user is /root and not /home/root is because usually /home is a mount point to a separate partition/volume/disk... (for various reasons, such as disk space, or remote, ...)

If for some reason mounting /home would fail, you could still connect as root and be in your /root home directory to investigate and fix things

Moreover, for maintenance, initial setup, resizing, ... you would be connected as root and should be able to unmount/remount /home

Solution 2:

You are not logging as root by running a sudo command. You are starting a shell with root privileges.

If you want to stay in the current user home directory, you can use sudo -s instead of sudo -i command.

cd ~ will take you to the same directory as if you are not in a shell with root privileges. Literally /home/$USER.

When you use sudo -i, the system acts as if you are logged in as root user. Because of this

cd ~ 

brings you to the root user home directory that is /root.

/root directory is a home directory for the root user.

The main difference is that shell settings files like .bashrc are used from /root in case of sudo -i, and from a normal user in case of sudo -s.

Solution 3:

The Linux filesystem is structured in a specific way. Essential binaries are in /bin/, boot loader files are in /boot/, most device files are in /dev/, mount points for removable media are in /media/, etc...

See https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard.

Some minor details may differ from distro to distro (e.g. /usr/bin/ vs. /usr/local/bin/), but in general almost all Linux distros follow the same directory structure.

To answer your question:

The users' home directories are in /home/. In principle, Linux is a multi user operating system. You may just have one user account on your laptop with its home directory in /home/<username>/, but if you look into /home/ on a shared Linux server, you will see many home directories: one for each user account. The idea is that every user of the system has write permissions only in their own home directory. If your username is bob you can read and write and delete files in /home/bob/ but you cannot touch anything in /home/alice/ or in /var/log/.

root is different though. root is the administrative user and has write privileges everywhere on the system (and can act as any user of the system). So it makes sense that root has the special home directory /root/ because root is not a regular user. Other than that, /root/ is just a regular directory with no special magic, although it is quiet possible (even likely) that system utilities rely on /root/ being the home of user root.

When you execute sudo -i in a terminal, you switch from being e.g. the regular user bob to being root. Note that this switch affects only the terminal window where you typed sudo -i. For your file manager you are still bob and if you open another terminal window you are still bob in there. In this context the symbol ~ is a shorthand for the current user's home directory. For bob ~ means /home/bob/ but for root ~ means /root/.

I hope that clarifies things for you.

Solution 4:

What's the reason and what does /root directory do exactly?

root has /root as its home and when you switch to root it will have you end up in its home. That is the nature of sudo -i. sudo -sdoes the same but not switch directories. Manual for sudo:

-s [command]

The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in the password database. If a command is specified, it is passed to the shell for execution via the shell's -c option. If no command is specified, an interactive shell is executed.

-i [command]

The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution via the shell's -c option. If no command is specified, an interactive shell is executed. sudo attempts to change to that user's home directory before running the shell. The security policy shall initialize the environment to a minimal set of variables, similar to what is present when a user logs in. The Command Environment section in the sudoers(5) manual documents how the -i option affects the environment in which a command is run when the sudoers policy is in use.

The root user needs to be part of the system. When you put /home on a different partition and root was part of /home you would run into serious trouble if the partition did not mount. For the same reason we also have

/bin
/sbin 

and

/usr/bin
/usr/sbin

When you put /usr on a partition and /usr does not mount you still have a working system.