Let’s talk about “Linux users” - one of the key concepts in Linux. 1
There are two main categories of users: non-privileged users and root which is also referred to as the 2
superuser or the administrator. Root exists on any Linux system. 3
What privileges are the powers that the root account has on the system. 4
The root account is the most privileged on the system and has absolute power over it. Among its privileges are 5
the ability to run any command or to access any file on the system. 6
Actually, it can do anything on a Linux system, such as installing a new software or updating the existing 7
ones, setting up the network, changing the ownership of files, managing other users accounts, or 8
administering any service that's running. 9
On the other hand, all other users, which are called non privileged or normal users, have no special 10
rights on the system. 11
They can only work in a directory called user home directory and cannot perform any administrative 12
tasks like installing new software, creating other users or running administrative commands. 13
Because the root is so powerful, it's not recommended to use it for ordinary tasks such as browsing 14
the web, writing e-mails or documents. 15
Even a simple mistake, like, for example, a mistyped command can cause problems to the entire system. 16
So for ordinary tasks, it's highly recommended to use a normal user account. 17
And when root permissions are needed, you simply become root only to perform that particular administrative 18
task. 19
On many Linux distributions like those based on Debian or Ubuntu the direct logging in as root is prohibited. 20
Let’s see how to gain root access in the terminal! 21
There are more approaches and I'll show you all of them. 22
The first way to temporarily become root in a terminal is to run: sudo su 23
By the way sudo stands for “super user do” and su stands for “substitute user”. It's asking for 24
the user's password. 25
Note that this is not the root password, it's the non privileged users password that run the command. 26
I'm entering the password, and if the password is right and the user is part of a specific group which 27
is called sudo on Ubuntu and wheel on CentOs then a new shell with root privileges starts and the 28
user can run there administrative commands as root. You can say that you've temporarily become root in 29
that terminal until you logged out from it. To see the user you are currently logged in run the id 30
command. 31
This command prints out the logged in user and the groups it belongs to. To log out, you type exit or 32
press Ctrl + D. 33
Note that when becoming root, if you add a white space and a hyphen after su, then the working environment 34
is activated. 35
The current working directory was changed to the root home directory. 36
This executed all of the root users shall initialization scripts and set all environmental variables 37
as if the root user was logged into a fresh shell session. By convention on Linux, all users prompt ends 38
in a dollar sign. 39
Root is the exception 40
as its prompt ends in a #. 41
A second way to run commands as root is without logging into the root account, but by using the sudo command and 42
the command you want to run as root. 43
If you prefix a command with sudo, you are prompted for your password (not the root password), 44
and if your user belongs to a specific group (sudo on Ubuntu and wheel on CentOS) the command will run 45
with root privileges. 46
Let's see how this works. 47
I want to run an administrative command that creates a new group. 48
If I ran it simply by typing the command, I'll get a permission denied error. 49
Only root is allowed to run the group add command and create a new group on the system. 50
I'm simply running the command. 51
And I've got a permission denied error. Now let's write sudo before the command the name. 52
And the command was executed successfully. A new group was created. 53
Note that I’m not logged in as root but as the initial normal user. 54
I'm student. 55
Using sudo makes it much more difficult to forget that you are root since you are not logged into the 56
account and you don't need to log out after you've finished your job. 57
Let's try to run a second command as root. This time I'm going to create a new user on the system. 58
sudo useradd John 59
And you notice that the command ran as root without asking for any password, as in the first example 60
when I ran the sudo command. That's because the sudo policy caches for five minutes, the credentials 61
I've entered in the first place, and any time I ran the sudo command the timestamp is updated to five 62
minutes. 63
In this example, after I ran the sudo useradd command, I could run other commands as root without 64
being asked for any password for exactly 5 minutes. By running sudo with the -v option a user 65
can update the cached credentials without running a command. 66
And if I want to invalidate the user's cached credentials, I need to run sudo-k 67
In other words, the next time sudo is run, a passport will be required. Let's create a new user! 68
And a password is required. 69
The last method of running commands as root is to temporarily become root in that terminal by running 70
the su command and entering the root password. 71
Note that on some distributions like Ubuntu based, this method is by default disabled because direct 72
access to root is locked. 73
Remember that you have installed Ubuntu in the first place without setting a password for the root account. 74
To unlock the account 75
on such a distribution, you simply have to set its password. 76
Note that not having a password doesn't mean that without it you can login as root. But on contrary, 77
it means that you cannot login at all. 78
I'm setting the root password. 79
The command used to change a password in Linux is passwd: 80
So sudo passwd: and the user name root 81
It's prompting for my password so that the command will run as root, I'm entering my password 82
and now the new password for the root account. 83
I enter it two times. 84
Note that on Linux when you are entering a password it doesn't display it; it's a security measure 85
so that no one can find out how long the password is. Type the password, even though you don't see it. 86
Now, I can temporarily login as root by running su and entering the root password; so su 87
and I'm entering the new root password. 88
And I'm root in this terminal. This method of becoming root is available only in a terminal. 89
You are still not allowed to login as root in the graphical environment. On CentOs this method of 90
becoming root is by default available 91
and the root password is the one you've set at the installation time. 92
I am entering the root password. 93
And I'm root. 94
Note that a user can change its own password by running the password command 95
and root can change the password of any user by running the password command and the username whose 96
password will be changed. 97
Root is changing the password sort of a user called student. 98
The password shouldn't be shorter than 8 characters. 99
That’s all. You’ve just learned how to run administrative commands as root in Linux. At the end of this 100
lecture 101
I want to clear for you that the word root has also several additional related meanings when used as 102
part of other terms. 103
This can be a source of confusion for the newcomers to Linux systems. 104
Let me give you some examples. 105
One of the most frequently met terms is the root directory, which is the top level directory of any 106
Linux file system. 107
All other directories and files reside in this directory. 108
The root directory is designated by a forward slash. 109
This is the contents of the root directory. 110
Another term is /root. 111
This is the root user's home directory. A home directory is the primary directory of a user's file and it's 112
actually the directory in which a user finds itself when it logs into a system. 113
/root is a subdirectory of the root directory, as indicated by the forward slash at the beginning, and it should 114
not be confused with that directory. 115
This is root and this is /root. 116
Home directories for users other than root are by default created in the / home directory, which is another 117
standard subdirectory of the root directory. 118
This is where the home directories of the other users are located.