User Administration

Adding new users

Creating a new user most of the time is done by either running the GUI application User Manager or console application useradd (usually located in /usr/sbin).

The useradd command has the following options:

Option Description
-b BASE_DIR
--base-dir BASE_DIR
Set the default base directory for the system if -d dir is not specified. BASE_DIR is concatenated with the account name to define the home directory. If the -m option is not used, BASE_DIR must exist. Note: use the -D option first.
-c COMMENT
--comment COMMENT
Any text string. It is generally a short description of the login, and is currently used as the field for the user’s full name.
# useradd -c "Test User #1" test1
-d HOME_DIR
--home-dir HOME_DIR
home directory for the new user account. The new user will be created using HOME_DIR as the value for the user’s login directory. The default is to append the LOGIN name to BASE_DIR and use that as the login directory name. The directory HOME_DIR does not have to exist but will not be created if it is missing.
# useradd -d /home/testdir test2
-D
--defaults
print or save modified default useradd configuration
-e EXPIRE_DATE
--expiredate EXPIRE_DATE
set account expiration date to EXPIRE_DATE. The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD.
# useradd -e 29-02-2008 test3
-f INACTIVE
--inactive INACTIVE
Sets the number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature. The default value is -1.
-g GROUP
--gid GROUP
The group name or number of the user’s initial login group. The group name must exist. A group number must refer to an already existing group.
-G GROUP1[,GROUP2,...[,GROUPN]]]
--groups GROUP1[,GROUP2,...[,GROUPN]]]
A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening white space. The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group.
-k SKEL_DIR
--skel SKEL_DIR
Specify an alternative skeleton directory containing initial configuration files and login scripts that should be copied to a new user's home directory. This parameter can only be used in conjunction with the -m option.
-m
--create-home
The user’s home directory will be created if it does not exist. The files contained in SKEL_DIR will be copied to the home directory if the -k option is used, otherwise the files contained in /etc/skel will be used instead. Any directories contained in SKEL_DIR or /etc/skel will be created in the user’s home directory as well. The default is to not create the directory and to not copy any files.
-M The user’s home directory will not be created, even if the system wide settings from /etc/login.defs is to create home dirs.
-n A group having the same name as the user being added to the system will be created by default. This option will turn off this Red Hat Linux specific behavior. When this option is used, users by default will be placed in whatever group is specified in /etc/default/useradd. If no default group is defined, group 1 will be used.
-o
--non-unique
Allow the creation of a user account with a duplicate (non-unique) UID. Use with -u UID to create a user account that has the same UID as another user name. This effectively lets you have two different users with authority over the same set of files and directories.
# useradd -o -u 0 admin
-r This flag is used to create a system account. That is, a user with a UID lower than the value of UID_MIN defined in /etc/login.defs and whose password does not expire. Note that useradd will not create a home directory for such an user, regardless of the default setting in /etc/login.defs. You have to specify -m option if you want a home directory for a system account to be created. This is an option added by Red Hat
-p ENC_PASSWORD
--password ENC_PASSWORD
The encrypted password, as returned by crypt(3). The default is to disable the account. Since the password has to be specified in the encrypted form, it's easier to create a user without a password and then define one using the password command.
-s SHELL
--shell SHELL
The name of the user’s login shell. The default is to leave this field blank, which causes the system to select the default login shell.
# useradd -s /bin/zsh test4
If you wish do create a user that cannot login, you can use special nologin shell:
# useradd -s /bin/nologin -d /var/www/html webuser
-u UID
--uid UID
The numerical value of the user’s ID. This value must be unique, unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value greater than 999 and greater than every other user. Values between 0 and 999 are typically reserved for system accounts.
Once a new user is created, you can set the initial password using the passwd command:
# useradd -c "Test User" billy
# passwd billy
Changing password for user billy.
New UNIX password: *********
Retype new UNIX password: *********
# passwd -n 3 -x 90 -w 5 billy
Adjusting aging data for user billy.
passwd: Success
# passwd -S billy
billy PS 2008-02-20 3 90 5 -1 (Password set, MD5 crypt.) 

In creating a new account for Billy, the useradd command performs the following actions:

Modifying user accounts

Sometimes, users need to modify their accounts. This can done via the User Manager window or by means of the usermod command. The usermod is similar to the useradd command and even shares some of the same options. Some of the new options available for the usermod are:

If would like to change the description (full name) of an account we can use -c option
# usermod -c "New User Name"
or use the chfn command:
$ chfn -f "W.C. Chief" bobby
Additional arguments of this command are: The difference between the two approaches is that any user can change his/her own description using the chfn command, while he/she has to be a super user to deploy the usermod.

Similarly, anyone can change its own shell by using chsh command:

$ chsh -s /bin/zsh daniel

Deleting user accounts

The userdel command is used to remove an existing user. This command basically has two options:

Note that this command doesn't remove files that belong to the user but located in other parts of the file system. These files have to be seached for and deleted manually. This search can be performed by find command:

Group management

groupadd

Every group can have administrators, members and a password. The gpasswd command can be used to manage group password and administrator. This command if used with no options and with just one argument (name of a group) sets a pasword for the group.

If password is set the members can still newgrp without a password, non-members must supply the password.

newgrp is used to change the current group ID during a login session. If the optional flag is given, the user’s environment will be reinitialized as though the user had logged in, otherwise the current environment, including current working directory, remains unchanged.

# groupadd admin
# gpasswd admin
Changing the password for group admin
New Password: ******
Re-enter new password: ****** 
# gpasswd -a daniel admin
Adding user daniel to group admin
# su - daniel
$ whoami
daniel
$ echo test > test1
$ newgrp admin
$ echo test > test2
$ ls -l test?
??????????
$ exit
# su - mary
$ newgrp admin
Password: ******
Please predict the output of the ls command.

groupmod modifies the definition of a group. By definition we mean either the name of group ID (GID). The following options can be used:

groupdel command removes a group. Please note that you may not remove the primary group of any existing user. You must remove the user before you remove the group.

Homework assignment