了解最新公司動態(tài)及行業(yè)資訊
linux命令-用戶管理
用戶切換
[root@eric ~]# su eric # 切換到用戶eric
[eric@eric root]$
[eric@eric root]$ su # 切換到root
Password:
[root@eric ~]#
[root@eric ~]# exit # 退出當前用戶
exit
[eric@eric root]$
usermod ye02 -G wheel # 用戶組切換
用戶信息存儲位置
# /etc/passwd # 存儲Linux中的?戶信息
# /etc/shadow # 存儲Linux中的?戶密碼信息
# /etc/group # 存儲Linux中的?戶組信息
# /etc/gshadow # 存儲Linux中的?戶組密碼信息
passwd文件解讀
useradd zhangsan
more /etc/passwd
zhangsan:x:1002:1002::/home/zhangsan:/bin/bash
[1]:[2]:[3]:[4]:[5]:[6]:[7]
[1] : ?戶名
[2] : ?戶密碼 | X - 密碼單獨存儲在/etc/shadow
[3] : ?戶ID
[4] : 組ID group-id (GID) # 主組
[5] : ?戶描述信息
[6] : 該?戶的家?錄
[7] : ?戶所使?的默認shell殼-類型
shell類型
#查看當前系統(tǒng)中?持的linux shell類型
[root@centos7 ~]# cat /etc/shells
/bin/sh #最古?
/bin/bash #最常?/最常?
/sbin/nologin # ?戶禁?登錄
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
#查看當前環(huán)境正在使?shell
[root@centos7 ~]# echo $SHELL
/bin/bash
#臨時shell切換
[root@centos7 ~]# sh
sh-4.2#
sh-4.2# bash
#永久切換(賬戶退出重新登錄后?效)
[root@centos7 tom]# chsh -s /bin/sh
Changing shell for root.
Shell changed.
用戶操作
沒有指定uid和gid,系統(tǒng)默認從1001開始,自動創(chuàng)建命令home/用戶,自動使用bash
useradd zhangsan
useradd -u 2001 lisi # 指定uid
useradd -u 2002 -c "description" user02 #添加描述
useradd -u 2003 -s /bin/csh user03 #指定shell
useradd user04 -u 2004 -d /home/user04 # 指定家目錄
useradd user05 -u 2005 -g 2004 # 指定用戶組(用戶組必須已經(jīng)存在)
useradd user07 -u 2007 -g 2004 -G 2003 # 指定擴展用戶組(擴展用戶組必須已經(jīng)存在)
[root@eric home]# cat /etc/passwd
user07:x:2007:2004::/home/user07:/bin/bash
[root@eric home]# cat /etc/group
user03:x:2003:user07
[root@eric home]# id user07
uid=2007(user07) gid=2004(user04) groups=2004(user04),2003(user03)
[user07@eric home]$ cd user07 #切換到用戶07
[user07@eric ~]$ touch 128.txt
-rw-r--r--. 1 user07 user04 0 Jul 31 18:46 128.txt 默認創(chuàng)建文件在user04組
[user07@eric ~]$ newgrp user03 #切戶用戶組到03。(可以采用命令exit退出該用戶組03)
[user07@eric ~]$ touch 456.txt
-rw-r--r--. 1 user07 user03 0 Jul 31 18:50 456.txt 創(chuàng)建文件的所屬組在03
userdel user08 #刪除用戶,用戶組沒有其他成員的話,也會相應(yīng)刪除用戶組。
userdel -r user05 # 刪除用戶及對應(yīng)的信息
userdel -rf user05 # 強制刪除用戶及對應(yīng)的信息
/etc/shadow文件解讀
user01:$6$bfcEHLZS$rfpTnTE8vogY0cWFRJqks9qdwsB0vukkCiWL7Hj/Xwe0p0LjkpEYUIDGA1XW
QcwxA8BsLd5P2m2lG3L/avZJd/:20615:0:99999:7:::
user02:!!:20615:0:99999:7:::
[1]:[2]:[3]:[4]:[5]:[6]:[7]:[8]:[9]
[1] : ?戶名
[2] : 密碼 | “!!” 賬號鎖定 / $6$ 密碼已經(jīng)設(shè)置,并且密碼本地加密。
/etc/logins.defs
# Use SHA512 to encrypt password.
ENCRYPT_METHOD SHA512 (哈希法)
[3] : 上次修改密碼時間 (從1970-1-1 (unix)到 修改密碼當天)
[4] : 密碼的最短有效期 (3) | 0 【不設(shè)置最短有效期】
[5] : 密碼的最?有效期 (30)| 99999
[6] : 密碼到期前多久發(fā)送告警 - 7 提前7天發(fā)送警告
[7] : 密碼過期后的寬限?期 - 8 再寬限8天
[8] : 賬號的失效?期 ------優(yōu)先級最?, ?旦當前參數(shù)到期,該賬號直接?即?法使?。 (從
1970-1-1 (unix)到 修改密碼當天)
[9] : 保留
用戶密碼管理
[root@eric ~]# passwd --help
Usage: passwd [OPTION...]
-k, --keep-tokens keep non-expired authentication tokens
-d, --delete delete the password for the named account (root only)
-l, --lock lock the password for the named account (root only)
-u, --unlock unlock the password for the named account (root only)
-e, --expire expire the password for the named account (root only)
-f, --force force operation
-x, --maximum=DAYS maximum password lifetime (root only)
-n, --minimum=DAYS minimum password lifetime (root only)
-w, --warning=DAYS number of days warning users receives before password
expiration (root only)
-i, --inactive=DAYS number of days after password expiration when an account
becomes disabled (root only)
-S, --status report password status on the named account (root only)
--stdin read new tokens from stdin (root only)
Help options:
-?, --help Show this help message
--usage Display brief usage message
root賬號可直接修改密碼。用戶修改自己的賬號需要提交舊密碼,并有大小寫等要求。
root@eric user07]# passwd user01
[root@eric ~]# passwd -S user01 # 查看用戶密碼情況
user01 PS 2023-07-31 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@eric ~]# passwd -l user02 # 鎖定用戶user02
Locking password for user user02.
passwd: Success
[root@eric ~]# passwd -u user02 # 解除鎖定用戶user02
chage -E 2023-8-1 user01 #強制用戶的實效時間。
chage -d 0 user01 # 下次登錄時候強制修改密碼
passwd -e user01 # 下次登錄時候強制修改密碼
passwd -d user02 # 刪除用戶密碼
echo "123" | passwd --stdin user01 通過管道方式修改密碼
passwd -n 3 -x 30 -w 5 -i 2 user01 #最短3天最長30天提前5天警告寬限2天
用戶組管理
#?戶組信息
cat /etc/group
user02:x:2002:user01
[1]:[2]:[3]:[4]
[1]: ?戶組組名
[2]: ?戶組密碼信息
[3]: ?戶組ID
[4]: 附加?戶組成員
groupadd wangwu # 新增用戶組
[root@eric ~]# cat /etc/group
wangwu:x:2009:
groupdel wangwu2 # 刪除用戶組
groupadd -g 3001 wangwu2 # 指定id創(chuàng)建組
usermod命令
[root@eric ~]# usermod --help
Usage: usermod [options] LOGIN
Options:
-c, --comment COMMENT new value of the GECOS field
-d, --home HOME_DIR new home directory for the user account
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP as new primary group
-G, --groups GROUPS new list of supplementary GROUPS
-a, --append append the user to the supplemental GROUPS
mentioned by the -G option without removing
him/her from other groups
-h, --help display this help message and exit
-l, --login NEW_LOGIN new value of the login name
-L, --lock lock the user account
-m, --move-home move contents of the home directory to the
new location (use only with -d)
-o, --non-unique allow using duplicate (non-unique) UID
-p, --password PASSWORD use encrypted password for the new password
-R, --root CHROOT_DIR directory to chroot into
-s, --shell SHELL new login shell for the user account
-u, --uid UID new UID for the user account
-U, --unlock unlock the user account
-Z, --selinux-user SEUSER new SELinux user mapping for the user account
usermod user01 -g 2003 # 已經(jīng)創(chuàng)建的用戶需要通過usermod修改組,-g修改默認值
usermod user01 -G 2004 # -G 覆蓋模式,增加或者修改擴展組
usermod user01 -aG 2002 # -aG 追加模式,增加擴展組
gpasswd命令
[root@eric ~]# gpasswd --help
Usage: gpasswd [option] GROUP
Options:
-a, --add USER add USER to GROUP
-d, --delete USER remove USER from GROUP
-h, --help display this help message and exit
-Q, --root CHROOT_DIR directory to chroot into
-r, --delete-password remove the GROUPs password
-R, --restrict restrict access to GROUP to its members
-M, --members USER,... set the list of members of GROUP
-A, --administrators ADMIN,...
set the list of administrators for GROUP
Except for the -A and -M options, the options cannot be combined.
gpasswd wangwu # 用戶組密碼設(shè)置,用于用戶切換用戶組時候輸入密碼
[root@eric ~]# gpasswd -d user04 user02 # user02組中刪除user04用戶
Removing user user04 from group user02
[root@eric ~]# gpasswd -a user04 user02 # user02用戶組添加user04用戶
Adding user user04 to group user02
/var/spool/mail 刪除用戶后,該目錄下用戶文件夾需要手工刪除
[root@eric ~]# ls /var/spool/mail
eric ovswitch rpc user02 user04 user10
lisi root user01 user03 user07 zhangsan
[root@eric ~]# rm -rf /var/spool/mail/uer03 # user03用戶刪除后,該目錄還在,需要手工刪除。
[root@eric mail]# ls -a /home/user02 #創(chuàng)建用戶時候,系統(tǒng)會自動生成隱藏文件。(不建議手動創(chuàng)建家目錄文件夾)
. .. .bash_logout .bash_profile .bashrc .mozilla
[root@eric skel]# ls /etc/skel -a # 在該文件夾下創(chuàng)建文件,之后添加用戶時候會自動添加到用戶的家目錄。
. .. .bash_logout .bash_profile .bashrc .mozilla
[root@eric ~]# chfn # 修改用戶描述信息
Changing finger information for root.
Name [root]: user10
Office []: abc
Office Phone []: 123
Home Phone []: 123
Finger information changed.
向用戶彈窗
[root@eric ~]# wall "this sever will be shutdown,please exit" # 向所有用戶彈窗
Broadcast message from root@eric.centos7 (pts/0) (Tue Aug 1 17:46:49 2023):
this sever will be shutdown,please exit
禁止用戶登錄
[root@centos7 ~]# touch /etc/nologin #禁?所有普通?戶登錄
[root@centos7 ~]# rm -rf touch /etc/nologin #取消禁?所有普通?戶登錄