- All right. If you are going to work with Linux,
it's important to know about the use of privileges.
In Linux, there is the ordinary user account
with limited possibilities.
And there is the root account,
or we should say the administrator level user account,
which does have access to more privileges.
So how does that work?
Well, it starts with the user root.
In the old days of Linux,
there was the root user and there was the ordinary user,
and the root user is unrestricted, and still is.
And for that reason,
you should not be working as a root user directly.
Modern Linux distributions are even disabling
the root account.
Well, disabling is a strong word.
They don't set a password for the root account
so that you cannot log in as the root user.
On Ubuntu, a root user doesn't get a password
and there's no option to change that while installing
and on recent versions of Red Hat family Linux,
you can choose whether or not you want to be able
to work as root.
But working as root is not a smart idea.
You better use sudo.
In order to use sudo, you need to have a user
that is a member of the group "wheel" on Red Hat
or "sudo" on Ubuntu.
And if you are a member of one of these groups,
then you can use sudo to run commands
with administrative privileges.
Alternatively, you can use su -.
Su - is a generic command that allows you
to open a shell as another user.
And that is very convenient if you need to configure setup
for other users.
Always use su with the minus command
because that ensures full access
to the target user environment.
The minus makes sure
that the new shell is started as a login shell.
And in that login shell, you have full access
to the variables that make the user environment.
And without a minus, that is not going to happen.
If you use su - without any arguments,
then your current shell is trying to open a root shell.
Obviously, that is only going to work
if your root user is enabled,
that means that you are going to be prompted
for a password, and the password that you enter
is the root password.
And well, you can guess what is going to happen
if root doesn't have a password, then you cannot use su -.
When you use su - with a username as argument,
a user shell is opened,
a user shell for that specific user account.
Now let's talk about sudo.
What exactly is sudo?
Sudo on Linux is the default mechanism
to allow you to run commands with enhanced privileges.
It allows authorized users to run tasks
with escalated privileges.
And if you want to use it, use sudo followed by the command
that you want to run, like sudo ls /root.
I really like sudo ls /root because it's simple
and it shows whether or not sudo is working
for your account.
But do remember, in order to be able to use sudo,
the user who is using sudo must be a member
of the sudo group, which is "wheel" on Red Hat
or "sudo" on Ubuntu.
Let's go check this out.
So here we are on the Ubuntu environment.
And I'm using sudo ls /root.
Why is this such a good command?
Well, "sudo" is opening a sudo shell,
"ls" is listing files,
and /root is the home directory of the root user.
So if I'm using sudo ls /root
then I can verify that I have permissions to do so.
It's prompting for a password,
so I'm entering my password.
And there we go.
We see the contents of the home directory
of the root user, just containing one file
with the name "snap".
But this proves the point that sudo is working
for this user account.
Let's also do this on the CentOS system.
So likewise, if I'm using sudo ls /root
then you see a message and we need to enter the password.
That is the password
for the current sudo enabled user account.
And there we go.
And you can see on the CentOS system,
there is one file with the name "anaconda-ks.cfg".
So that means that with sudo, I have full access.
And in case you are wondering,
"what is happening if I just type ls /root?"
Well then I'm getting "permission denied"
because just ls /root is running this command
as an ordinary user.
And as you can see,
the ordinary user does not have sufficient privileges.