20

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.

11
  • 11
    root is also a user, so he have his personal home directory, but this one is named root
    – damadam
    Jun 8, 2018 at 14:06
  • 3
    You can use sudo -s to be in the same user directory.
    – Pilot6
    Jun 8, 2018 at 14:16
  • 3
    @Pilot6 sorry I added that at the same time as I saw your comment :D Hope you don't mind :)
    – Rinzwind
    Jun 8, 2018 at 14:18
  • 2
    @Unbesiegbarkeit man sudo will answer the question. To be simple user -i is using the root user settings, but sudo -s the current user settings.
    – Pilot6
    Jun 8, 2018 at 14:19
  • 6
    Possible duplicate of What are the differences between "su", "sudo -s", "sudo -i", "sudo su"?
    – muru
    Jun 8, 2018 at 16:14

7 Answers 7

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

1
  • 7
    +1, This should be the accepted answer. You could change the home of root to be anywhere, but we leave /root to be on / and not at /home for recovery reasons.
    – ntg
    Jun 11, 2018 at 13:12
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.

13
  • 3
    What advantages has having /root as home?
    – user833907
    Jun 8, 2018 at 14:49
  • 2
    Files like .bashrc are used from there in a specific shell. In most cases there is no much difference.
    – Pilot6
    Jun 8, 2018 at 14:51
  • 11
    @Unbesiegbarkeit /home may not be mounted under certain circumstances, e.g. single user mode, no network access, etc., and root still needs to be able to log on in those circumstances.
    – doneal24
    Jun 8, 2018 at 16:37
  • 2
    @DougO'Neal If /home is not available, the problem doesn't exist. Then you really log in as root.
    – Pilot6
    Jun 8, 2018 at 16:43
  • 4
    @Pilot6: There are differences, or should be. For example, on my machines, I set up root's .*shrc file to make it visually apparent that I'm running as root - e.g. prompt in a different format and colored red. Also aliases are set up not to do certain things that might be harmful if done as root, &c.
    – jamesqf
    Jun 8, 2018 at 17:58
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.

2
  • 3
    This is a perfect answer!
    – Pilot6
    Jun 8, 2018 at 18:31
  • 5
    It's probably worth noting that Root's home directory was originally /. The standard location got changed for security reasons (everyone has to be able to list files in/, so anyone could see what dotfiles the root user had. Jun 9, 2018 at 1:05
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.

7
  • 2
    How does this answer the question? Is it a comment?
    – Pilot6
    Jun 8, 2018 at 14:07
  • 2
    The sudo -i was not part of the question when I answered ;-)
    – Rinzwind
    Jun 8, 2018 at 14:11
  • 2
    Do we have a Q&A about sudo -i vs sudo -s?
    – Pilot6
    Jun 8, 2018 at 14:18
  • 2
    Hmm we might. I'll have a search. edit I did not find one but I did remember seeing one but that was on our dutch ubuntu forums :D
    – Rinzwind
    Jun 8, 2018 at 14:18
  • 2
    It seems that I saw one here, but couldn't find either.
    – Pilot6
    Jun 8, 2018 at 14:20
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.

6
  • 3
    Try it, and depending on distro and security settings as a normal user you will not log in, or end up with / as cwd and $HOME just inaccessible. The point is that root will most likely want to have access to it's home directory when others don't, e.g. because of special needs files there. Similar on most fss you have reserved space for root, and system accounts have home dirs outside /home
    – PlasmaHH
    Jun 9, 2018 at 9:33
  • 2
    The answers from alexf and arp, posted later, are basically saying the same thing. Doesn't anybody read the existing posts before adding their own answer?
    – Jennifer
    Jun 9, 2018 at 13:50
  • @Dan -- I have rolled back your changes. To wit:
    – Jennifer
    Jun 12, 2018 at 8:46
  • (1) I suppose it's an improvement to put /home and ~ in tick boxes, but not all that important to do so, because everybody knows what I mean anyway. You knew what I meant. (2) "you'd crash as soon as you try" is inconsistent in tense. "you'd" is "you would" and "would" is past tense, so it has to go with "tried" which is past tense rather than "try" which is present tense. (3) "hir" is a new pronoun invented by Kate Bornstein; it covers "him" and "her" and is useful when you don't know the gender. ("Ze" covers "he" and "she".)
    – Jennifer
    Jun 12, 2018 at 8:53
  • @Jennifer No worries. You learn something new every day. I thought hir was a typo. Thanks for all the info! I do recommend keeping the code formatting tough, but that's up to you.
    – Dan
    Jun 12, 2018 at 9:08
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.

1
  • 1
    Perfect answer !
    – user833907
    Jun 10, 2018 at 11:06
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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.