Create and Delete Users

Key Notes:

There are three critical things that need to be done while adding a new user:

1. Add a new line in /etc/passwd file, with a unique User ID (UID) and login name

2. Either add a new line in the /etc/group file to create a new default group for the new user, or add the user's login name in front of an existing group.

3. Create a home directory for the user. This default directly should be owned by the new user.

Typically above three actions are accomplished by a tool, e.g. useradd, linuxconf etc. Tools also help to check for any syntax errors and hence prevent from problems resulting from keyboard errors in configuration files.

Syntax of each line in the file /etc/passwd is as follows:

username:passwd:UID:GID:full_name:home_dir:shell

E.g:
nobel:x:501:501:Alfred Nobel:/home/nobel:/bin/bash

In order to set the password of the new user, use the command passwd, E.g.:

# passwd nobel
<system will prompt you to type the new password twice>

Deletion of a user can be accomplished by undoing the three actions mentioned above. But note that in some cases just need to disable the login capability of a user but not remove their home directory. This can be accomplished by simply commenting out their corresponding line from /etc/passwd file or by putting /bin/false in place of their login shell. You can use tools like userdel, linuxconf etc. to delete users from a system.

 

Quick test:

Q. Identify the key problem with the following two lines in /etc/passwd which represent two unique users:

ck:x:830:801:Calvin Klein:/home/ck:/bin/bash
tommy:x:830:801:Tommy Hilfiger:/usr/people/tommy:/bin/tcsh

a) The home directories of the two users need to be in the same place on the system.
b) Two users need to have different default Group IDs.
c) Two users need to have the same default shell.
d) Two users need to have different and unique User IDs.

 

Answer:

d) The two users have the same user id 830. They need to have different user ids. Note that by default some distributions also create a new and unique group id for every new user. This is not necessary. In fact some distributions add all new users to the same default group. Also, while it is a good practice to have home directories of all users at the same place on the system, it is not enforced and you can create home directory of a user anywhere on the directory structure.