Linux user related operations

Time:2020-10-22

Create, delete modified user

You can refer to it firstLinux user and user group management

Create user

There are two forms of commands to create users in Linux

$addUser < user name >
The first is to automatically create the user directory in the home directory and prompt to create a login password.

$useradd < user name >
The second one is to specify the settings manually and actively, adding some optional parameters, such as:

  • -D directory specifies the user home directory. If this directory does not exist, you can create a home directory by using the – M option.
  • -G user group specifies the user group to which the user belongs.
  • -G user group specifies the additional group to which the user belongs.
  • -The S SHELL file specifies the login shell of the user.
  • -P password specifies the user login password

For example:
$ useradd -d /home/lilei -p 123 lilei
A user Lilei with a directory of / home / Lilei and a password of 123 was created

tip

1. Both Su < user > and Su – < user > switch users. The difference is that the former environment variable is still the original user’s, and the latter environment variable is also switched to the corresponding user’s. And when the parameter < user > is empty, both switch to root by default.

2. Add sudo permission to the user. Sometimes we don’t need to use the command of root authority frequently, so it is very troublesome to switch to root user for one or two commands. At this time, sudo appears. For details, please refer to:Linux adds sudo rights to users

delete user

There are also two forms of commands:

$deluser < user name >
However, this will only delete the user and will not delete the user’s home directory
$deluser — remove home < user name >
Will delete the user home directory together

$userdel < user name >
In the same way, only users are deleted, so you need to:
$userdel – R < user name >
In deleting users, the second form of command is more concise

Modify user

There is only one form of command to modify users:

$usermod [options] < user name >
The optional parameters here are the same as those of useradd:

  • -D directory specifies the user home directory. If this directory does not exist, you can create a home directory by using the – M option.
  • -G user group specifies the user group to which the user belongs.
  • -G user group specifies the additional group to which the user belongs.
  • -The S SHELL file specifies the login shell of the user.
  • -P password specifies the user login password

Change the user and user group to which the file belongs

$chown < new user name > < file name >
Modify the user of the file to a new user, such as:
$ chown lilei my.txt

$chgrp < new user group name > < file name >
Change the user group to which the file belongs, such as:
$ chgrp root you.txt

If you want to modify the user and user group of the file at the same time, you can:
$chown < new user name >: < new user group name > < file name >