Bookmark this page

Guided Exercise: Running Commands as root

In this lab, you will practice running commands as root.

Outcomes

Use the su with and without login scripts to switch users. Use sudo to run commands with privilege.

Reset your serverX system.

  1. Log into the GNOME desktop on serverX as student with a password of student.

  2. Open a window with a Bash prompt.

    Select ApplicationsUtilitiesTerminal.

  3. Explore characteristics of the current student login environment.

    1. View the user and group information and display the current working directory.

      [student@serverX ~]$ id
      uid=1000(student) gid=1000(student) groups=1000(student),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
      [student@serverX ~]$ pwd
      /home/student
      
    2. View the variables which specify the home directory and the locations searched for executable files.

      [student@serverX ~]$ echo $HOME
      /home/student
      [student@serverX ~]$ echo $PATH
      /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/student/.local/bin:/home/student/bin
      
  4. Switch to root without the dash and explore characteristics of the new environment.

    1. Become the root user at the shell prompt.

      [student@serverX ~]$ su
      Password: redhat
    2. View the user and group information and display the current working directory. Note the identity changed, but not the current working directory.

      [root@serverX student]# id
      uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
      [root@serverX student]# pwd
      /home/student
      
    3. View the variables which specify the home directory and the locations searched for executable files. Look for references to the student and root accounts.

      [root@serverX student]# echo $HOME
      /root
      [root@serverX student]# echo $PATH
      /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/student/.local/bin:/home/student/bin
      
    4. Exit the shell to return to the student user.

      [root@serverX student]# exit
      exit
      
  5. Switch to root with the dash and explore characteristics of the new environment.

    1. Become the root user at the shell prompt. Be sure all the login scripts are also executed.

      [student@serverX ~]$ su -
      Password: redhat
    2. View the user and group information and display the current working directory.

      [root@serverX ~]# id
      uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
      [root@serverX ~]# pwd
      /root
      
    3. View the variables which specify the home directory and the locations searched for executable files. Look for references to the student and root accounts.

      [root@serverX ~]# echo $HOME
      /root
      [root@serverX ~]# echo $PATH
      /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
      
    4. Exit the shell to return to the student user.

      [root@serverX ~]# exit
      logout
      
  6. Run several commands as student which require root access.

    1. View the last 5 lines of the /var/log/messages.

      [student@serverX ~]$ tail -5 /var/log/messages
      tail: cannot open ‘/var/log/messages’ for reading: Permission denied
      [student@serverX ~]$ sudo tail -5 /var/log/messages
      Feb  3 15:07:22 localhost su: (to root) root on pts/0
      Feb  3 15:10:01 localhost systemd: Starting Session 31 of user root.
      Feb  3 15:10:01 localhost systemd: Started Session 31 of user root.
      Feb  3 15:12:05 localhost su: (to root) root on pts/0
      Feb  3 15:14:47 localhost su: (to student) root on pts/0
      
    2. Make a backup of a configuration file in the /etc directory.

      [student@serverX ~]$ cp /etc/motd /etc/motdOLD
      cp: cannot create regular file ‘/etc/motdOLD’: Permission denied
      [student@serverX ~]$ sudo cp /etc/motd /etc/motdOLD
      
    3. Remove the /etc/motdOLD file that was just created.

      [student@serverX ~]$ rm /etc/motdOLD
      rm: remove write-protected regular empty file ‘/etc/motdOLD’? y
      rm: cannot remove ‘/etc/motdOLD’: Permission denied
      [student@serverX ~]$ sudo rm /etc/motdOLD
      
    4. Edit a configuration file in the /etc directory.

      [student@serverX ~]$ echo "Welcome to class" >> /etc/motd
      -bash: /etc/motd: Permission denied
      [student@serverX ~]$ sudo vim /etc/motd
      
Revision: rh124-7-1b00421