How to use the SSH su command

The SSH su (substitute user) command lets you switch to another user account and run commands as that user. In particular, it’s used on VPS and dedicated servers to become root or another system user without logging out. Furthermore, su loads that user’s environment (home directory, PATH, etc.) so you can work as if you had logged in directly. Note: su typically requires root or sudo access; shared hosting accounts usually cannot use su.

Switch to another user

To switch to a different user:

su anotherUser

You will be prompted for that user’s password. To switch to root, use su or su root. To get a login shell (full environment as if you had logged in), use su - anotherUser or su - for root. The hyphen loads the target user’s profile.

Preserve your environment variables

To keep your current environment (PATH, variables) while running as another user, use -m or -p:

su -m anotherUser

Useful when you need another user’s permissions but want to keep your shell settings. Without -m, su may reset some variables.

Specify a shell (users without login shell)

Some users have no shell (e.g. /usr/sbin/nologin) for security. To switch to them and use bash anyway:

su -s /bin/bash anotherUser

This overrides the user’s default shell. Use only when you need to run commands as a service or system user that normally cannot log in.

Run a single command as another user

To run one command without starting an interactive shell:

su -c "command" anotherUser

Example: su -c "whoami" www-data. After the command finishes, you return to your original user.

su vs sudo

su switches you to another user (you need their password, or root’s). sudo runs a single command as root or another user using your own password. On many systems, sudo is preferred for administrative tasks because it logs who did what. Use su when you need a full session as another user.

Common use cases

Typical uses for SSH su command on a VPS or dedicated server:

  • Becoming root to perform system administration
  • Running commands as the web server user (e.g. www-data) to fix permissions
  • Testing scripts or configs as a different user
  • Debugging issues that only occur for a specific user

To exit the su session and return to your original user, type exit or press Ctrl+D.

Need more help?

Explore more SSH and hosting guides in our knowledgebase. JetHost web hosting offers VPS and dedicated servers with full root access for advanced users.