Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Su Command in Linux (Switch User) - DesignLinux

Jun 24 2020
designlinux 0 Comments
Su Command in Linux (Switch User)

The su command is used to run commands as a another user. It is simplest way to switch to the administrative account in the current login session. It is very useful when the root user is not allowed to log in to the system through ssh. For example, in Ubuntu, root user is disabled by default to make more secure. This tutorial explains how to use su command.

How to Use the su Command#

The following is the basic syntax for the su command:

su [OPTIONS] [USER [ARGUMENT...]]

When the su command is run without any arguments it will run as interactive shell as root:

su

It will prompt you to enter root password, and if successfully authenticated then it will currently logged in user becomes temporarily.

The session shell (SHELL) and home (HOME) environment variables are set from substitute user’s /etc/passwd entry, and the current directory is not changed.

Execute the whoami command to confirm that user is changed:

whoami

It will print the name of the user running the current shell session:

root

With su the commonly used options are -, -l and --login. By using this shell converted to a login shell with environment same as real login and changed to the current directory.

su -

Use the -s, --shell option to run another shell session instead of the one defined in the passwd file.

su -s /usr/bin/zsh

You can use the -p, --preserve-environment option to preserve the entire environment (HOME, SHELL, USER, and LOGNAME) of the calling user.

su -p

When the - option is used -p is ignored.

Use the -c, --command option, to run a command as substitute user without starting an interactive shell. For instance, to invoke the ps command as root you would type:

su -c ps

You can switch to another user by passing username as an arguments to su. For example, to switch to the user tecnstuff you would type:

su tecnstuff

Sudo vs Su#

As we seen above that Linux systems like Ubuntu, the root user account is disabled by default to increase the security. So no password is set for root user and you cannot use su to switch to root.

One option to change to root would be to prepend the su command with sudo and enter the currently logged in user password:

sudo su -

Using sudo you can run the programs as another user and default is the root user.

If the user is granted with sudo access the su command will be invoked as root. Running sudo su - and using the user password is the same as running su - using the root password.

If the sudo used with the -i option, it will run an interactive login shell with the root user’s environment:

sudo -i

sudo -i is basically the same as running su -.

The root password doesn’t need to be shared among multiple administrative user accounts while using sudo, this is the main advantage over su.

With sudo, you also can allow users to run only specific programs with root privileges.

Conclusion#

The su is a command-line utility and used to run commands as a another user.

If you have any questions or feedback, please leave a comment below.

Related

Tags: sudo, terminal

How To Set Up Apache Virtual Hosts on Ubuntu 20.04

Prev Post

How to Install Gradle on Debian 10 Buster

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 2
  • 1,104
  • 571,459

DesignLinux.com © All rights reserved

Go to mobile version