Easy to understand the difference between the Linux commands sudo, su, sudo su and sudo su -command

Easy to understand the difference between the Linux commands sudo, su, sudo su and sudo su -command

The su command (switch user) is a command to switch users.

[ec2-user@humidai ~]$ su ec3-user

This will switch the user if a user named ec3-user exists.

If you omit the user in the su command, you are specifying root.

[ec2-user@humidai ~]$ su
password:

Enter the password; if you do not know the root password, you cannot switch to the root user; you will need the password to change to the root user with the su command.

Execute the specified command with root privileges using the sudo command

There are times when you want to execute a command with root privileges without knowing the root user’s password.

You can use the sudo command to execute commands with root privileges. (You need to give the permission with visudo.)

sudo command

Now you can execute commands with root privileges. “sudo vi hoge.txt” will create hoge.txt as root.

[ec2-user@humidai ~]$ ls -la
total amount 28
drwxrwxr-x 11 ec2-user ec2-user 193 12月 12 13:10 .
drwx------ 18 ec2-user ec2-user 4096 12月 11 20:32 ..
-rw-r--r-- 1 root root 3 12月 12 13:10 hoge.txt ★

This does not change the user to root, but allows the command to be executed with root (specified user) privileges.

sudo su in plain language

The sudo command executes the specified command with root privileges. su command changes the user.

The sudo su command executes the su command with root privileges. (Same as sudo su root)

Now you can change to the root user without knowing the root user’s password.

Difference between “sudo su” and “sudo su -“.

sudo su and sudo su – are almost the same but different.

The difference is that the home directory after the command is executed changes.

[ec2-user@humidai ~]$ sudo su -
[root@humidai ~]# pwd
/root

[ec2-user@humidai ~]$ sudo su
[root@humidai ec2-user]# pwd
/home/ec2-user

A hyphen (-) changes the home directory to the root home directory.

Environment variables (printenv) and shell variables (set) also change to root variables.

command environment variable
sudo su -(sudo -s) does not change
sudo su -(sudo -i) does not change

コメント

タイトルとURLをコピーしました