您的位置:首页 > 博客中心 > 网络系统 >

Linux_用户和组管理

时间:2022-04-03 16:25

用户和组管理


用户管理

  1. useradd //用户创建命令;创建一个叫linux001的用户(不能用纯数字创建)
[root@lc ~]# useradd linux001
[root@lc ~]# id linux001
uid=1000(linux001) gid=1000(linux001) groups=1000(linux001)
[root@lc ~]# 
  1. ll //列出当前文件或目录的详细信息,含有时间、读写权限、大小、时间等信息;判断用户是否是某个文件的属主
  • r //表示读取权限
  • w //表示写入权限
  • x //表示执行权限
  • ‘-’ //表示无此权限
[root@lc ~]# ll
total 12
-rw-r--r--. 1 root root 12 Nov  8 18:15 abc
-rw-r--r--. 1 root root  5 Nov  8 18:51 baozi
-rw-r--r--. 1 root root  5 Nov  8 18:16 def
  1. 进程所能够访问的所有资源的权限取决于进程的发起者的身份 //在root 和 linux001 里编辑abc文件,查看进程
[root@lc ~]# ps -ef|grep vi
root         966       1  0 12:00 ?        00:00:00 /usr/bin/VGAuthService -s
root       15011   14889  0 19:30 pts/0    00:00:00 vi abc
root       15013   14936  0 19:30 pts/1    00:00:00 grep --color=auto vi
[root@lc ~]# ps -ef|grep vi
root         966       1  0 12:00 ?        00:00:00 /usr/bin/VGAuthService -s
root       15011   14889  0 19:30 pts/0    00:00:00 vi abc
linux001   15015   14985  0 19:31 pts/2    00:00:00 vim abc
root       15017   14936  0 19:31 pts/1    00:00:00 grep --color=auto vi
[root@lc ~]# 
  1. -u UID //[UID_MIN,UID_MAX]定义在/etc/login.defs文件中
[root@lc ~]# useradd -u 2000 linux002
[root@lc ~]# id linux002
uid=2000(linux002) gid=2000(linux002) groups=2000(linux002)
[root@lc ~]# 
  1. -g GID //指定用户所属基本组,可为组名或GID
[root@lc ~]# useradd -g linux001 linux003
[root@lc ~]# id linux003
id=2001(linux003) gid=1000(linux001) groups=1000(linux001)
[root@lc ~]# 
  1. -G groupname,... //附加组,可以有多个,用逗号隔开。组groupname必须事先存在
[root@lc ~]# useradd -g linux001 -G linux002 linux004
[root@lc ~]# id linux004
uid=2002(linux004) gid=1000(linux001) groups=1000(linux001),2000(linux002)
[root@lc ~]# 

7.-c //“COMMENT”,注释信息

[root@lc ~]# useradd -c "我是中国人,何必学外文,什么ABC,都是狗屁文" linux006
[root@lc ~]# id linux006
uid=2003(linux006) gid=2003(linux006) groups=2003(linux006)
[root@lc ~]# grep linux006 /etc/passwd
linux006:x:2003:2003:我是中国人,何必学外文,什么ABC,都是狗屁文:/home/linux006:/bin/bash
[root@lc ~]# 

8.-d //指定用户的家目录,此目录必须不能事先存在,否则将不会从etc/skel中复制环境文件

[root@lc ~]# ls /opt/
anaconda-ks.cfg
[root@lc ~]# useradd -d /opt/007 linux007
[root@lc ~]# ls /opt/
007  anaconda-ks.cfg

9.-s //指定的shell登录(组合使用)

  • M //创建用户时不给其创建家目录
  • -r //添加一个系统用户
[root@lc ~]# useradd -r -M -s /sbin/nologin linux008
[root@lc ~]# id linux008
uid=994(linux008) gid=991(linux008) groups=991(linux008)

查看用户账号的信息命令 //id

  1. id //当前登录账户的信息
[root@lc ~]# id linux008
uid=994(linux008) gid=991(linux008) groups=991(linux008)
[root@lc ~]# 
  1. id -u //查看UID
