Wednesday, 28 August 2019

Linux su and sudo commands - what is different?


   There are two Linux command to switch to other users account or execute command as another user: su and sudo.

    The key difference is that to use su commands you have to know another user password, but to use sudo your don't to need know the password of the other user (when using sudo you are prompted for your password).

    The sudo configuration controlled by the system administrators (file /etc/sudoers).


su username                      - run command as username
su - username                   - login to username account with username's                                                              environment
su - user1 -c ls                -  execute ls command as user1 in user1's home                                                           directory

sudo -l                                - list all available commands
sudo command                   - run command as root
sudo -u user command       - run command as user
sudo su                               - switch to superuser account
sudo su  -                            - switch to superuser account with root's environment

Sudo configuration

visudo command - starts vi to edit /etc/sudoers file (root privileges required)

sudoers format:
user (host)=(users)[NOPASSWD:]commands

admin ALL=(ALL)[NOPASSWD:]ALL  - allow user admin from all hosts to                                                                            execute all command as root without
operator webserver1=/usr/sbin/poweroff  - allow user operator to power off                                                                               server webserver1

No comments: