Ubuntu 20.04 Essentials
上QQ阅读APP看书,第一时间看更新

10.1 User Management from the Command-line

New users may be added to an Ubuntu system via the command-line using the useradd utility. To create a new user account, enter a command similar to the following:

# adduser john

Adding user `john' ...

Adding new group `john' (1001) ...

Adding new user `john' (1001) with group `john' ...

The home directory `/home/john' already exists. Not copying from `/etc/skel'.

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

Changing the user information for john

Enter the new value, or press ENTER for the default

Full Name []:

Room Number []:

Work Phone []:

Home Phone []:

Other []:

Is the information correct? [Y/n] Y

By default, this will create a home directory for the user in the /home directory (in this case /home/john). To specify a different home directory, use the --home command-line option when creating the account:

# adduser --home /users/johnsmith john

Once the account has been created, the password can be changed at any time using the passwd tool:

# passwd john

Changing password for user john.

New password:

Retype new password:

passwd: all authentication tokens updated successfully.

An existing user may be deleted via the command-line using the deluser utility. While this will delete the account, the users files and data will remain intact on the system:

# deluser john

It is also possible to remove the user’s home directory and mail spool as part of the deletion process:

# deluser --remove-home john

All users on an Ubuntu system are members of one or more groups. By default, new users are added to a private group with the same name as the user (in the above example, the account created for user john was a member of a private group also named john). As an administrator, it makes sense to organize users into more logical groups. For example all sales people might belong to a sales group, while accounting staff might belong to the accounts group and so on. New groups are added from the command-line using the addgroup command-line tool, for example:

# addgroup accounts

Use the adduser tool to add an existing user to an existing group from the command-line:

# adduser john accounts

To remove a user from a group, use the deluser command as follows:

# deluser john accounts

An existing group may be deleted from a system using the delgroup utility:

# delgroup accounts

Note that if the group to be deleted is the primary or initial group for any user it cannot be deleted. The user must first be deleted, or assigned a new primary group using the usermod command before the group can be removed. A user can be assigned to a new primary group using the usermod -g option:

# usermod -g sales john

# delgroup accounts

To find out the groups to which a user belongs, simply run the groups command. For example:

$ groups john

john : accounts support

By default, only the first user account created on an Ubuntu system has the ability to use the sudo command to perform privileged tasks. If a newly added user attempts to use sudo, a message similar to the following will be displayed:

john is not in the sudoers file. This incident will be reported.

To add the user to the sudoers file, simply add the user to the sudo group:

# adduser john sudo