[root@lc ~]# id -u linux008
994
[root@lc ~]#
  1. id -g //查看GID
[root@lc ~]# id -g linux008
991
[root@lc ~]#
  1. id -G //查看Groups
[root@lc ~]# id -G linux008
991
[root@lc ~]#   

删除用户;-r 删除用户的同时删除其家目录(userdel默认不会删除其家目录) //userdel

[root@lc ~]# ll /home/
total 0
drwx------. 2 linux001 linux001 99 Nov  9 20:22 linux001
drwx------. 2 linux002 linux002 62 Nov  9 19:42 linux002
drwx------. 2 linux003 linux001 62 Nov  9 19:44 linux003
drwx------. 2 linux004 linux001 62 Nov  9 20:19 linux004
[root@lc ~]# userdel -r linux004
[root@lc ~]# ll /home/
total 0
drwx------. 2 linux001 linux001 99 Nov  9 20:22 linux001
drwx------. 2 linux002 linux002 62 Nov  9 19:42 linux002
drwx------. 2 linux003 linux001 62 Nov  9 19:44 linux003
[root@lc ~]# 

修改用户属性 //usermod

  1. -u //修改UID
[root@lc ~]# id linux001
uid=1000(linux001) gid=1000(linux001) groups=1000(linux001)
[root@lc ~]# usermod -u 2000 linux001
usermod: UID ‘2000‘ already exists
[root@lc ~]# usermod -u 3000 linux001
[root@lc ~]# id linux001
uid=3000(linux001) gid=1000(linux001) groups=1000(linux001)
  1. -g //修改GID;组必须要存在;下面是把linux001的GID变成了linux002的GID(组名就是GID)
[root@lc ~]# id linux002
uid=2000(linux002) gid=2000(linux002) groups=2000(linux002)
[root@lc ~]# usermod -g linux002 linux001
[root@lc ~]# id linux001
uid=3000(linux001) gid=2000(linux002) groups=2000(linux002)
[root@lc ~]# 
  1. -a -G groupname //不使用-a选项,会覆盖此前的附加组
[root@lc ~]# usermod -a -G lc linux004
[root@lc ~]# id linux004
uid=3001(linux004) gid=3001(linux004) groups=3001(linux004),3002(lc)
[root@lc ~]# 
  1. -d -m //-d后面必须加家目录;改变用户家目录的同时把原来家目录的文件移动到新的家目录;下面是把linux004搬到opt下面,名叫004
[root@lc ~]# usermod -m -d /opt/004 linux004
[root@lc ~]# ll /home/
total 0
drwx------. 2 lc       lc       62 Nov 10 01:12 lc
drwx------. 2 linux001 linux002 99 Nov  9 20:22 linux001
drwx------. 2 linux002 linux002 62 Nov  9 19:42 linux002
drwx------. 2 linux003 linux001 62 Nov  9 19:44 linux003
[root@lc ~]# ls /opt/
004  007  anaconda-ks.cfg
[root@lc ~]# 
  • -L //锁定账户
  • -U //解锁账户
[root@lc ~]# usermod -L lc
[root@lc ~]# usermod -U lc
  1. -s shell //修改用户的默认shell
[root@lc ~]# usermod -s /bin/sh linux008
[root@lc ~]# grep linux008 /etc/passwd
linux008:x:994:991::/home/linux008:/bin/sh
[root@lc ~]# 

切换用户命令 //su

  1. su username //非登录切换,既不会读取目标用户的配置文件
[root@lc ~]# su lc
[lc@lc root]$ 
  1. su - username //登陆式切换,读取目标用户配置文件
[lc@lc root]$ su - root
Password: 
Last login: Tue Nov 10 00:06:06 CST 2020 from 192.168.91.1 on pts/2
[root@lc ~]# 
  1. su - //不指定用户切换root
[root@lc ~]# su lc
[lc@lc root]$ su -
Password: 
Last login: Tue Nov 10 01:33:47 CST 2020 on pts/2
[root@lc ~]# 
  1. -c command //切换身份执行命令结束后回到原来身份
