RHCSA Rapid Track
In this exercise, you will practice switching to the root account and running commands as root.
Outcomes
You should be able to:
Use sudo to switch to
rootand access the interactive shell asrootwithout knowing the password of the superuser.Explain how su and su - can affect the shell environment through running or not running the login scripts.
Use sudo to run other commands as
root.
Log in to workstation as student using student as the password.
On workstation, run lab users-sudo start to start the exercise.
This script creates the necessary user accounts and files to set up the environment correctly.
[student@workstation ~]$lab users-sudo start
From
workstation, open an SSH session toserveraasstudent.[student@workstation ~]$ssh student@servera...output omitted...[student@servera ~]$Explore the shell environment of
student. View the current user and group information and display the current working directory. Also view the environment variables that specify the user's home directory and the locations of the user's executables.Run id to view the current user and group information.
[student@servera ~]$iduid=1000(student) gid=1000(student) groups=1000(student),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023Run pwd to display the current working directory.
[student@servera ~]$pwd/home/studentPrint the values of the
HOMEandPATHvariables to determine the home directory and user executables' path, respectively.[student@servera ~]$echo $HOME/home/student[student@servera ~]$echo $PATH/home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
Switch to
rootin a non-login shell and explore the new shell environment.Run sudo su at the shell prompt to become the
rootuser.[student@servera ~]$sudo su[sudo] password for student:student[root@servera student]#Run id to view the current user and group information.
[root@servera student]#iduid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023Run pwd to display the current working directory.
[root@servera student]#pwd/home/studentPrint the values of the
HOMEandPATHvariables to determine the home directory and user executables' path, respectively.[root@servera student]#echo $HOME/root[root@servera student]#echo $PATH/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/binIf you already have some experience with Linux and the su command, you may have expected that using su without the dash (
-) option to becomerootwould cause you to keep the currentPATHofstudent. That did not happen. As you will see in the next step, this is not the usualPATHforrooteither.What happened? The difference is that you did not run su directly. Instead, you ran su as
rootusing sudo because you did not possess the password of the superuser. The sudo command initially overrides thePATHvariable from the initial environment for security reasons. Any command that runs after the initial override can still update thePATHvariable, as you will see in the following steps.Exit the
rootuser's shell to return to thestudentuser's shell.[root@servera student]#exitexit[student@servera ~]$
Switch to
rootin a login shell and explore the new shell environment.Run sudo su - at the shell prompt to become the
rootuser.[student@servera ~]$sudo su -[root@servera ~]#Notice the difference in the shell prompt compared to that of sudo su in the preceding step.
sudo may or may not prompt you for the
studentpassword, depending on the time-out period of sudo. The default time-out period is five minutes. If you have authenticated to sudo within the last five minutes, sudo will not prompt you for the password. If it has been more than five minutes since you authenticated to sudo, you need to enterstudentas the password to get authenticated to sudo.Run id to view the current user and group information.
[root@servera ~]#iduid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023Run pwd to display the current working directory.
[root@servera ~]#pwd/rootPrint the values of the
HOMEandPATHvariables to determine the home directory and the user executables' path, respectively.[root@servera ~]#echo $HOME/root[root@servera ~]#echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binAs in the preceding step, after sudo reset the
PATHvariable from the settings in thestudentuser's shell environment, the su - command ran the shell login scripts forrootand set thePATHvariable to yet another value. The su command without the dash (-) option did not do that.Exit the
rootuser's shell to return to thestudentuser's shell.[root@servera ~]#exitlogout[student@servera ~]$
Verify that the
operator1user is configured as to run any command as any user using sudo.[student@servera ~]$sudo cat /etc/sudoers.d/operator1operator1 ALL=(ALL) ALLBecome
operator1and view the contents of/var/log/messages. Copy/etc/motdto/etc/motdOLDand remove it (/etc/motdOLD). These operations require administrative rights and so use sudo to run those commands as the superuser. Do not switch to root using sudo su or sudo su -. Useredhatas the password ofoperator1.Switch to
operator1.[student@servera ~]$su - operator1Password:redhat[operator1@servera ~]$Attempt to view the last five lines of
/var/log/messageswithout using sudo. This should fail.[operator1@servera ~]$tail -5 /var/log/messagestail: cannot open '/var/log/messages' for reading: Permission deniedAttempt to view the last five lines of
/var/log/messageswith sudo. This should succeed.[operator1@servera ~]$sudo tail -5 /var/log/messages[sudo] password for operator1:redhatJan 23 15:53:36 servera su[2304]: FAILED SU (to operator1) student on pts/1 Jan 23 15:53:51 servera su[2307]: FAILED SU (to operator1) student on pts/1 Jan 23 15:53:58 servera su[2310]: FAILED SU (to operator1) student on pts/1 Jan 23 15:54:12 servera su[2322]: (to operator1) student on pts/1 Jan 23 15:54:25 servera su[2353]: (to operator1) student on pts/1Note
The preceding output may differ on your system.
Attempt to make a copy of
/etc/motdas/etc/motdOLDwithout using sudo. This should fail.[operator1@servera ~]$cp /etc/motd /etc/motdOLDcp: cannot create regular file '/etc/motdOLD': Permission deniedAttempt to make a copy of
/etc/motdas/etc/motdOLDwith sudo. This should succeed.[operator1@servera ~]$sudo cp /etc/motd /etc/motdOLD[operator1@servera ~]$Attempt to delete
/etc/motdOLDwithout using sudo. This should fail.[operator1@servera ~]$rm /etc/motdOLDrm: remove write-protected regular empty file '/etc/motdOLD'?yrm: cannot remove '/etc/motdOLD': Permission denied[operator1@servera ~]$Attempt to delete
/etc/motdOLDwith sudo. This should succeed.[operator1@servera ~]$sudo rm /etc/motdOLD[operator1@servera ~]$Exit the
operator1user's shell to return to thestudentuser's shell.[operator1@servera ~]$exitlogout[student@servera ~]$Log off from
servera.[student@servera ~]$exitlogout Connection to servera closed.[student@workstation ~]$