To add, remove or modify user in linux, we can use this command:useradd, userdel, usermod. The other way to do this is just edit the /etc/passwd and /etc/group.
The general syntax for the useradd command:
* useradd [options] LOGIN;
* useradd -D;
* useradd -D [options].
The second line invoked with only the -D option, useradd will display the current default values. The third line invoked with -D plus other options, useradd will update the default values for the specified options.
The first line is the common way to add a user, the LOGIN is the user name, typically used for login. The most common options to create a normal user are as follows:
* -c, --comment COMMENT. This option can be any text string, just enter some words to describe this user, or just leave it empty.
* -d, --home-dir HOME_DIR. This option set the user's login directory(HOME_DIR) when the user is created. If the directory HOME_DIR does not exist, then it will be created unless the -M option is specified. If you don't set this option, it will use default value: the BASE_DIR(can be set by the "-b" option) appending the LOGIN name.
* -g, --gid GROUP. Set the user's primary group, The GROUP can be group name or group id(number), the group must exist, and the id must refer to a real group. If you omit this option, the group will be one of the following values:
♥ If the variable "USERGROUPS_ENAB" in /etc/login.defs is set to yes (or -U/--user-group is specified on the command line), a group with the same name as the loginname will be created.
♥ If the variable "USERGROUPS_ENAB" is set to no (or -N/--no-user-group is specified on the command line), the group will be the GROUP variable in /etc/default/useradd. If the GROUP is not set, the the group will be the one with gid 1000.
* -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]. Add a list of supplementary groups. This is optional groups, you can have 0 supplementary group is you want, the principle is the almost the same as "-g" option. Add variable "GROUPS" to the file /etc/default/useradd if you want add those supplementary groups to the new created users.
* -k, --skel SKEL_DIR. This option is only valid if the -m (or --create-home) option is specified. When the new user is created, the files and directories in the SKEL_DIR will be copied to new created home directory. The default value is set by the SKEL variable in /etc/default/useradd, or if the SKEL variable is not set, it will be /etc/skel.
* -p, --password PASSWORD. Set the initial password. Without this option, the new account will be locked until you set the password. It's not a good ideal to use this option in public, because the password is visible, the better way is leave it, then use the "passwd" command to set the password.
* -s, --shell SHELL. Set shell path. The default is the SHELL variable in /etc/default/useradd, if this variable is not set, the field for the login shell in /etc/passwd remains empty.
* -u, --uid UID. Set the numerical value of the user's ID. The value must be unique, unless you used the -o option. The default is the smallest number which between UID_MIN and UID_MAX(in the file /etc/login.defs).
Summary, most of this options have default value, so the simplest way to create an account is: useradd -c "comment" LOGINNAME,then use this command to set the password: passwd LOGINNAME.
The general syntax for the usermod command:
usermod [options] LOGIN.
The most common options are as follows:
* -c, --comment COMMENT. This option can change the comment of the user(in the file /etc/passwd).
* -d, --home HOME_DIR. Change home directory.
* -g, --gid GROUP. Change primary group. This will just change the group of the files which in the user's home directory to the new one, In other place, you should change the files' group manually.
* -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]. Change supplementary groups, the groups not mentioned here will be removed. use the -aG option to add supplementary groups,use the -rG option to remove supplementary groups.
* -l, --login NEW_LOGIN. Change the login name, nothing else. So the home directory or mail spool should be renamed manually.
* -m, --move-home. Combine with the -d(or --home) option to move the home directory to the new location.
* -p, --password PASSWORD. Change the password.
* -u, --uid UID. Set a new UID.
userdel [options] LOGIN. use the -r(or --remove) option to remove home directory and mail spool