Related commands for user and group management:
Useradd, Usermod, passwd, Userdel
Groupadd, Groupmod, GPASSWD, Groupdel
Chage, Chsh, CHFN
ID, W, who, WhoAmI
Su
Useradd: Create User
useradd [Options] LOGIN
useradd-d [Options]
-r: Create a System user
-U uid: Specifies uid;
-G GID: Specifies the base group to which the user belongs, which must exist beforehand
-C ' COMMENT ':
-d/path/to/somewhere: Specifies the user's home directory path; The location cannot exist beforehand, otherwise its user-related profile will be copied;/etc/skel
-S Shell: Sets the user's default shell;
Cat/etc/shells
-G GID,... : Specifies the additional group to which it belongs;
-M: Do not create home directory for users;
Example:
[[Email protected] ~] Useradd tank #添加用户 [[email protected] ~] useradd-r zhangy #添加系统用户 [[email protected] ~] UserA dd-d/home/zhangying zhangying #添加用户, and designation, home directory [[email protected] ~] useradd-g mytest-d/home/hao Hao #添加用户, referring to Set the home directory (this home directory cannot exist beforehand) [[email protected] ~] useradd-e 04/05/13 fxxk #添加用户, and set the expiration date for the user
Exercise: Create user Oracle, belong to additional group database and Sql,id number is 400, home directory is/home/database;
[[Email protected] ~] useradd-u 400-g database,sql-d/home/database Oracle
Groupadd: Creating Groups
Groupadd [OPTIONS] GROUPNAME
-G GID: Indicates the group ID;
-r: Create a system group;
Example:
Add a user group
[[Email protected] ~] Groupadd mytest
Add a user group and specify the GID
[[Email protected] ~] groupadd-g 444 test
ID: View user-related ID information
ID [OPTION] ... [USER]
-u:uid
-g:gid
-g:groups
-n:name
Example:
[[email protected] ~]# ID #显示当前用户的信息uid =0 (root) gid=0 (root) group =0 (root) [[email protected] ~]# ID zhangy # Show zhangy information uid=999 (zhangy) gid=999 (zhangy) group =999 (zhangy)
Su:switch user, switch users or execute commands as other users
Switching mode:
Su USERNAME: Non-complete switching, non-login switching
Su-username or su-l USERNAME: full switch, login switch
Executes the specified command only as the specified user:
Su-username-c ' COMMAND '
Example:
[[Email protected] ~] su zhangying #切换用户, the original user environment variable [[email protected] ~] su-zhangying #切换用户, new user environment variable [[email Protected] ~] su-c ls root #切换用户执行命令, return to the original user after execution
Usermod: User Property modification
usermod [OPTION] ... LOGIN
-u UID
-G GID
-G Gid[,gid,...] : Modify the additional groups that the user belongs to, and use the-a option to append additional groups;
-S SHELL
-C ' COMMENT '
-D Home: When you modify a user's home directory to a new location, the user's original file is not moved to the new home; the-m option allows it to be moved to a new home directory at the same time;
-L LOGIN:
-l:lock User
-u:unlock User
Example:
[[Email protected] ~] usermod-d/home/bak/zhangying #更改用户home目录 [[email protected] ~] USERMOD-E 05/06/13 zhangying
#给用户加个有效期
1. Add Newuser2 to the group staff# USERMOD-G Staff Newuser2
2, modify the NewUser user name is Newuser1# usermod-l Newuser1 NewUser
3. Lock Account Newuser1# usermod-l Newuser1
4, release the lock on the Newuser1# usermod-u Newuser1
passwd: Add a password to the user
passwd [OPTION] [UserName]
-l:lock User
-u:unlock User
-N mindays: Minimum period of use;
-X maxdays: Default is 99,999 days;
-W Warndays:
-I. Inactivedays:
--stdin: Receive user password from standard output;
echo ' CentOS ' | passwd--stdin CentOS
Example:
[[Email protected] ~] passwd zhangying #给zhangying修改密码
Userdel: Deleting users
Userdel [-R] USERNAME
-R: Delete the user's home directory at the same time;
Groupmod: Group Property Modification
Groupmod [OPTION] GROUPNAME
-N group_name
-G GID
Example:
[[Email protected] ~] Userdel fxxk #删除用户 [[email protected] ~] Userdel-r hao #删除用户, and delete the user home directory
GPASSWD: Set password for group
Usage: gpasswd[-a user][-d user][-a user,...] [-M user,...] [-R] [-r]groupname
Parameters:
-A: Adding users to Groups
-D: Remove a user from a group
-A: Specify administrator
-M: Specifies that the group member and-a use almost
-R: Remove password
GPASSWD groupname: If the system has a Peter account, the account itself is not a member of the GroupName group, using NEWGRP need to enter a password
Example:
[Email protected] ~]# gpasswd-a Peter Test2 #将peter设为test2组管理员 [[email protected] ~]# gpasswd-a Peter Test2 #将用户 Peter joins the Test2 Group [[email protected] ~]# gpasswd-d Peter Test2 #将用户peter从test2组中移出
NEWGRP: Toggles the base group for the specified group
Example:
[Email protected] ~]# newgrp www #登录到www群组
Description: The NEWGRP directive is similar to the login command when it is logged in to the system with the same account number and another group name. To use the NEWGRP command to switch groups, you must be a user of that group, or you will not be able to log in to the specified group. A single user who wants to belong to multiple groups simultaneously needs to take advantage of the alternate user's settings. If you do not specify a group name, the NEWGRP instruction will log in to the preset group for that user name.
Groupdel: Deleting a group
Example:
[[Email protected] ~] Groupdel Test #删除组test
Chage: Modify the properties of user account and password
chage [OPTION] ... LOGIN
Parameter meaning:
The minimum number of days that the-m password can change. At zero, the password can be changed at any time.
The maximum number of days that the-m password remains valid.
The number of days before the-W user password expires before the warning message is received.
The date that the-e account expires. This account will not be available after this day.
-D last Changed date
-I stasis period. If a password has expired these days, then this account will not be available.
The-l example shows the current setting. By non-privileged users to determine when their password or account expires.
Example:
1
[[email protected] ~]# chage-l zhangy #查看用户密码设定情况最近一次密码修改时间 : April 27, 2013 password expiration : Never password expiration time : Never account expiration : The minimum number of days between changing passwords from two times :-12 times the maximum number of days between password changes: -1 days before password expiration : -1[[email protected] ~]# chage-m Zhangy #密码有效期90天 [[email protected] ~]# chage-d 0 zhangy #强制用户登陆时修改口令 [[email protected] ~]# chage-d 0-m 0-m 90-w zhangy #强制用户下次登陆时修改密码, and set password minimum validity period of 0 and up to 90, 15 days advance alert prompt
# chage-e ' 2014-09-30 ' Test # Test This account is valid for 2014-09-30
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/72/26/ Wkiol1xdzl-tt5draaa54jigcja822.jpg "" 244 "height=" 143 ">
Other commands: Chfn: Change your finger message chsh: The login shell used to change the user. If the shell is not specified on the command line, CHSH can make a prompt. Finger: User Information lookup WhoAmI: Prints the user name associated with the currently active user ID. Same as Id-un. Who: Displays user information that is currently logged in
Usage: who [options] ... [File | parameter 1 parameter 2]
Example:
[[email protected] ~]# who #显示目前登入系统的用户信息tank tty1 2013-05-10 08:29tank pts/0 2013-05-10 08 : (192.168.56.1) [[email protected] ~]# who-a #显示目前登入系统的用户详细信息 system boot 2013-05-10 08:11 run Level 2 2 013-05-10 08:11 Login Tty4 2013-05-10 08:11 814 id=4 login Tty5 2013-05-10 08:11 825 id=5 Login Tty2 2013-05-10 08:11 842 id=2 login Tty3 2013-05-10 08:11 8 id=3 Login Tty6 2013-05-10 08:11 852 id=6tank-tty1 2013-05-10 08:29 01:08 13 95tank + pts/0 2013-05-10 08:29. 1493 (192.168.56.1) [[email protected] ~]# who-q #列出所有已登录用户的登录名与用户数量tank tank# number of users =2[[email protected] ~]# Who-u #列出已登录的用户tank tty1 2013-05-10 08:29 01:08 1395tank pts/0 2013-05-10 08:29. 1493 (192.168.56.1) [[email protected] ~]# who-r #查看已登录的用户的级别 runlevel 2 2013-05-10 08:11[[email protected] ~]# who-b #上次系统启动时间 system boot 2013-05-10 08:11
W: Displays user information currently logged into the system.
Syntax: w [-fhlsuv][user name]
-F turn on or off to show where users are logged into the system.
-H does not display the header information column for each field.
-L uses a detailed format list, which is a preset value.
-S uses a concise format list, which does not show the CPU time consumed by user login times, terminal stage jobs, and programs.
-U ignores the name of the executing program and the information that the program consumes CPU time.
Example:
[Email protected] ~]# W #示目前登入系统的用户信息 09:30:39 up 1:19, 2 users, load average:0.00, 0.01, 0.05USER TTY from [email protected] IDLE jcpu PCPU whattank tty1 08:29 1:01m 1.04s 0.91s-bashtank pts/0 192.168.56.1 08:29 0.00s 1.70s 0.13s sshd:tank [Priv]
Summary of usage of user management commands