The su command is continuously used together with different instructions like sudo for brief increased get right of entry to, passwd for converting passwords, and whoami to test which consumer you’re lately logged in as. Working out su is the most important for device management duties and for customers who want to perform in a multi-user atmosphere.

So, let’s get began and discover ways to successfully use the su command to modify consumer accounts and lift permissions.

Common syntax for su command:

$ su [OPTIONS] [USER [ARGUMENT...]]
1. Login with a unique consumer account
su [username]

Move the username to the su command, and it’ll supply a login consultation when the password has been verified.

Instance 1: Switching to Root Person

For those who run su with none username, it defaults to the basis consumer. You’ll be induced to go into the basis password.

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

Instance 2: Switching to a Explicit Person

To modify to a selected consumer, you’ll be able to specify the username as a controversy. As an example, to modify to a consumer named john, you can do:

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

Instance 3: Operating a Command as Some other Person

You’ll additionally run a unmarried command as some other consumer with out getting into their shell. As an example, to run whoami as john:

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

The -c choice means that you can run a selected command beneath a unique consumer account and show the ends up in the present login consultation.

Instance 1: Operating a Command as Root

Let’s say you need to run the ls /root command to record the contents of the /root listing, which is typically most effective obtainable through the basis consumer. You’ll use:

su root -c "ls /root"

After executing this command, you’ll be induced to go into the basis password. As soon as authenticated, the ls /root command might be finished, and also you’ll see the contents of the /root listing.

Instance 2: Operating a Command as Some other Person

Assume you’ve got some other consumer in your device named john, and you need to run a Python script positioned at /house/john/script.py as that consumer. You’ll use:

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

Once more, you’ll be induced to go into the password for the john account. As soon as authenticated, the Python script might be finished as john.

Instance 3: Operating More than one Instructions

You’ll additionally run more than one instructions in collection. As an example, to modify to a listing after which record its contents as consumer john, you’ll be able to do:

su john -c "cd /house/john/paperwork && ls"

This may increasingly transfer to the john consumer, navigate to /house/john/paperwork, after which record the contents of that listing.

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

Use the -s strategy to trade an current default shell to another one.

Instance:

Let’s say you’re lately logged in because the consumer john, and you need to modify to the consumer alice the use of the bash shell. You may execute the next command:

su -s /bin/bash alice

After working this command, you’ll be induced to go into the password for alice. If the password is right kind, you’ll be switched to alice’s consumer account and the shell might be bash.

4. Maintain consumer account atmosphere
su –p [another_user]

The -p choice means that you can stay the surroundings of the present consumer account.

Instance:

Let’s say you’re lately logged in because the consumer john, and you need to modify to the consumer alice however maintain john’s atmosphere variables.

Via typing within the following command:

su -p alice

You’ll be induced to go into the password for alice.

After effectively getting into the password, you’re going to be switched to the consumer alice, however your atmosphere will nonetheless include the settings from john.

Extra Linux instructions:
Listing Operations rmdir · cd · pwd · exa · ls
Document Operations cat · cp · dd · much less · contact · ln · rename · extra · head
Document Machine Operations chown · mkfs · find
Networking ping · curl · wget · iptables · mtr
Seek and Textual content Processing in finding · grep · sed · whatis · ripgrep · fd · tldr
Machine Knowledge and Control env · historical past · most sensible · who · htop · glances · lsof
Person and Consultation Control display screen · su · sudo · open

The publish The right way to Use the SU Command in Linux gave the impression first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/linux-command-su/

[ continue ]