Mastering Elevated Privileges: A Comprehensive Guide to sudo and su Commands in Linux

Try this guide with our instant dedicated server for as low as 40 Euros

sudo command in linux

What you can do and access on a Linux system is largely dependent upon the privileges available to your user account. 

Generally, user accounts in a Linux environment can be divided into two categories – superusers and normal users. 

Super users have wide-ranging privileges, and these users can carry out sensitive tasks such as mounting filesystems, editing critical configuration files, and making changes to system security applications. On the other hand, regular user accounts are restricted by the privileges available to their group or individual users. 

Many system administrators prefer logging into their systems as regular users to avoid security-related issues and only switch to superuser roles when they need these privileges. 

That’s where the switch user (su) and superuser do (sudo) command come into play. 

In this tutorial, we will take a close look at su and sudo commands. 

Table Of Contents

  1. A Short Introduction to the su Command
    1. The su Command Syntax
    2. The su Command Options
  2. How to use su and sudo Commands in Linux
    1. The Prerequisites
    2. The su Command Usage Examples
    3. Command Comparison: su vs sudo
  3. Conclusion
  4. FAQs

A Short Introduction to the su Command

The su command in Linux is a standard way of gateway to adopting another user’s identity, primarily for administrative tasks within a session. 

This command is of special significance because, in almost all Linux distributions, the root account is often disabled for added security. In this context, the su command provides a workaround. It allows a regular user to assume root privileges temporarily, thus overcoming command execution restrictions and carrying out tasks that require higher user permissions.

The su Command Syntax

When used without a username, the standard su command syntax switches to the superuser role.

# su

Alternatively, you can include a username to switch over to that user account. For instance, run the following command to switch over to a user account named harry:

# su harry

su harry

The su Command Options

Depending upon your Linux distribution, the su command may support several flags that modify its default execution and output. 

To display a list of commands, enter the following command:

# su -h

su -h

As you can see, the su command on our test system supports several options. 

How to use su and sudo Commands in Linux

Before going into the details of using these two critical commands, let’s check the prerequisites:

The Prerequisites

  • A system running a Linux distribution.
  • A user account with sudo privileges.

The su Command Usage Examples

Let’s see some important use cases of the su command:

Switch to a Different User

To change the current user in the terminal, use the following command:

# su -l harry

su -l harry

You will be prompted to enter a password. Once entered, the login will switch to the specified user.

We strongly recommend confirming the switch using the whoami command.

Note: If you are having trouble with login credentials, you can easily change the password for the root or sudo account using a few simple steps.

  1. Open Terminal: Use Ctrl+Alt+T or your system’s method to open the terminal.
  2. Become Root: Type su to become root. If you use sudo, type sudo su and enter your password.
  3. Change Password: Type passwd for root or passwd username for another user. Follow the prompts to enter a new password.
  4. Exit: Type exit to leave the root account.
  5. Check: Try logging in with the new password to make sure it works.

Use a Different Shell

You can switch the current shell with the -s flag. For instance, the following command switches over to the root user and switches to the Z shell.

# su -s /usr/bin/zsh 

su -s

This command initiates a root user account in the Z shell.

Command Comparison: su vs sudo

The sudo command

The sudo command grants a temporary elevation to root privileges for specific tasks, allowing for the execution of commands requiring higher permissions before returning to normal user status. Users must be added to the sudo group to utilize this option.

The su command

The su command is versatile, allowing a switch to another user. To execute commands as a non-root user, specify the username with the [username] option. 

The su command also supports switching between different shell interpreters, providing a broader range of functionality than sudo. The -c option with su can mimic sudo’s one-time command execution feature in a new shell session.

Conclusion

With a solid grasp of how to use the su command for making temporary user changes and running commands in Linux, you’re ready to put these techniques into practice. For an enhanced Linux experience and to fully leverage the power of your system, consider exploring the solutions offered by RedSwitches. 

RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What does the su command do in Linux?

The su command, or “switch user,” is utilized for executing commands as a different user within Linux.

Q. What are the requirements for using the su command?

Necessary prerequisites include a Linux system and terminal window/command-line access.

Q. How does the su command function?

The su command allows users to take on another user’s role, typically for administrative tasks within a session, enabling temporary root access to bypass restrictions and execute tasks with varied permissions.

Q. What is the su command’s syntax?

The su command defaults to the superuser role without a specified username. To switch identities, simply add the desired username to the command.

# su [options] [username]

Q. Can you illustrate the su command’s application in user switching?

To switch the active user in a terminal, input the command su [username] and provide the user’s password when prompted. Confirm the switch with the whoami command.

Q. How to resolve login credential issues with the su command?

If login credential problems arise, changing the root or sudo account password can be done with ease.

Q. Is switching shells possible with the su command?

Yes, the su command allows for initiating a root user session in an alternate shell, like the Z shell, with a specific command.

Q. How do su and sudo commands differ?

While sudo offers temporary root access for particular tasks, su enables a complete user switch. su also permits shell interpreter changes, offering a more versatile toolset than sudo.

Q. What advantages does su offer over sudo?

su, being an older command, provides additional functionalities, including the -c option for executing a single command within a new shell, akin to sudo’s capabilities.

Q. How to reset a forgotten root password?

Boot into single-user mode or use a live CD, then apply the passwd command to update the root password.

Q. Can I switch users in the same session?

Yes, use su – username to switch to another user without ending the current session.

Q. How do we check the current user in the terminal?

Use the whoami command to display the username of the current session.

Q. What is the difference between sudo and su for root access?

sudo executes a single command with root privileges, while su switches the entire session to the root user.

Q. Best practices for managing administrative passwords?

Use strong, unique passwords, update them regularly, and limit root access by preferring sudo for administrative tasks.

Try this guide with our instant dedicated server for as low as 40 Euros