用戶管理
2024-06-28 16:04:54
供稿:網友
用戶管理
用戶管理
groupadd,groupdel,useradd,usermod,userdel,passwd,chage
一、用戶/組基本概念Users and groups:. Every PRocess (running program) on the system runs as a particular user.. Every file is owned by a particular user. . access to files and directories are restricted by user. . The user associated with a running process determines the files and directories accessible to that process.. 系統中每一個運行的進程都需要一個特別的用戶. 每一個文件都有一個特別的擁有者. 設置文件的訪問權限需要針對于用戶來設置. 進程所關聯的用戶將決定它對文件的訪問權限1、產看當前登錄的用戶信息[root@Server ~]# iduid=0(root) gid=0(root) 組=0(root) 環境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c10232、查看文件的owner:[root@Server ~]# ll /home/drwx------. 4 alice alice 4096 2月 11 05:35 alicedrwx------. 4 tom tom 4096 2月 11 05:34 tom3、查看運行進程的用戶[root@Server ~]# ps auxUSER PID %CPU %MEM VSZ rss TTY STAT START TIME COMMANDroot 1 0.0 0.1 19364 1260 ? Ss Feb10 0:02 /sbin/initroot 2 0.0 0.0 0 0 ? S Feb10 0:00 [kthreadd]root 3 0.0 0.0 0 0 ? S Feb10 0:00 [migration/0]root 4 0.0 0.0 0 0 ? S Feb10 0:00 [ksoftirqd/0]4、和用戶組相關的一些文件[root@localhost ~]# man 5 passwd[root@localhost ~]# man 5 shadow[root@localhost ~]# man 5 group[root@localhost ~]# man 3 crypt/etc/passwd root:x:0:0:root:/root:/bin/bash第一段:用戶名第二段:密碼占位符第三段:uid第四段:gid第五段:描述第六段:家目錄第七段:用戶shell類型/etc/shadow root:$1$MYG2NDG6$a1wtyr5GDM2esAPjug0YP0:17025:0:99999:7:::第一列:用戶名第二列:密碼位 (有密碼 無密碼 !帳號鎖定 !!密碼鎖定 *該用戶永久不能登錄系統)$id$salt$encrypted加密算法$id:$1: md5$5: SHA-256$6: SHA-512注釋:加密算法就是用明文的密碼和一個叫做salt的東西通過函數crypt()完成加密的,salt由a-z A-Z 0-9 . / 組成,用來決定使用4096種不同內建表格的哪一種第三列:最后一次修改密碼時間# echo $(($(date --date="2017/02/12" +%s)/86400+1))--date="2017/02/12" 表示2017年2月12號這一天%s 表示自UTC 時間 1970-01-01 00:00:00 以來所經過的秒數86400 表示一天的所有的秒數+1 表示1970年1月1號那一天echo $(()) echo $[] linux系統中進行運算的語法結構 第四列:密碼最短有效天數(即密碼至少使用的天數,0無限制) -n第五列:密碼最長有效天數(99999 永久生效) -x第六列:密碼過期前警告時間(默認是7天,在這期間可使用就舊密碼) -w第七列:密碼過期后的寬限時間(密碼過期后預留給賬戶修改密碼的時間) -i第八列:帳號失效期第九列:保留列/etc/group root:x:0:第一列:組名第二列:組密碼占位符第三列:gid第四列:[用戶列表]系統約定: RHEL6uid: 0 特權用戶uid: 1~499 系統用戶uid: 500~60000 普通用戶二、用戶/組管理用戶組:[root@localhost ~]# groupadd hr[root@localhost ~]# groupadd sale[root@localhost ~]# groupadd it[root@localhost ~]# groupadd fd[root@localhost ~]# groupadd net01 -g 2000 //添加組net01,并指定gid 2000[root@localhost ~]# grep 'net01' /etc/group //查看/etc/group中組net01信息[root@localhost ~]# groupdel net01 //刪除組net01用戶:useradd creates users創建用戶 未指定選項[root@localhost ~]# useradd user01 . 未指定該用戶的主組. 未指定該用戶的附加組. 未指定用戶的HOME. 未指定用戶的SHELL. 未指定用戶的UID...[root@localhost ~]# grep 'user01' /etc/passwd /etc/shadow /etc/group/etc/passwd:user01:x:507:512::/home/user01:/bin/bash/etc/shadow:user01:!!:16589:0:99999:7:::/etc/group:user01:x:512:[root@localhost ~]# id user01 uid=507(user01) gid=512(user01) groups=512(user01)小結:如果創建一個用戶時,未指定任何選項,系統在/home生成和用戶同名的文件夾作為用戶的家目錄,沒有指定shell的時候,默認是登陸shell,沒有指定uid的時候默認分配一個uid,且分配的uid值時passwd文件中最大的一個普通用戶占用的uid值的下一個數字,會自動生成和用戶名相同的組作為用戶的Primary Group,且一般情況下gid的值和uid的值相同,除非相同的這個gid值被其他的組占用了,他會自動的向下查找,直到找到一個沒有被占用的gid作為當前主組的gid刪除用戶userdel deletes users[root@localhost ~]# userdel user10 //刪除用戶user10,但不刪除用戶家目錄和mail spool[root@localhost ~]# ll -d /home/user10/drwx------ 3 506 510 4096 05-01 21:14 /home/user10/[root@localhost ~]# ll /var/spool/mail/user10 -rw-rw---- 1 506 mail 0 05-01 21:14 /var/spool/mail/user10[root@localhost ~]# userdel -r user2 //刪除用戶user2,同時刪除用戶家目錄和mail spool創建用戶 指定選項[root@localhost ~]# useradd user02 -u 503 //創建用戶usr02,指定uid[root@localhost ~]# useradd user03 -d /aaa //創建用戶user03 指定家目錄[root@localhost ~]# useradd user05 -s /sbin/nologin //創建用戶并指定shell[root@localhost ~]# useradd user07 -G hr,it,fd //創建用戶,指定附加組[root@localhost ~]# useradd user10 -u 4000 -s /sbin/nologin[root@localhost ~]# useradd user04 -M //創建用戶user04,不創建家目錄創建用戶時-g選項的相關注意事項
[root@localhost ~]# useradd user01[root@localhost ~]# grep user01 /etc/passwd /etc/group /etc/shadow/etc/passwd:user01:x:500:500::/home/user01:/bin/bash/etc/group:user01:x:500:/etc/shadow:user01:!!:17003:0:99999:7:::[root@localhost ~]# id user01uid=500(user01) gid=500(user01) 組=500(user01)[root@localhost ~]# useradd user02 -g 500[root@localhost ~]# id user02uid=501(user02) gid=500(user01) 組=500(user01)[root@localhost ~]# grep user02 --color /etc/passwd /etc/group/etc/passwd:user02:x:501:500::/home/user02:/bin/bash[root@localhost ~]# userdel -r user01userdel: group user01 is the primary group of another user and is not removed.[root@localhost ~]# userdel -r user02[root@localhost ~]# grep user01 /etc/groupuser01:x:500:[root@localhost ~]# groupdel user01[root@localhost ~]# grep user01 /etc/group創建用戶時-G選項相關的注意事項
[root@localhost ~]# useradd user01[root@localhost ~]# grep user01 /etc/passwd /etc/group/etc/passwd:user01:x:500:500::/home/user01:/bin/bash/etc/group:user01:x:500:[root@localhost ~]# useradd user02 -G user01[root@localhost ~]# grep user02 /etc/passwd /etc/group/etc/passwd:user02:x:501:501::/home/user02:/bin/bash/etc/group:user01:x:500:user02/etc/group:user02:x:501:[root@localhost ~]# id user02uid=501(user02) gid=501(user02) 組=501(user02),500(user01)[root@localhost ~]# userdel -r user01[root@localhost ~]# grep user01 /etc/passwd /etc/group/etc/group:user01:x:500:user02[root@localhost ~]# grep user02 /etc/passwd /etc/group/etc/passwd:user02:x:501:501::/home/user02:/bin/bash/etc/group:user01:x:500:user02/etc/group:user02:x:501:[root@localhost ~]# userdel -r user02[root@localhost ~]# grep user02 /etc/passwd /etc/group[root@localhost ~]# [root@localhost ~]# grep user01 /etc/passwd /etc/group/etc/group:user01:x:500:[root@localhost ~]# groupdel user01=====================================================================================================創建相同uid的帳號[root@localhost ~]# useradd -o -u 0 admin組成員管理注意:gpasswd將用戶添加到組或從組中刪除,只針對已存在的用戶[root@localhost ~]# gpasswd -a user07 it //將某個用戶加入到某個組[root@localhost ~]# gpasswd -M user01,user02 it //將某些成員添加到某個組[root@localhost ~]# gpasswd -d user07 it //刪除用戶usr07從it組用戶密碼[root@localhost ~]# passwd alice[alice@localhost ~]$ passwd更改用戶 alice 的密碼 。為 alice 更改 STRESS 密碼。(當前)UNIX 密碼: //輸入的是alice剛剛設置過的密碼新的 密碼: //輸入的是alice新的密碼,不可以是簡單的數字,也不可以是基于字典的單詞passwd: 所有的身份驗證令牌已經成功更新。修改密碼信息passwd -l jim 鎖定密碼passwd -u jim 解鎖密碼passwd -S robin 查看密碼狀態passwd -d user01 刪除已命名帳號的密碼(只有根用戶才能進行此操作)chage -d 0 alice 強制用戶在下一次登錄時修改密碼chage -l alice 查看用戶密碼信息修改用戶的信息 usermodusermod modifies existing users==修改UID,SHELL==[root@localhost ~]# usermod --help[root@localhost ~]# useradd user10[root@localhost ~]# grep 'user10' /etc/passwduser10:x:509:509::/home/user10:/bin/bash[root@localhost ~]# usermod -u 2000 user10 //修改用戶uid[root@localhost ~]# usermod -s /sbin/nologin user10 //修改用戶shell[root@localhost ~]# usermod -G hr,it,fd user10 //直接覆蓋附加組[root@localhost ~]# usermod -a -G yw,kf user10 //在原有基礎上添加附加組帳號鎖定,解鎖[root@localhost ~]# useradd user1000[root@localhost ~]# passwd user1000[root@localhost ~]# grep 'user1000' /etc/shadowuser1000:$1$Hw2wCJoe$FU91eSBsBx1W0CGdIhTwh/:15775:0:99999:7:::[root@localhost ~]# usermod -L user1000[root@localhost ~]# grep 'user1000' /etc/shadowuser1000:!$1$Hw2wCJoe$FU91eSBsBx1W0CGdIhTwh/:15775:0:99999:7:::登錄測試,帳號鎖定之后,普通用戶登錄不上[root@localhost ~]# usermod -U user1000[root@localhost ~]# grep 'user1000' /etc/shadowuser1000:$1$Hw2wCJoe$FU91eSBsBx1W0CGdIhTwh/:15775:0:99999:7:::登錄測試設置賬號過期[root@localhost ~]# date 2017年 02月 12日 星期日 1:22:20 CST[root@localhost ~]# usermod -e 2017-02-12 user1000[root@localhost ~]# grep 'user1000' /etc/shadowuser1000:$1$Hw2wCJoe$FU91eSBsBx1W0CGdIhTwh/:17025:0:99999:7::16415:登錄測試[root@localhost ~]$ su - user1000PassWord: Your account has expired; please contact your system administratorsu: incorrect password修改用戶名[root@localhost ~]# usermod -l tomm tom[root@localhost ~]# id tomid: tom:無此用戶[root@localhost ~]# id tommuid=502(tomm) gid=503(tom) 組=503(tom)三、批量導入用戶[root@localhost tmp]# cat user.txt (用戶名,uid,家目錄不可以重復)abc1:x:1000:1000::/home/abc1:/bin/bashabc2:x:1001:1001::/home/abc2:/bin/bash[root@localhost tmp]# newusers < user.txt [root@localhost tmp]# cat passwd.txt abc1:123abc2:123[root@localhost tmp]# chpasswd < passwd.txt [root@localhost ~]# su - aaa1-bash-4.1$導入用戶的家目錄中的.bash環境[root@localhost ~]# su - aaa1-bash-4.1$ ls -a. .. -bash-4.1$ cp /etc/skel/.bash* .-bash-4.1$ ls -a. .. .bash_history .bash_logout .bash_profile .bashrc-bash-4.1$ logout[root@localhost ~]# su - aaa1[aaa1@localhost ~]$ useradd 命令參考的文件1. /etc/login.defs2. /etc/default/useradd //可以使用命令 useradd -D查看3. /etc/skel/* //用戶的初始配置文件 .bashrc .bash_profile.bash_logout環境變量配置文件.bash_history 保存用戶執行的歷史命令,當用戶退出時寫入該文件..bash_logout 保存當用戶退出時,執行的命令.bashrc 保存用戶定義的別名和函數.bash_profile 保存用戶搜索命令的路徑.bashrc shell級別環境配置文件.bash_profile 登錄級別環境配置文件/etc/bashrc 全局shell級別環境配置文件/etc/profile 全局登錄級別環境配置文件 加載順序[root@localhost robin]# su - robin/etc/profile.bash_profile.bashrc/etc/bashrcsu - robin 和 su robinsu - robin 登錄級別切換用戶su robin shell級別切換用戶四、給普通用戶提權以下兩種方式都可以將普通用戶提升為root1. Switching users with su[alice@localhost ~]$ useradd u1-bash: /usr/sbin/useradd: 權限不夠[alice@localhost ~]$ su - rootpassword:[root@localhost ~]# 2. Running commands as root with sudo以root身份授權普通用戶[root@localhost ~]# vim /etc/sudoers%wheel ALL=(ALL) NOPASSWD: ALL或[root@localhost ~]# visudo //可以驗證sudoers文件是否有語法錯誤[root@localhost ~]# useradd lusu -G wheel[root@localhost ~]# id lusu uid=504(lusu ) gid=504(lusu ) 組=504(lusu ),10(wheel)[lusu @localhost ~]$ useradd gougou10-bash: /usr/sbin/useradd: 權限不夠[lusu @localhost ~]$ sudo useradd gougou10[lusu @localhost ~]$ id gougou10uid=505(gougou10) gid=505(gougou10) 組=505(gougou10)