Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Linux - User Management: root and sudo

Back


Root User

su - root
id
# uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

sudo: superuser do

CMD DESC
sudo -l List available commands.
sudo command Run command as root.
sudo -u user command Run as user.
sudo -s Start a shell
sudo -u user -s Start a shell as user

Doing as Superuser (or Doing as Substitute User)

These users can then precede one of those commands with a utility called sudo (superuser do, a.k.a. substitute user do) at the time of executing that command. The users are prompted to enter their own password, and if correct, the command is executed successfully for them.

Configuration File


Entry format

# full access to every administrative function
root  ALL=(ALL) ALL    # root
user1 ALL=(ALL) ALL   # user
%dba  ALL=(ALL) ALL    # group
user1 ALL=/usr/bin/cat
%dba  ALL=/usr/bin/cat
# No password prompt for all
user1 ALL=(ALL) NOPASSWD:ALL
%dba  ALL=(ALL) NOPASSWD:ALL

# No password prompt for a command
john  ALL=(ALL) NOPASSWD: /bin/systemctl restart apache2
Cmnd_Alias  PKGCMD = /usr/bin/yum, /usr/bin/rpm
User_Alias  PKGADM = user1, user100, user200

PKGADM  ALL = PKGCMD

su -
# create a new usesr
useradd devops
passwd devops

# edit the /etc/sudoers file
sudo visudo

# devops ALL=(ALL) NOPASSWD: /bin/yum

# Test
# switch user
su - devops
sudo yum upgrade -y

# try other commands
sudo cat /
# Sorry, user devops is not allowed to execute '/bin/cat /' as root on rhelhost.localdomain.

Sudo command log

The sudo command logs successful authentication and command data to the /var/log/secure file under the name of the actual user executing the command

tail /var/log/secure
# Feb 15 19:42:23 ServerB groupadd[4391]: new group: name=dba, GID=5000
# Feb 15 19:43:20 ServerB usermod[4398]: add 'user100' to group 'dba'
# Feb 15 19:43:20 ServerB usermod[4398]: add 'user100' to shadow group 'dba'
# Feb 15 19:46:35 ServerB groupmod[4411]: group changed in /etc/group (group linuxadmin/5000, new name: sysadm)
# Feb 15 19:46:35 ServerB groupmod[4411]: group changed in /etc/gshadow (group linuxadmin, new name: sysadm)
# Feb 15 19:46:53 ServerB groupmod[4425]: group changed in /etc/group (group sysadm/5000, new gid: 6000)
# Feb 15 19:46:53 ServerB groupmod[4425]: group changed in /etc/passwd (group sysadm/5000, new gid: 6000)
# Feb 15 19:48:36 ServerB groupdel[4432]: group 'sysadm' removed from /etc/group
# Feb 15 19:48:36 ServerB groupdel[4432]: group 'sysadm' removed from /etc/gshadow
# Feb 15 19:48:36 ServerB groupdel[4432]: group 'sysadm' removed

wheel Group


Configuration File


Lab: Enable a user to perform any sudo command

# Create the New User
sudo useradd devops1
# Set a Password for the User
sudo passwd devops1
# Add the User to the wheel Group
sudo usermod -aG wheel devops1
# Verify
groups devops1
# devops1 : devops1 wheel

# Test the Configuration
# Switch to the devops1 user:
su - devops1
# test sudo command
sudo yum update