Linux用户管理

2018-01-06  本文已影响0人  其实我很dou

一. 用户配置文件

用户配置文件主要包括

1. 用户信息文件

用户信息配置文件为/etc/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
...
test:x:502:505::/home/test:/bin/bash
tony:x:503:506::/home/tony:/bin/bash
tom:x:504:507::/home/tom:/bin/bash
lw:x:505:509::/home/lw:/bin/bash
uar:x:506:510::/home/uar:/bin/bash

各字段含义:

2. 密码配置文件(影子文件)

用户密码配置文件为/etc/shadow, 权限为000, 只有root能对其进行修改

root:$6$AZ76b/wn$jw8/5ZnecagBeRrt3j3hLGK5aLlBuoHHH/PRBViVNwG8rvYENmdTMEXpCcEiG8/MlmkGh1ART2S/xnkrzhpbC0:17230:0:99999:7:::
bin:*:15980:0:99999:7:::
.....
uar:$6$ALNMp7A9$EMdZzzClqH0bS4M1z3KpcWNuQvY9Q1Mxivu3DF3S68il0pkxvPqEsiHZnvyV235Eetd6LY3ob9PFCYV1V9kEv.:17525:10:20:7:5::

各字段含义:

3. 用户组配置文件

用户组配置文件为/etc/group

root:x:0:
bin:x:1:bin,daemon
..
uar:x:510:

各字段含义

4. 用户管理相关文件

# 添加用户生成, 生成文件模板目录为/etc/skel
-rw-------  1 uar  uar    78 12月 26 23:10 .bash_history
-rw-r--r--  1 uar  uar    18 7月  18 2013 .bash_logout
-rw-r--r--  1 uar  uar   176 7月  18 2013 .bash_profile
-rw-r--r--  1 uar  uar   124 7月  18 2013 .bashrc
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2

二. 用户管理命令

1. 用户添加命令useradd

执行useradd -u 666 -d /hello -c "helloworld" -G root,uar -s /bin/bash hello

[root@localhost ~]# ll -d /hello/
drwx------ 3 hello hello 4096 1月   6 10:18 /hello/

[root@localhost ~]# grep hello /etc/passwd
hello:x:666:666:helloworld:/hello:/bin/bash

[root@localhost ~]# grep hello /etc/group
root:x:0:hello #hello用户的扩展组
uar:x:510:hello
hello:x:666:

[root@localhost ~]# grep hello /etc/shadow
hello:!!:17537:0:99999:7:::

2. 设定密码passwd

格式 passwd [选项] [用户名]

示例:

[root@localhost ~]# passwd -S hello
hello PS 2018-01-06 0 99999 7 -1 (密码已设置,使用 SHA512 加密。)

[root@localhost ~]# passwd -l hello
锁定用户 hello 的密码 。
passwd: 操作成功

#锁定密码, 其实就是在密码前加入 '!!'
[root@localhost ~]# grep hello /etc/shadow
hello:!!$6$hYvSf3tE$66xZYlCTuTLdGa7RtFXZUVfx1GOgseUSwbmb1sTa3uzgPa.a5Iv6RhJCM0zkUlqE3PwvBoeYoFVv5Q4Ln4JA61:17537:0:99999:7:::

#解锁用户, 就是移除密码前的'!!'
[root@localhost ~]# passwd -u hello
解锁用户 hello 的密码 。
passwd: 操作成功

[root@localhost ~]# grep hello /etc/shadow
hello:$6$hYvSf3tE$66xZYlCTuTLdGa7RtFXZUVfx1GOgseUSwbmb1sTa3uzgPa.a5Iv6RhJCM0zkUlqE3PwvBoeYoFVv5Q4Ln4JA61:17537:0:99999:7:::

# 使用--stdin设定密码
[root@localhost ~]# echo 1234 | passwd --stdin hello
更改用户 hello 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
[root@localhost ~]# grep hello /etc/shadow
hello:$6$WqbpYyC8$Er8ytu9jGFvXplyBfim32SRMb.e8EspoBJKRdiH5sl8VhsuEBjt8ZmQGtIsupU.5hh42SNdbNvssUCv.Z75A00:17537:0:99999:7:::

3. 修改用户信息 usermod

useradd是添加新用户, usermod是修改已经存在的用户

格式 usermod [选项] 用户名

4. 修改用户密码状态 chage

格式 chage [选项] 用户名

示例:
chage -d 0 hello
这个命令时把密码日期归0为(shadow第三字段), 用户已登录, 就要提示其修改密码, 在shell脚本中, 可以给很多用户设定一个初始密码, 然后将其密码修改日期归档为0, 用户登陆后, 必须修改密码

[root@localhost ~]# chage -d 0 hello

# 将其第三字段改为0
[root@localhost ~]# grep hello /etc/shadow
hello:$6$WqbpYyC8$Er8ytu9jGFvXplyBfim32SRMb.e8EspoBJKRdiH5sl8VhsuEBjt8ZmQGtIsupU.5hh42SNdbNvssUCv.Z75A00:0:0:99999:7:::
[root@localhost ~]# su - uar

[uar@localhost ~]$ su - hello
密码:
您需要立即更改密码(root 强制)
为 hello 更改 STRESS 密码。
(当前)UNIX 密码:
新的 密码:

5 删除用户

userdel [选项] 用户名

[hello@localhost ~]$ id
uid=666(hello) gid=666(hello) 组=666(hello),0(root),510(uar)

6 切换用户身份

格式: su [选项] 用户名

三. 用户组管理

1.添加用户组

groupadd [选项] 组名

2. 修改用户组

groupmod [选项] 组名

3. 删除组

groupdel 组名

[root@localhost ~]# groupadd gtest
[root@localhost ~]# grep gtest /etc/group
gtest:x:511:

[root@localhost ~]# useradd -g gtest anny
[root@localhost ~]# grep anny /etc/passwd
anny:x:507:511::/home/anny:/bin/bash

[root@localhost ~]# useradd -G gtest jack
[root@localhost ~]# grep gtest /etc/group
# 会将jack放在gtest附加组里
gtest:x:511:jack

# 如果有初始用户, 则不能删除
[root@localhost ~]# groupdel gtest
groupdel: cannot remove the primary group of user 'anny'

[root@localhost ~]# userdel jack
[root@localhost ~]# grep gtest /etc/group
gtest:x:511:
[root@localhost ~]# userdel anny

# 删除anny用户后, 再删除gtest组, 和jack是否存在没关系
[root@localhost ~]# groupdel gtest

4. 把用户添加入组或者从组中删除

gpasswd 选项 组名

[root@localhost ~]# gpasswd -a lw,uar gtest
gpasswd: user 'lw,uar' does not exist

[root@localhost ~]# gpasswd -a lw gtest
Adding user lw to group gtest

[root@localhost ~]# gpasswd -a uar gtest
Adding user uar to group gtest

[root@localhost ~]# grep gtest /etc/group
gtest:x:511:lw,uar

[root@localhost ~]# gpasswd -d lw gtest
Removing user lw from group gtest

[root@localhost ~]# gpasswd -d uar gtest
Removing user uar from group gtest

[root@localhost ~]# grep gtest /etc/group
gtest:x:511:

上一篇 下一篇

猜你喜欢

热点阅读