The right way to Use the SU Command in Linux

by | Dec 7, 2023 | Etcetera | 0 comments

The su command is incessantly used along with other directions like sudo for short higher get admission to, passwd for changing passwords, and whoami to check which shopper you’re in recent times logged in as. Figuring out su is a very powerful for software control tasks and for purchasers who need to carry out in a multi-user setting.

So, let’s get started and learn how to effectively use the su command to change shopper accounts and raise permissions.

Commonplace syntax for su command:

$ su [OPTIONS] [USER [ARGUMENT...]]
1. Login with a novel shopper account
su [username]

Transfer the username to the su command, and it’ll provide a login session when the password has been verified.

Example 1: Switching to Root Individual

For individuals who run su without any username, it defaults to the root shopper. You’ll be caused to enter the root password.

$ su
Password: [Enter root password here]
# [You are now in a shell as the root user]

Example 2: Switching to a Particular Individual

To change to a decided on shopper, you’ll have the ability to specify the username as an issue. For example, to change to a client named john, you’ll be able to do:

$ su john
Password: [Enter john's password here]
$ [You are now in a shell as john]

Example 3: Working a Command as Any other Individual

See also  Learn how to Turn into an Skilled in The rest (And Write About It)

You’ll moreover run a single command as every other shopper without coming into their shell. For example, to run whoami as john:

$ su -c "whoami" john
Password: [Enter john's password here]
john
2. Login with a novel shopper account + run a command
su [username] -c [command]

The -c selection signifies that you’ll be able to run a decided on command underneath a novel shopper account and display the results in the existing login session.

Example 1: Working a Command as Root

Let’s say you want to run the ls /root command to document the contents of the /root record, which is normally best accessible during the foundation shopper. You’ll use:

su root -c "ls /root"

After executing this command, you’ll be caused to enter the root password. Once authenticated, the ls /root command may well be completed, and in addition you’ll see the contents of the /root record.

Example 2: Working a Command as Any other Individual

Suppose you’ve were given every other shopper for your software named john, and you want to run a Python script situated at /area/john/script.py as that shopper. You’ll use:

su john -c "python3 /area/john/script.py"

Yet again, you’ll be caused to enter the password for the john account. Once authenticated, the Python script may well be completed as john.

Example 3: Working A couple of Directions

You’ll moreover run multiple directions in assortment. For example, to change to a list and then document its contents as shopper john, you’ll have the ability to do:

su john -c "cd /area/john/forms && ls"

This will increasingly more switch to the john shopper, navigate to /area/john/forms, and then document the contents of that record.

3. Specify the shell
su -s /bin/[shell]

Use the -s technique to business an present default shell to any other one.

See also  How one can Create Merchandise Diversifications in Your Divi Cloud

Example:

Let’s say you’re in recent times logged in for the reason that shopper john, and you want to change to the shopper alice the usage of the bash shell. You might execute the following command:

su -s /bin/bash alice

After operating this command, you’ll be caused to enter the password for alice. If the password is correct, you’ll be switched to alice’s shopper account and the shell may well be bash.

4. Deal with shopper account setting
su –p [another_user]

The -p selection signifies that you’ll be able to keep the environment of the current shopper account.

Example:

Let’s say you’re in recent times logged in for the reason that shopper john, and you want to change to the shopper alice alternatively care for john’s setting variables.

By way of typing throughout the following command:

su -p alice

You’ll be caused to enter the password for alice.

After successfully coming into the password, you’re going to be switched to the shopper alice, alternatively your setting will nevertheless come with the settings from john.

Further Linux directions:
Record Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · much less · contact · ln · rename · extra · head
File Device Operations chown · mkfs · find
Networking ping · curl · wget · iptables · mtr
Search and Text Processing in finding · grep · sed · whatis · ripgrep · fd · tldr
Device Wisdom and Regulate env · historical past · most sensible · who · htop · glances · lsof
Individual and Session Regulate display screen · su · sudo · open

The put up The right way to Use the SU Command in Linux seemed first on Hongkiat.

See also  The Block Editor: A Good Transfer for WordPress Companies

WordPress Website Development

Supply: https://www.hongkiat.com/blog/linux-command-su/

[ continue ]

WordPress Maintenance Plans | WordPress Hosting

read more

0 Comments

Submit a Comment

DON'T LET YOUR WEBSITE GET DESTROYED BY HACKERS!

Get your FREE copy of our Cyber Security for WordPress® whitepaper.

You'll also get exclusive access to discounts that are only found at the bottom of our WP CyberSec whitepaper.

You have Successfully Subscribed!