If you're confused about what su and sudo commands do for you, this brief guide will help you sort them out. Show
Posted: March 30, 2021 |%t min read| by Bryant Son (Sudoer, Red Hat) Image Photo by Miguel Á. Padriñán from Pexels This article explores the differences between the [ You might also enjoy: Linux command line basics: sudo ] Working as root means that you have the power to:
Basically, you can do anything to the system as the root user. It is the all-powerful administrative account. And, unlike other more chatty operating systems, you won't see a, "Are you sure?" dialog to be sure that the 0 command you just issued was in 1 rather than at 2. As you can imagine, errors made as the root user can be irreversible and devastating. There is an alternative: sudo .sudo
5 file, you can issue single commands as root or as another user. To continue running commands with root power, you must always use the sudo command. For example, if you want to install the Nginx package, you run:
But you will see an error if you are not root or in the sudo group. Instead, if you run this command:
You will be asked to type your password, and then you can run the command if you are a part of the sudo group. A simple way to switch to an interactive session as a root user is the following:
More Linux resources
The theory behind using sudo is that the act of issuing the sudo command before any command you run makes you think more about what you're doing and hopefully make fewer mistakes with an account that possesses unlimited power. su
For example, if you type:
In the above example, you are switching to root and you need the root password. The ( 7) switch provides you with root's environment (path and shell variables) rather than simply giving you root user power for a single command while keeping your own environment.
For the second example, you are switching to bryant, and so you need bryant's password unless you are root. If you want to switch to the bryant user account including bryant's path and environment variables, use the ( 7) switch:
The ( 7) switch has the same effect as logging into a system directly with that user account. In essence, you become that user.Wrap upRecapping what you've learned.
[ Want to learn more about security? Check out the IT security and compliance checklist. ] But when do you use one, not another? Since the 5, this can give powerful permission controls. Since sudo can pretty much do everything that su can, I would say it is best to stick with sudo unless you are working with some legacy codes that require the su command.Check out these related articles on Enable SysadminImage Real sysadmins don't sudo Or do they? This opinion piece from contributor David Both takes a look at when sudo makes sense, and when it does not. Posted: April 17, 2020 Author: David Both (Sudoer alumni) Image Using sudo to delegate permissions in Linux This article explores some legitimate use cases for the sudo command. Posted: April 20, 2020 Author: David Both (Sudoer alumni) Image Linux command line basics: sudo Learn to elevate your work and your permissions by becoming a super user with sudo. Posted: February 11, 2020 Author: Tyler Carrigan (Editorial Team, Red Hat) Topics: Linux Linux administration Security Bryant SonBryant Jimin Son is a Consultant at Red Hat, a technology company known for its Linux server and opensource contributions. More about me Red Hat Summit 2022: On DemandGet the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand. Register for free Related ContentImage Find anything you need with fzf, the Linux fuzzy finder tool I'm thankful for the Linux fuzzy finder tool because it superpowers the command line by making it fast to find whatever I'm looking for. Posted: November 25, 2022 Author: Ricardo Gerardi (Sudoer, Red Hat) Image How to verify Mastodon users with cryptography Use open source GPG key pairs and Keyoxide to prove your identity on Mastodon. Posted: November 28, 2022 Author: Seth Kenlon (Editorial Team, Red Hat) Image Top 15 articles sysadmins are thankful for in 2022 Enable Sysadmin has published more than 1,200 articles; these are the favorites sysadmins turn to month after month. Which Linux command can be used to let you run a single command as another user the root for example )?3. Using su. su is a command-line tool that is commonly used to switch users in Linux. Additionally, it also allows us to execute scripts or commands as another user.
What command can you use to switch to a different user in Linux?You can change the user ID associated with a session (if you know that user's login name) by using the su (switch user) command.
What command allows you to run a shell as a different user?-u user The -u (user) option causes sudo to run the specified command as a user other than root. To specify a uid instead of a user name, use #uid.
Which command can be used to execute another command in Linux?exec command in Linux is used to execute a command from the bash itself. This command does not create a new process it just replaces the bash with the command to be executed.
|