21

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.

7 Answers7

30

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

alexf
  • 309
29

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.

Pilot6
  • 92,041
17

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.

maria s
  • 311
16

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.

Rinzwind
  • 309,379
14

I see everyone is answering what happens when you log on as root (you get a ~ directory that isn't in /home) but no one is saying why. Here's why:

If the entire /home structure becomes unusable and you have to fix it, or if you need to reallocate /home because it's running out of space, you have to do so somehow. You can't log on as any user that has ~ in /home, because you'd crash as soon as you tried to do anything. So the root user has hir ~ elsewhere.

Jennifer
  • 276
8

You say you're not an advanced user, so I'll try to write a step-by-step answer.

When you use Linux, you have to remember some important things:

  • Linux is a multi user system. It was conceived like that from the beginning, following the Unix philosophy (notice the different approach taken by Windows, which was conceived as a single user system)

  • As a multi user system, Linux has the ability to separate the files belonging to each user in the system. The way Linux accomplishes this is by assigning each user a subfolder inside the /home/ directory. Each user files are owned by him/her, and only (s)he can open them, modify them or allow other users to use them. Conversely, each file in the filesystem belongs to a user, and that user has power over who can see or use that file. As mentioned in another answer, originally users could put their files anywhere in the system, but at some point it became evident that things should be better organized, and /home/ ("slash-home") became the place where user's files should be.

  • There are, however, files that the system itself and/or every single user in the system needs: binaries, device files, system files, etcetera (remember: in Linux, and in any other unix-like system, everything is a file). This globally needed files are stored in various places along the filesystem, organized in various folders within the root of the filesystem: /. Also, these globally needed files don't belong to a particular user of the system, but to the system itself... but that can be confusing: who owns the binaries? Who gives permissions over these globally needed files?

  • Since every file in the system "needs an owner", there must be a "special user" who can administrate the globally needed files. Moreover, this "special user" can do things no other user in the system can do: he can create other users for the system. This user is called "super user" (for us the mortals) or "root" (for the system).

  • Now, this superuser is another user of the system, but has powers (and responsibilities) far above those of other users: maintenance, for example. So it's natural to think that "root" needs a place to put his stuff, but It must not be confused with the stuff belonging the other users. So, instead of having a subfolder below /home/, "root" has his own reserved space in the filesystem: /root/ (called "slash-root"). Remember: "root" is a very special user, so it needs a special, privileged place to put his stuff.

  • Now, Ubuntu does not have a root account enabled by default, so a "normal" user has to have the authority over the system as a whole. That's where the sudo ("switch user and do") command kicks in: it allows a normal user (previously included in the "sudoers" list) to perform commands as if (s)he was other user, for example "root".

  • Finally, that -i options mean "simulate initial login". That means that sudo will read the data of the "target user's profile" before executing the given command. If you don't provide a command, then sudo will start a shell instance with the target user's (root) profile... and that starts with moving to the "home" folder of the "target user". So sudo -i will move you to do root's home folder (/root/).

I hope this helps you understand what happens with sudo -i.


EDIT

I think I left somethings unclarified, so I'll add them here:

  • I said above: "in Linux, everything is a file". And I mean it, literally! Your documents and images are files, but also terminals (those you can access pressing [CTRL]+[ALT]+[Fn]), and physical drives, and your keyboard, for example. A unix-like system works by reading or writing data from/to files, and directing the data flow to/from the appropriate files in the system, which represent the file you're working with, and the input of keys you are typing, and the output you're seeing on screen. Some of these files can be used directly by users, but some others cannot; for example, you can't read or write directly to a hard drive, but you must let Linux to link a normal, browsable folder, to the device file (under /dev/) representing your hard drive.

  • A Linux filesystem can span multiple physical drives. A simple example is when you plug a USB pen drive: Linux can mount that drive (mounting is the process by which the system connects a standard folder, where you can see files in a shell or file navigator, to a device file in /dev/) and you can work with it, and when you're done, you ask Linux to dismount the drive and then remove it from the USB port. The important thing here is that this "mount - unmount" cycle affects *the same filesystem" you use everyday: you don't create a new filesystem everytime you add or remove a physical drive, but you add that physical drive to the filesystem (again, compare this philosophy with Wndows' approach).

  • Since the filesystem can span multiple physical drives, it becomes evident that different files of the system can be written into different physical drives. One drive can store the system binaries (/bin/) and another can store user's files (/home/ and its descendants). In a multi-drive setup, it is common to have /home/ written in a different physical drive than /root/, so, if the system breaks and users can't log in because the drive where /home/ stops working, root can. (This is simplistic... many things need to be copied into each physical drive to allow root to log in if no other user can, but this can give you a general idea).

  • And that tilde (~) character... it denotes the home directory of the current user. If you are logged in as "bob", then cd ~ will take you to /home/bob/, but if you're logged in as "root", then cd ~ will take you to /root/

tl;dr So now I think everything is said:

  • "root" is a special user, with powers and responsibilities far above other users

  • "/root/" is the place where "root" can store its stuff, without risking it being confused with other stuff belonging to normal users. This folder can be written into a different physical drive than /home/.

  • sudo -i starts a shell simulating root's login, and that implies moving to root's home folder. As with any other user, cd ~ will take you to your home directory, but, if you are root, it will be /root/

  • If the system is installed across different physical drives, root can login and try to fix things even if another drive in the system fails.

Barranka
  • 191
7

The core reason for root's home directory to be treated differently:

When things go wrong you need to be able to perform system recovery tasks as soon as you manage to mount the / filesystem.

Depending on your setup user home directories might be on a different filesystem or they might even be mounted across the network.

In order to log in as root (i.e. enter the name root and the root password at a login prompt) from the console you only need the one filesystem mounted.

(This is also why core utilities are in /bin and /sbin instead of /usr/bin or /usr/sbin -- everything in /usr is expendable.)

arp
  • 171
  • 3