centos 简单用户管理

时间:2023-03-09 16:56:50
centos 简单用户管理

一、配置文件

  • /etc/passwd:存放用户信息,以“:”分割成7个部分

  centos 简单用户管理

    1、账号名称,用来对应UID;

    2、早期密码存放位置,后来密码改存/etc/shadow中,以“x”代替;

    3、UID,使用者表示,系统主要通过UID识别账户

    4、GID,用户组的ID

    5、用户信息说明

    6、家目录

    7、登陆的shell

  • /etc/shadow:存放用户密码等信息,同样以“:”分割,分为9段

  centos 简单用户管理

    1、账号名称,与/etc/passwd相对应;

    2、密码,经过加密;

    3、最近更改密码的日期,以1970-1-1开始计数;

    4、不允许更改密码的天数,从密码变更起始开始计数,如果数值为n,表示密码变更后,n天内不允许在修改密码,0表示随时可以修改密码;

    5、密码需要更改的天数,从密码变更起始开始计数,如果数值为m,表示密码变更后,在m天内需要更改密码,99999表示永远不要修改密码;

    6、密码更改期限前的警告天数,与5相比,如果数值为x,表示在5字段密码需要更改的前x天,发出警告,提示需要修改密码;

    7、密码过期后的宽限时间,与5字段相比,如果数值为y,表示在5字段,密码需要更改后,在y天内密码有效,y天后密码失效,登陆时会强制用户更改密码;

    8、账号失效日期,如果数值为z,表示在z天以后,账号不能登陆;

    9、保留字段

  • /etc/group,存放用户组信息,同样以“:”分割,4个字段

  centos 简单用户管理

    1、组名

    2、组密码

    3、GID

    4、组成员

  • /etc/gshadow 组密码等信息位置
  • /etc/skel
[root@web01 ~]# ll -a /etc/skel/
总用量
drwxr-xr-x.    root root   5月   : .
drwxr-xr-x.  root root  5月   : ..
-rw-r--r--     root root     3月    .bash_logout
-rw-r--r--     root root    3月    .bash_profile
-rw-r--r--     root root    3月    .bashrc
drwxr-xr-x.    root root   11月   .gnome2

二、命令管理

  • 添加用户

  

[root@web01 ~]# useradd -u  test1     #指定uid
[root@web01 ~]# id test1
uid=(test1) gid=(test1) 组=(test1)
[root@web01 ~]# useradd -g root test2    #指定主组
[root@web01 ~]# id test2
uid=(test2) gid=(root) 组=(root)
[root@web01 ~]# useradd -G root test3    #添加新用户到附加组
[root@web01 ~]# id test3
uid=(test3) gid=(test3) 组=(test3),(root)
[root@web01 ~]# useradd -s /sbin/nologin test4  #指定登陆shell
[root@web01 ~]# tail - /etc/passwd
test4:x::::/home/test4:/sbin/nologin-M  不为用户创建家目录-d  为用户指定家目录
  • 设置密码

  

[root@web01 ~]# passwd test1
更改用户 test1 的密码 。
新的 密码:
无效的密码: 它基于字典单词
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@web01 ~]# echo "password"|passwd test2 --stdin  #非交互设置密码,可用于批量添加用户
更改用户 test2 的密码 。
passwd: 所有的身份验证令牌已经成功更新。[root@web01 ~]# passwd -d test2  #清除密码清除用户的密码 test2。passwd: 操作成功
  • 修改用户信息

  

[root@web01 ~]# id test2
uid=(test2) gid=(root) 组=(root)
[root@web01 ~]# usermod -u  test2
[root@web01 ~]# id test2
uid=(test2) gid=(root) 组=(root)
[root@web01 ~]# usermod -g test3 test2
[root@web01 ~]# id test2
uid=(test2) gid=(test3) 组=(test3)
[root@web01 ~]# usermod -G root test2
[root@web01 ~]# id test2
uid=(test2) gid=(test3) 组=(test3),(root)
[root@web01 ~]# grep test2 /etc/passwd
test2:x::::/home/test2:/bin/bash
[root@web01 ~]# usermod -s /sbin/nologin test2
[root@web01 ~]# grep test2 /etc/passwd
test2:x::::/home/test2:/sbin/nologin
##########与useradd用法几乎相同############
  • 删除用户
[root@web01 ~]# userdel test1  #默认不删除家目录
[root@web01 ~]# ll -d /home/test1/
drwx------     5月   : /home/test1/
[root@web01 ~]# userdel -r test4  #删除家目录
[root@web01 ~]# ll -d /home/test4
ls: 无法访问/home/test4: 没有那个文件或目录
  • 查看用户信息
[root@web01 ~]# id
uid=(root) gid=(root) 组=(root)
[root@web01 ~]# id test1
id: test1:无此用户
[root@web01 ~]# id test2
uid=(test2) gid=(root) 组=(root)
  • 组管理
[root@web01 ~]# groupadd test1
[root@web01 ~]# tail - /etc/group
suffergtf:x::
test1:x::
[root@web01 ~]# groupdel test1
[root@web01 ~]# tail - /etc/group
test3:x::
suffergtf:x::

三、sudo管理

配置文件/etc/sudoers,可以使用vim编辑工具直接编辑该文件,但是没有语法检查,所以推荐使用visudo管理

格式如下

root    ALL=(ALL)       ALL

root:表示需要提权的用户

ALL:运行的主机

(ALL):表示以谁的身份来运行命令

ALL:表示给予字段1中的用户使用字段3的用户权限运行这个命令

例:

test    ALL=(root)      /usr/sbin/useradd
[root@web01 test2]# su - test
[test@web01 ~]$ /usr/sbin/useradd test100
-bash: /usr/sbin/useradd: 权限不够
[test@web01 ~]$ sudo /usr/sbin/useradd test100
[sudo] password for test:
[test@web01 ~]$ id test100
uid=(test100) gid=(test100) 组=(test100)

sudo别名功能(别名只能用大写)

User_Alias USER = test200,test300,test400  #定义用户别名Host_Alias HOST = web01,web02    #定义主机别名Cmnd_Alias CMD = /usr/sbin/useradd,/usr/sbin/userdel  #定义命令别名

USER    ALL=(root)      CMD###########别名需要使用大写字母表示,并且定义的别名如果没有使用,会报错#############[root@web01 test2]# su - test200[test200@web01 ~]$ /usr/sbin/useradd test11-bash: /usr/sbin/useradd: 权限不够[test200@web01 ~]$ sudo /usr/sbin/useradd test11[sudo] password for test200: [test200@web01 ~]$ /usr/sbin/userdel test11-bash: /usr/sbin/userdel: 权限不够[test200@web01 ~]$ sudo /usr/sbin/userdel test11###########需要输入密码比较麻烦,可以定义不用输入执行sudo的用户的密码##########USER    ALL=(root)      NOPASSWD:CMD[root@web01 test2]# su - test200[test200@web01 ~]$ sudo /usr/sbin/useradd test22[test200@web01 ~]$ id test22uid=1011(test22) gid=1011(test22) 组=1011(test22)