Operations 11 min read

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.

Open Source Linux
Open Source Linux
Open Source Linux
Master su and sudo: Switch Users Safely and Harness Linux Privileges

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  ubuntu

Set 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 successfully

Exit back to the regular user:

root@VM-0-14-ubuntu:~# exit
logout
ubuntu@VM-0-14-ubuntu:~$
LinuxCommand LineSystem AdministrationSudosuuser switching
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.