其他分享
首页 > 其他分享> > 用户和组操作

用户和组操作

作者:互联网

添加用户
[root@localhost ~]# useradd juliet
[root@localhost ~]# useradd romeo
[root@localhost ~]# useradd hamlet
[root@localhost ~]# useradd reba
[root@localhost ~]# useradd dolly
[root@localhost ~]# useradd elvis
[root@localhost ~]# tail -n 10 /etc/passwd
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zhou:x:1000:1000:zhou:/home/zhou:/bin/bash
jyy:x:6000:6000::/jyy:/bin/bash
juliet:x:6001:6001::/home/juliet:/bin/bash
romeo:x:6002:6002::/home/romeo:/bin/bash
hamlet:x:6003:6003::/home/hamlet:/bin/bash
reba:x:6004:6004::/home/reba:/bin/bash
dolly:x:6005:6005::/home/dolly:/bin/bash
elvis:x:6006:6006::/home/elvis:/bin/bash

添加用户组
[root@localhost ~]# groupadd -g 30000 shakespeare
[root@localhost ~]# groupadd artists
[root@localhost ~]# tail -n 2 /etc/group
shakespeare:x:30000:
artists:x:30001:

修改用户附加组
[root@localhost ~]# usermod -G shakespeare juliet
[root@localhost ~]# usermod -G shakespeare romeo
[root@localhost ~]# usermod -G shakespeare hamlet
[root@localhost ~]# tail -n 8 /etc/group
juliet:x:6001:
romeo:x:6002:
hamlet:x:6003:
reba:x:6004:
dolly:x:6005:
elvis:x:6006:
shakespeare:x:30000:juliet,romeo,hamlet
artists:x:30001:
[root@localhost ~]# usermod -G artists reba
[root@localhost ~]# usermod -G artists dolly
[root@localhost ~]# usermod -G artists elvis
[root@localhost ~]# tail -n 3 /etc/group
elvis:x:6006:
shakespeare:x:30000:juliet,romeo,hamlet
artists:x:30001:reba,dolly,elvis

同时加入多个附加组
[root@localhost ~]# usermod -G shakespeare,artists romeo
如果用户已有附加组再新加一个附加组,加-a 参数配合-G ,否则之前的组会被替换成新的组
[root@localhost ~]# usermod -G artists romeo -a
[root@localhost ~]# tail -n 2 /etc/group
shakespeare:x:30000:juliet,hamlet,romeo
artists:x:30001:reba,dolly,elvis,romeo

练习二
添加并指定用户组id,添加一个新用户并指定刚才的组为附加组
[root@localhost ~]# groupadd adminuser -g 40000
[root@localhost ~]# useradd natasha -G adminuser
[root@localhost ~]# tail -n 2 /etc/group
adminuser:x:40000:natasha
natasha:x:6007:

创建一个用户并指定其不能登录shell
[root@localhost ~]# useradd sarah -G adminuser
[root@localhost ~]# usermod -s /bin/false
[root@localhost ~]# usermod -s /bin/false sarah
[root@localhost ~]# su - sarah
[root@localhost ~]#
[root@localhost ~]# usermod -s /usr/sbin/nologin sarah
[root@localhost ~]# su - sarah
This account is currently not available.
[root@localhost ~]# tail -1 /etc/passwd
sarah:x:6009:6009::/home/sarah:/usr/sbin/nologin

修改用户密码
[root@localhost ~]# echo gelgunge | passwd --stdin harry
Changing password for user harry.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# echo gelgunge | passwd --stdin natasha
Changing password for user natasha.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# echo gelgunge | passwd --stdin sarah
Changing password for user sarah.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# su - natasha
[natasha@localhost ~]$ su - harry
Password:
su: Authentication failure
[natasha@localhost ~]$ su - harry
Password:
[harry@localhost ~]$

标签:bin,root,用户,romeo,home,操作,usermod,localhost
来源: https://blog.51cto.com/2738556/2486658