How-to

The su Command: Switch Users on Linux

su starts a shell as another user after asking for that user's password. With a dash it gives a clean login environment, and with -c it runs a single command.

Why does the dash in su - matter?

su postgres
su - postgres
su --login postgres

Plain su switches the user but keeps most of your current environment: the working folder, PATH and many variables. su -, su -l and su --login are the same and start a login shell, which resets the environment, reads the target user's profile files and moves to their home. Use the dash almost always. Without it, you may run commands as root with a regular user's PATH, which is both confusing and a security risk.

Whose password does su ask for?

The target account's. su - with no name switches to root and asks for root's password. On Ubuntu the root password is locked by default, so su - fails there unless you set one, which we do not recommend. Root itself can su to anyone without a password. That is the key difference from sudo, which asks for your own password and checks a policy file; see sudo vs su.

How do I run one command as another user?

su - backup -c 'rsync -a /srv/data/ /mnt/backup/data/'
su -l deploy -c "cd ~/app && git pull --ff-only"

-c runs the string through the target user's shell and returns. Quote carefully: single quotes stop your current shell from expanding variables, so $HOME becomes the target user's home. In scripts run as root, this is a tidy way to drop privileges for one step.

How do I switch to a service account with no shell?

Service accounts often have /usr/sbin/nologin as their shell, so su - www-data prints "This account is currently not available". Override the shell for that session:

sudo su -s /bin/bash -l www-data
sudo -u www-data -s
sudo -u www-data php /var/www/shop/artisan cache:clear

-s picks the shell for this switch only. The sudo forms do the same and are logged per user, which is better on shared machines. Running app maintenance commands as the app's own account avoids files owned by root that the app later cannot write.

Can I keep my environment when switching?

su -m deploy
su -w EDITOR,LANG - deploy

-m or -p preserves the whole environment. Newer util-linux su supports -w to whitelist specific variables while still starting a login shell, a cleaner middle ground. Be careful preserving variables when switching to root; a manipulated PATH or LD_PRELOAD is a classic privilege trap.

How do I restrict who can use su?

On the RHEL family and Debian, PAM can limit su to members of one group. Uncomment this line in /etc/pam.d/su:

auth required pam_wheel.so use_uid group=wheel

On Debian and Ubuntu, create a group such as suadmins and name it in the line. Only members can then use su at all, even with the correct password. Combined with key only SSH and a locked root password, this closes several common attack paths. Harden SSH on Linux covers the SSH side.

Where are su sessions logged?

sudo grep -E 'su(\[|:)' /var/log/auth.log | tail      # Debian, Ubuntu
sudo grep -E 'su(\[|:)' /var/log/secure | tail        # RHEL family
journalctl _COMM=su --since today

Each switch is logged with the source and target user and whether authentication succeeded. A login shell opened with su shows who switched, but not what they did afterwards. sudo logs every command instead, which is the main reason teams prefer it.

Should I use su or sudo?

Questionsusudo
Whose passwordTarget accountYour own
Needs a root passwordYes, for rootNo
Per command loggingNoYes
Fine grained rulesNoYes, in sudoers
Works with locked rootOnly from rootYes

On your own server with several admins, sudo is the better default. su stays handy for root running something as a service account, and on FreeBSD, where it is the traditional tool. The sudo command shows how to set sudo up properly.

How do I leave an su session?

Type exit or press Ctrl+D. You return to the previous user. Nested su sessions stack, so exit pops one level at a time; whoami and echo $SHLVL show where you are.

Can I use su in scripts without a password?

Only when the script already runs as root, since root can switch to any account without one. For other cases, use sudo with a narrow rule in sudoers that allows a single command without a password, rather than storing anyone's password in a file. That keeps automation working while every action stays logged.

Entry Machines for Your First Server

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E1Intel Xeon E3-1240 v34 / 816 GB DDR32x 240 GB SSD2x 1 Gbps$30.52$91.56 (3 months)
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)

Keep Reading

sudo vs su

Two ways to act as root: su switches accounts with the target password, sudo grants logged, per-command rights with yours.

Open

The sudo command

How sudo decides who may run what, the flags you use daily, safe sudoers rules, logging and the habits that keep root powers contained.

Open

usermod command

Change an existing Linux account with usermod: add groups without losing old ones, rename, move home folders, lock, expire and change shells.

Open

Harden SSH on Linux

A practical hardening list for sshd on a fresh dedicated box: keys, users, rate limits, firewall scope and an outside audit.

Open

FAQ

Quick Answers

It starts a login shell with a clean environment and the target user's profile.

The root password is locked by default. Use sudo -i instead.

Use sudo -u www-data command, or su -s /bin/bash -c with root.

It logs the switch only. sudo logs each command.

Type exit or press Ctrl+D.