用户创建命令 useradd
//语法:useradd [option] USERNAME
-u UID //[UID_MIN,UID_MAX]定义在/etc/login.defs文件中
-g GID //指定用户所属基本组,可为组名或GID
-G groupname,... //附加组,可以有多个,用逗号隔开。组groupname必须事先存在
-c "COMMENT" //注释信息
-d /path/to/directory //指定用户的家目录。此目录必须不能事先存在, \
//否则将不会从/etc/skel中复制环境设置文件
-s shell //这里的shell最好使用/etc/shells里面有的shell, \
// /etc/shells指定了当前系统可用的安全shell
-M //创建用户时不给其创建家目录
-r //添加一个系统用户
-D //直接打印/etc/default/useradd文件的内容或配合其它选项 \
//(例如-s SHELL)直接修改/etc/default/useradd文件中的默认值
[root@localhost ~]# useradd zhangsan
[root@localhost ~]#
[root@localhost ~]# useradd tom
[root@localhost ~]# useradd -G tom lisi
// -G groupname,... //附加组,可以有多个,用逗号隔开。组groupname必须事先存在
[root@localhost ~]# id lisi
uid=1012(lisi) gid=1012(lisi) 组=1012(lisi),1011(tom)
[root@localhost ~]# useradd -u 1020 -G tom wangwu
//-u UID //[UID_MIN,UID_MAX]定义在/etc/login.defs文件中
//-G groupname,... //附加组,可以有多个,用逗号隔开。组groupname必须事先存在
[root@localhost ~]# id wangwu
uid=1020(wangwu) gid=1020(wangwu) 组=1020(wangwu),1011(tom)
[root@localhost ~]# useradd -u 1021 -g tom -G zhangsan liuliu
// -u UID //[UID_MIN,UID_MAX]定义在/etc/login.defs文件中
// -g GID //指定用户所属基本组,可为组名或GID
// -G groupname,... //附加组,可以有多个,用逗号隔开。组groupname必须事先存在
[root@localhost ~]# id liuliu
uid=1021(liuliu) gid=1011(tom) 组=1011(tom),1010(zhangsan)
[root@localhost ~]# ls /home/
lisi liuliu tom wangwu zhangsan
[root@localhost ~]# ls /opt/
[root@localhost ~]# useradd -d /opt/chengdong chengdong
//-d /path/to/directory //指定用户的家目录。此目录必须不能事先存在, \
//否则将不会从/etc/skel中复制环境设置文件
[root@localhost ~]# ls /opt/
chengdong
[root@localhost ~]# su - chengdong
[chengdong@localhost ~]$ id
uid=1022(chengdong) gid=1022(chengdong) 组=1022(chengdong) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[chengdong@localhost ~]$ exit
登出
[root@localhost ~]#
[root@localhost ~]# mkdir /opt/chengxi
[root@localhost ~]# useradd -d /opt/chengxi/ chengxi
//useradd:警告:此主目录已经存在。不从 skel 目录里向其中复制任何文件。
[root@localhost ~]# ll /opt/chengxi -d
drwxr-xr-x. 2 root root 6 11月 9 21:39 /opt/chengxi
[root@localhost ~]# su - chengxi
-bash-4.2$ ls -a
. ..
-bash-4.2$ ls /etc/skel/ -a
. .. .bash_logout .bash_profile .bashrc
[root@localhost ]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
[root@localhost ]# useradd -s /bin/sh liwu
-s shell //这里的shell最好使用/etc/shells里面有的shell, \
// /etc/shells指定了当前系统可用的安全shell
[root@localhost ]# su - liwu
-sh-4.2$ ls -a
. .. .bash_logout .bash_profile .bashrc
-M: 创建用户时不给其创建家目录
-r //添加一个系统用户
[root@localhost ]# useradd -M -s /sbin/nologin -r admin
[root@localhost ]# id admin
uid=997(admin) gid=995(admin) 组=995(admin)
[root@localhost ]# ls /home/
lisi liuliu liwu tom wangwu zhangsan
[root@localhost ]# su - admin
//su: 警告:无法更改到 /home/admin 目录: 没有那个文件或目录
This account is currently not available.
查看用户账号的信息命令 id
id //查看用户的帐号属性信息
-u //查看UID
-g //查看GID
-G //查看Groups
[root@localhost ~]# id zhangsan
uid=1010(zhangsan) gid=1010(zhangsan) 组=1010(zhangsan)
[root@localhost ]# cd /etc/default/
[root@localhost default]# ls
grub nss useradd
[root@localhost default]# vi useradd
[root@localhost default]# vi /etc/login.defs //默认配置
[root@localhost default]# id
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@localhost default]# id -u
0
[root@localhost default]# id -g
0
[root@localhost default]# id -G
0
[root@localhost default]# id lisi
uid=1012(lisi) gid=1012(lisi) 组=1012(lisi),1011(tom)
[root@localhost default]# id -G lisi
1012 1011
[root@localhost default]# id -g lisi
1012
[root@localhost default]# id -u lisi
1012
[root@localhost default]#
用户删除命令 userdel
//语法:userdel [option] USERNAME
-r //删除用户的同时删除其家目录(userdel默认不会删除其家目录)
[root@localhost default]# userdel tom
[root@localhost default]# userdel tom
//userdel:组“tom”没有移除,因为它包含其它成员。
[root@localhost default]# id tom
id: tom: no such user
[root@localhost default]# ls /home
lisi liuliu liwu tom wangwu zhangsan
[root@localhost default]# ls /home/tom
[root@localhost default]# ls /home/tom -a
. .. .bash_logout .bash_profile .bashrc
[root@localhost default]# ll /home/
总用量 0
drwx------. 2 lisi lisi 62 11月 9 21:26 lisi
drwx------. 2 liuliu tom 62 11月 9 21:31 liuliu
drwx------. 2 liwu liwu 83 11月 9 21:49 liwu
drwx------. 2 1011 tom 62 11月 9 21:25 tom
drwx------. 2 wangwu wangwu 62 11月 9 21:29 wangwu
drwx------. 2 zhangsan zhangsan 62 11月 9 21:20 zhangsan
[root@localhost default]# id 1011
id: 1011: no such user
[root@localhost default]# useradd -u 1011 shitou
//指定shitou的uid为1011
[root@localhost default]# id shitou
uid=1011(shitou) gid=1025(shitou) 组=1025(shitou)
[root@localhost default]# ll /home/
总用量 0
drwx------. 2 lisi lisi 62 11月 9 21:26 lisi
drwx------. 2 liuliu tom 62 11月 9 21:31 liuliu
drwx------. 2 liwu liwu 83 11月 9 21:49 liwu
drwx------. 2 shitou shitou 62 11月 9 22:13 shitou
drwx------. 2 shitou tom 62 11月 9 21:25 tom
drwx------. 2 wangwu wangwu 62 11月 9 21:29 wangwu
drwx------. 2 zhangsan zhangsan 62 11月 9 21:20 zhangsan
[root@localhost default]#
-r:删除用户的同时删除其家目录(userdel默认不会删除其家目录)
[root@localhost default]# ls /home/
lisi liuliu liwu shitou tom wangwu zhangsan
[root@localhost default]# userdel -r shitou
[root@localhost default]# ls -l /home/
总用量 0
drwx------. 2 lisi lisi 62 11月 9 21:26 lisi
drwx------. 2 liuliu tom 62 11月 9 21:31 liuliu
drwx------. 2 liwu liwu 83 11月 9 21:49 liwu
drwx------. 2 1011 tom 62 11月 9 21:25 tom
drwx------. 2 wangwu wangwu 62 11月 9 21:29 wangwu
drwx------. 2 zhangsan zhangsan 62 11月 9 21:20 zhangsan
修改账户属性的命令 usermod
//usermod 修改用户属性
//语法:usermod [options] username
-u UID
-g GID
-a -G groupname //不使用-a选项,会覆盖此前的附加组
-d -m //改变用户家目录的同时把原来家目录的文件移动到新的家目录中
-e YYYY-MM-DD //指明用户帐号过期日期
-f INACTIVE //设定非活动期限
-L //锁定帐号。
//被锁定的帐号在/etc/shadow文件中密码前面会有一个!感叹号
-U //解锁帐号
//chsh 修改用户的默认shell
//语法:chsh [options] [username]
-s SHELL
[root@localhost default]# id lisi
uid=1012(lisi) gid=1012(lisi) 组=1012(lisi),1011(tom)
[root@localhost default]# usermod -u 1011 lisi
[root@localhost default]# id lisi
uid=1011(lisi) gid=1012(lisi) 组=1012(lisi),1011(tom)
[root@localhost default]# ll /home/
总用量 0
drwx------. 2 lisi lisi 62 11月 9 21:26 lisi
drwx------. 2 liuliu tom 62 11月 9 21:31 liuliu
drwx------. 2 liwu liwu 83 11月 9 21:49 liwu
drwx------. 2 lisi tom 62 11月 9 21:25 tom
drwx------. 2 wangwu wangwu 62 11月 9 21:29 wangwu
drwx------. 2 zhangsan zhangsan 62 11月 9 21:20 zhangsan
[root@localhost default]# usermod -g 1005 lisi
[root@localhost default]# id lisi
uid=1011(lisi) gid=1005(openstack6) 组=1005(openstack6),1011(tom)
[root@localhost default]# cd
[root@localhost ~]# id liuliu
uid=1021(liuliu) gid=1011(tom) 组=1011(tom),1010(zhangsan)
[root@localhost ~]# usermod -G 1021 lisi
//usermod:“1021”组不存在
//改变属组时组必须存在
[root@localhost ~]# id liwu
uid=1024(liwu) gid=1024(liwu) 组=1024(liwu)
[root@localhost ~]# usermod -G 1024 lisi
[root@localhost ~]# tail -1 /etc/group
admin:x:995:
[root@localhost ~]# id lisi
uid=1011(lisi) gid=1005(openstack6) 组=1005(openstack6),1024(liwu)
[root@localhost ~]# usermod -G admin lisi
//把lisi加入附加组admin
[root@localhost ~]# tail -2 /etc/group
liwu:x:1024:
admin:x:995:lisi
[root@localhost ~]# usermod -G liwu lisi
[root@localhost ~]# tail -2 /etc/group
liwu:x:1024:lisi
admin:x:995:
//不使用-a选项,会覆盖此前的附加组
[root@localhost ~]# id lisi
uid=1011(lisi) gid=1005(openstack6) 组=1005(openstack6),1024(liwu)
[root@localhost ~]# usermod -a -G admin lisi
//-a -G groupname 不使用-a选项,会覆盖此前的附加组
[root@localhost ~]# tail -2 /etc/group
liwu:x:1024:lisi
admin:x:995:lisi
[root@localhost ~]# id lisi
uid=1011(lisi) gid=1005(openstack6) 组=1005(openstack6),1024(liwu),995(admin)
[root@localhost ~]# ls /home/
lisi liuliu liwu tom wangwu zhangsan
[root@localhost ~]# touch /home/zhangsan/abc
[root@localhost ~]# ls /home/zhangsan/
abc
[root@localhost ~]# ls /opt/
chengdong chengxi
[root@localhost ~]# usermod -d /opt/yyjj -m zhangsan
[root@localhost ~]# ls /home/
lisi liuliu liwu tom wangwu
[root@localhost ~]# ls /opt/
chengdong chengxi yyjj
[root@localhost ~]# ls /opt/yyjj
abc
[root@localhost ~]# ll /opt/
总用量 0
drwx------. 2 chengdong chengdong 83 11月 9 21:37 chengdong
drwxr-xr-x. 2 root root 6 11月 9 21:39 chengxi
drwx------. 2 zhangsan zhangsan 73 11月 9 22:46 yyjj
[root@localhost ~]# su - zhangsan -c 'touch aaa'
// -c "COMMENT" :注释信息
[root@localhost ~]# ll /opt/
总用量 0
drwx------. 2 chengdong chengdong 83 11月 9 21:37 chengdong
drwxr-xr-x. 2 root root 6 11月 9 21:39 chengxi
drwx------. 2 zhangsan zhangsan 84 11月 9 23:14 yyjj
[root@localhost ~]# ll /opt/yyjj
总用量 0
-rw-rw-r--. 1 zhangsan zhangsan 0 11月 9 23:14 aaa
-rw-r--r--. 1 root root 0 11月 9 22:46 abc
[root@localhost ~]# cd /home/tom;su zhangsan -c 'touch aaa'
touch: 无法创建"aaa": 权限不够
[root@localhost tom]# ll /home
总用量 0
drwx------. 2 lisi openstack6 62 11月 9 21:26 lisi
drwx------. 2 liuliu tom 62 11月 9 21:31 liuliu
drwx------. 2 liwu liwu 83 11月 9 21:49 liwu
drwx------. 2 lisi tom 62 11月 9 21:25 tom
drwx------. 2 wangwu wangwu 62 11月 9 21:29 wangwu
[root@localhost tmp]# su zhangsan -c 'touch 456'
//以张三的身份在当前目录创建一个名为456的文件
[root@localhost tmp]# ll
总用量 0
-rw-r--r--. 1 zhangsan zhangsan 0 11月 9 23:19 456
drwxr-xr-x. 2 root root 25 11月 9 12:39 test
[root@localhost tmp]# cp /etc/passwd .
[root@localhost tmp]# ls
456 passwd test
[root@localhost tmp]# head -3 passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost tmp]# ll /etc/passwd
-rw-r--r--. 1 root root 1698 11月 9 22:48 /etc/passwd
[root@localhost tmp]# tail -3 /etc/passwd
chengxi:x:1023:1023::/opt/chengxi/:/bin/bash
liwu:x:1024:1024::/home/liwu:/bin/sh
admin:x:997:995::/home/admin:/sbin/nologin
[root@localhost tmp]# usermod -c 'I am boss' admin
//-c "COMMENT" 注释信息
[root@localhost tmp]# tail -3 /etc/passwd
chengxi:x:1023:1023::/opt/chengxi/:/bin/bash
liwu:x:1024:1024::/home/liwu:/bin/sh
admin:x:997:995:I am boss:/home/admin:/sbin/nologin