Master su and sudo: Switch Users Safely and Harness Linux Privileges
This guide explains the differences between the su and sudo commands, shows how to create and manage users, demonstrates login-shell versus non‑login-shell switching, covers the -c option for one‑off commands, and compares security implications of each method in Linux environments.
1. Preparation
Before demonstrating user switching, create a few test users to use later. The Linux command to add a user is useradd, typically found in the PATH. If the command is not found, use the absolute path /usr/sbin/useradd. Only the root user can execute useradd, so switch from the regular ubuntu user to root first:
ubuntu@VM-0-14-ubuntu:~$ su -
Password: # enter root password
root@VM-0-14-ubuntu:~# useradd -m test_user
root@VM-0-14-ubuntu:~# ls /home
test_user ubuntuSet a password for the new user with passwd:
root@VM-0-14-ubuntu:~# passwd test_user
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfullyExit back to the regular user:
root@VM-0-14-ubuntu:~# exit
logout
ubuntu@VM-0-14-ubuntu:~$Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
