RHCSA Rapid Track
Course update
An updated version of this course is available that uses a newer version of Red Hat Enterprise Linux in the lab environment. Therefore, the RHEL 9.0 version of the lab environment will retire on December 31, 2024. Please complete any work in this lab environment before it is removed on December 31, 2024. For the most up-to-date version of this course, we recommend moving to the RHEL 9.3 version.
In this exercise, you practice switching to the root account and running commands as root.
Outcomes
Use the
sudocommand to switch to therootuser and access the interactive shell asrootwithout knowing the password of the superuser.Explain how the
suandsu -commands affect the shell environment through running or not running the login scripts.Use the
sudocommand to run other commands as therootuser.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start users-superuser
Instructions
From
workstation, open an SSH session toserveraas thestudentuser.[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$Explore the shell environment of the
studentuser. 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 executable files.Run
idto 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
pwdto 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 the
rootuser in a non-login shell and explore the new shell environment.Run the
sudo sucommand at the shell prompt to become therootuser.[student@servera ~]$
sudo su[sudo] password for student:student[root@servera student]#Run
idto 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
pwdto 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/root/.local/bin:/root/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/binWhen you use the
sucommand to become therootuser, you do not keep the current path of thestudentuser. As you can see in the next step, the path is not therootuser path either.What happened? The difference is that you do not run
sudirectly. Instead, you run thesucommand as therootuser by usingsudobecause you do not have the password of the superuser. Thesudocommand overrides thePATHvariable from the environment for security reasons. Any command that runs after the initial override can still update thePATHvariable, as you can see in the following steps.Exit the
rootuser's shell to return to thestudentuser's shell.[root@servera student]#
exitexit [student@servera ~]$
Switch to the
rootuser in a login shell and explore the new shell environment.Run the
sudo su -command at the shell prompt to become therootuser.The
sudocommand might or might not prompt you for thestudentpassword, depending on the time-out period ofsudo. The default time-out period is five minutes. If you authenticated tosudowithin the last five minutes, then thesudocommand does not prompt you for the password. If more than five minutes elapsed since you authenticated tosudo, then you must enterstudentas the password for authentication tosudo.[student@servera ~]$
sudo su -[root@servera ~]#Notice the difference in the shell prompt compared to that of
sudo suin the preceding step.Run
idto 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
pwdto 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/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/binAs in the preceding step, after the
sudocommand resets thePATHvariable from the settings in thestudentuser's shell environment, thesu -command runs the shell login scripts forrootand sets thePATHvariable to yet another value. Thesucommand without the dash (-) option does not have the same behavior.Exit the
rootuser's shell to return to thestudentuser's shell.[root@servera ~]#
exitlogout [student@servera ~]$
Verify that the
operator1user can run any command as any user by using thesudocommand.[student@servera ~]$
sudo cat /etc/sudoers.d/operator1operator1 ALL=(ALL) ALLBecome the
operator1user and view the contents of the/var/log/messagesfile. Copy the/etc/motdfile to/etc/motdOLD. Remove the/etc/motdOLDfile. As these operations require administrative rights, use thesudocommand to run those commands as the superuser. Do not switch to root by usingsudo suorsudo su -. Useredhatas the password of theoperator1user.Switch to the
operator1user.[student@servera ~]$
su - operator1Password:redhat[operator1@servera ~]$Try to view the last five lines of
/var/log/messageswithout usingsudo. It should fail.[operator1@servera ~]$
tail -5 /var/log/messagestail: cannot open '/var/log/messages' for reading: Permission deniedTry to view the last five lines of
/var/log/messagesby usingsudo. It should succeed.[operator1@servera ~]$
sudo tail -5 /var/log/messages[sudo] password for operator1:redhatMar 9 15:53:36 servera su[2304]: FAILED SU (to operator1) student on pts/1 Mar 9 15:53:51 servera su[2307]: FAILED SU (to operator1) student on pts/1 Mar 9 15:53:58 servera su[2310]: FAILED SU (to operator1) student on pts/1 Mar 9 15:54:12 servera su[2322]: (to operator1) student on pts/1 Mar 9 15:54:25 servera su[2353]: (to operator1) student on pts/1Note
The preceding output might differ on your system.
Try to copy
/etc/motdas/etc/motdOLDwithout usingsudo. It should fail.[operator1@servera ~]$
cp /etc/motd /etc/motdOLDcp: cannot create regular file '/etc/motdOLD': Permission deniedTry to copy
/etc/motdas/etc/motdOLDby usingsudo. It should succeed.[operator1@servera ~]$
sudo cp /etc/motd /etc/motdOLD[operator1@servera ~]$Try to delete
/etc/motdOLDwithout usingsudo. It should fail.[operator1@servera ~]$
rm /etc/motdOLDrm: remove write-protected regular empty file '/etc/motdOLD'?yrm: cannot remove '/etc/motdOLD': Permission denied [operator1@servera ~]$Try to delete
/etc/motdOLDby usingsudo. It should succeed.[operator1@servera ~]$
sudo rm /etc/motdOLD[operator1@servera ~]$Return to the
workstationsystem as thestudentuser.[operator1@servera ~]$
exitlogout [student@servera ~]$exitlogout Connection to servera closed. [student@workstation ~]$
This concludes the section.