Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management

Time:2021-8-19

The Linux Su command is used to change the current user to the identity of other users. Any identity switching can be realized through this command, including switching from ordinary users to usersrootUser, slaverootWhen a user switches to an ordinary user or between ordinary users, except root, the user’s password needs to be entered. What does this mean?

For switching between ordinary users and switching from ordinary users to root users, you need to enter the other party’s password to achieve switching; Switching from root user to other users can be successful without the other user’s password.

1、 Linux user switching command:su

The format of the Su command is as follows:

Su [options] user name

Common options of this command and their meanings are as follows:

  • -: not only switch the current user to the specified user, but also switch the working environment to this user’s environment (including path variable, mail variable, etc.). Use the – option. If you omit the user name, you will switch to root by default.
  • -l: action and previous parameter-Similarly, while switching user identities, the working environment is completely switched, but the account to be switched must be added later
  • -p-m: indicates to switch to the identity of the specified user without changing the current working environment
  • -c: switch the user to execute the command only once, and automatically switch back after execution
1. For example, the user currently logged in to the system ischuan, I want to switch torootUser:

$ su root

2. Switch account torootAnd in executionlsAutomatically switch to the original user after command:

$ su -c ls root

Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management
What exactly does that mean? My current login account ischuan, in executionsu -c ls rootAfter the command, we first need to enter the root user password. After entering enter, the system willrootUser executionlsAfter the command is executed, it will automatically switch back tochuanUsers.

3. Note that when using the Su command, there – and no – are completely different

-Option means that while switching the user identity, even the currently used environment variable is also switched to that of the specified user. We know that environment variables are used to define the operating system environment. Therefore, if the system environment is not switched with the user identity, many commands cannot be executed correctly.

4. Switch back to the previous user

For example, the current login user ischuan, yessu rootAfter the command, you want to switch back tochuanUser, executeexitCommand.

IIwhoamiCommand andwho am icommand

Although the two command letters are the same, the only difference is that the second command has two more spaces, but these two are completely different. The former is used to print the user name of the current operation, and the latter is used to print the user name of the current Linux system.

For example, I’m logging in nowchuanUsers, execute these two commands respectively, as shown below:

Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management

Then I usesuCommand switch torootUser:

Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management

It can be seen that before switching users,whoamiandwho am iThe output of the command ischuanUser information, but usesuAfter switching users with the commandwhoamiThe command prints the user name after switching, andwho am iThe command still prints the user name used to log in to the system.

3、 Add user group command:groupadd

The command to add a user group isgroupadd, the information of the new workgroup will be added to the system file. The command format is as follows:

Groupadd [options] group name

Common options of this command and their meanings are as follows:

  • -g: Specifies the ID of the new workgroup
  • -r: create a system workgroup whose group ID is less than 500

User group related files:

  • /etc/groupGroup account information.
  • /etc/gshadowSecurity group account information.

For example, create a new group. The group name isgroupname1, and set the group ID to join the system:

groupadd -g 344 groupname1

Let’s look at the user group file/etc/group:

Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management

4、 Modify user group information:groupmod

This command is used to change the group ID or name. The command format is as follows:

Groupmod [options] group name

Common options of this command and their meanings are as follows:

  • -g GID: modify group ID
  • -N new group name: modify group name

For example, the group name created in the previous stepgroupname1Change totestgroup

$ groupmod -n testgroup groupname1

Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management

Tip: do not modify the group name and GID at will, because it is very easy to cause confusion in the administrator’s logic. If you have to modify the user name or group name, it is recommended that you delete the old one first and then create a new one.

5、 Delete user group information:groupdel

groupdelThe command is used to delete the specified workgroup. If some users are still included in the group, these users must be deleted before deleting the group. The command format is as follows:

Groupdel group name

Through the previous study, we know that usinggroupdelThe command to delete a group is actually delete/etc/gourpDocuments and/etc/gshadowData information about the target group in the file.

For example, now the system is calledgroupname2By viewing the above two files, you can see the information of the group. After executing the user group delete command, you can’t find the above two file information again:

Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management

Note: usegroupdelGroups cannot be deleted at will. If a group is still the initial group of a user, it cannot be deleted.

Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management

As shown in the figure above, execute the commandgroupdel chuanDelete namedchuanFailed to delete group.

Linux user management has been basically explained here. The next chapter will not start. We will start to explain the relevant knowledge of Linux package management.

Explanation of Su, whoamI, groupadd, groupmod and groupdel commands for Linux user management

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]