[lc@lc root]$ su -root -c "ip a"
su: invalid option -- ‘r‘
Try ‘su --help‘ for more information.
[lc@lc root]$ su - root -c "ip a"
Password: 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:40:37:5a brd ff:ff:ff:ff:ff:ff
    inet 192.168.91.131/24 brd 192.168.91.255 scope global dynamic noprefixroute ens160
       valid_lft 1385sec preferred_lft 1385sec
    inet6 fe80::3c7:210b:e904:d05b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[lc@lc root]$ 

密码管理

密码管理命令 //passwd

  1. passwd //给当前用户设置密码;当前是管理员账户所以可以直接管理密码
[root@runtime ~]# passwd
Changing password for user root.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@runtime ~]# 
  1. 在管理员账户下给别的用户设置密码
  • passwd tom //给tom设置密码
[root@runtime ~]# passwd tom
Changing password for user tom.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
Sorry, passwords do not match.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@runtime ~]#
  • echo ‘***’ |passwd --stdin tom //把密码给passwd命令给标准输入的tom用户(把密码重定向给tom;标准输入的重定向),这种方式会留下history,history -d 删除
[root@runtime ~]# echo ‘redhat‘|passwd --stdin tom
Changing password for user tom.
passwd: all authentication tokens updated successfully.
[root@runtime ~]# 
  1. -l //lock;锁定用户

  2. -u //unlock;解锁用户


密码生成工具 //openssl

  • command//包含标准命令、消息摘要命令、加密命令
  • version //查看程序版本号
  • dgst //提取特征码
  • passwd //生成密码
  • rand//生成伪随机数
  1. openssl degt -md5 //生成一个随机密码用md5加密算法
[tom@runtime ~]$ touch abc
[tom@runtime ~]$ openssl dgst -md5 abc
MD5(abc)= d41d8cd98f00b204e9800998ecf8427e
  1. openssl rand -base64 20 //生成一个20位的随机密码;
  [root@runtime ~]# openssl rand -base64 20
wZei4FsrJlink9xah6xWX5bPGF4=
[root@runtime ~]# openssl rand -base64 30
lA1M1Jri3c6Skr56aGA8t84eNjZ7ghZU6ScNhmv9

组管理

创建组命令 //groupadd

  • -g GID //指定GID

  • -r //添加一个系统组

    1. -r //创建一个组
[root@runtime ~]# groupadd -r mysys
[root@runtime ~]# grep mysys /etc/group
mysys:x:990:
[root@runtime ~]# 
  1. -g//指定一个GID
[root@runtime ~]# groupadd -g 6000 mysys1
[root@runtime ~]# grep mysys1 /etc/group
mysys1:x:6000:
[root@runtime ~]# 

删除组命令 //groupdel

  • groupdel //删除组
[root@runtime ~]# groupdel mysys
[root@runtime ~]# groupdel mysys1
[root@runtime ~]# grep mysys1 /etc/group
[root@runtime ~]# grep mysys /etc/group
[root@runtime ~]# 

各配置文件说明

配置文件 作用
/etc/passwd 用户及其属性信息(名称、uid、基本组id等等)
/etc/group 组及其属性信息
/etc/shadow 用户密码及其相关属性
/etc/gshadow 组密码及其相关属性。在用户执行基本组切换时使用

配置文件 /etc/passwd /etc/group
第一字段 用户名 组名
第二字段 密码占位符 组密码
第三字段 UID GID
第四字段 GID 以当前组为附加组的用户列表(分隔符为逗号)
第五字段 用户的描述信息
第六字段 用户家目录
第七字段 用户的登录shell

配置文件 /etc/shadow
第一字段 登录名
第二字段 加密后的密码
第三字段 最近一次更改密码的日期
第四字段 密码的最小使用期限
第五字段 密码的最大使用期限
第六字段 密码警告时间段
第七字段 密码禁用期
第八字段 帐号的过期日期
第九字段 保留字段

本类排行

今日推荐

热门手游