Linux使用者,組管理命令整理及例項詳解
使用者,組
相關檔案:
/etc/passwd 使用者相關資訊
/etc/login.defs 設定使用者建立時預設相關資訊
/etc/defualt/useradd 使用者新增時會建立的相關資訊設定
/etc/skel 使用者家目錄下相關隱藏檔案
/etc/group 組相關資訊
/etc/gshadow 組密碼
/etc/shadow 使用者密碼
/var/spool/mail 使用者郵箱記錄
新增使用者就是在/etc/passwd、/etc/shadow、/etc/group中新增使用者相關資訊
預設屬性來源:
/etc/default/useradd
/etc/skel
/etc/default/useradd**
使用者類別
管理員 0 root
普通使用者 1-65535,2^16個
管理使用者 1-200 系統中的管理使用者
系統使用者 201-999,專門用來執行後臺程序和服務的使用者,不允許登陸系統(服務使用者)
普通使用者 1000-60000,後面的很少使用
使用者組類別
按照使用者類別的分法
管理員組 root
普通組
常用分法
(主)基本組,使用者的預設組
附加組 額外組,預設組以外的其他組
檔案中各個欄位的含義
/etc/shadow
redhat:$6$e4FZXLbs$hgRQKtk00memafY5XWbVqGFcvEcgwFmNuKat90h9QmcBTnqeBnQR/Kg10JvmWvRXsfVzAR6HwpZjj1GXBWhYv.:17801:0:99999:7::: 使用者名稱:加密後的密碼: 最後一次修改時間:密碼最短使用期限:密碼最長使用期限:密碼過期警告時間:密碼過期寬限時限:賬號過期精確時間:保留欄位 最後一次修改時間:距離1970年1月1日的天數 密碼最短使用期限:為0 表示不限制 密碼最長使用期限:密碼使用多久之後必須更改,99999表示可以不更改 密碼過期警告時間:密碼過期前多少天進行警告,提醒使用者更改密碼但不鎖定使用者 密碼過期寬限時限:密碼過期後還可以更改密碼的時間,若沒改則鎖定使用者 賬號過期精確時間:若為99999則永不過期
etc/passwd
redhat:x:1088:1000:redhat:/home/redhat:/bin/bash
#使用者名稱:密碼的佔位符:UID:GID:描述欄位(註釋):使用者家目錄:shell欄位設定account 使用者名稱
password 密碼,x為密碼佔位符,目標為/etc/shadow第二個欄位
UID 使用者ID
GID 基本組ID,額外組資訊在/etc/group檔案中
CECOS 使用者基本資訊
HOME DIR 使用者家目錄,系統使用者不允許登陸
shell 使用者的預設shell,/etc/shells儲存了所有的合法shell
命令解析
使用者管理
useradd useradd USERNAME/adduser -l -u UID 指定UID -g GID 指定基本組 -G GID1,GID2,... 指定附加組 -c “註釋資訊” 指定使用者註釋資訊 chfn -d /path/to/dir 指定某個目錄為使用者家目錄 -s /shell 指定使用者使用的shell usermod 修改使用者相關資訊,選項同useradd -u 修改UID -g 修改基本組,基本組必須事先存在 -G 這個選項會覆蓋之前的附加組,和-a選項配合使用 usermod -a -G 追加附加組 -c 修改使用者註釋資訊 -s 修改使用者shell -l 修改登陸名 userdel userdel USERNAME -r 刪除使用者時刪除使用者家目錄和使用者的郵箱 id 檢視使用者的ID資訊,選項同useradd -u 顯示UID -G 顯示附加GID -g 顯示GID
passwd 修改使用者密碼
--stdin
echo "redhat" | passwd --stdin USERNAME
-l 鎖定使用者賬號密碼
-u 解鎖使用者賬號
-n 指定密碼最短使用期限
-x 指定密碼最長使用期限
-w 指定警告時間
-i 指定寬限動時間
-d 刪除使用者密碼,在redhat中,空密碼使用者禁止登陸
chage change age
-d 指定最後一次修改密碼日期
-E 過期日期
-I 寬限時間
-m 最短使用期限
-M 最長使用期限
-w 警告時間
組管理
groupadd 新增一個組
-n
-g GID
-r 新增系統組
groupdel 刪除一個組 (不能刪除基本組)
groupmod 修改一個組
-g GID
-n GRPNAME 修改組名
gpasswd +使用者名稱設定組密碼
-a 新增使用者到組
-d 將指定使用者移出本組
-M 設定組成員列表
-A 指定管理員(組長)
-r 刪除組密碼
-R 限制使用者登入組,只有組中成員才能加入該組
newgrp 登陸一個新組
使用exit退出新組
例項詳解
一、
建立學習組1,學習組2 ;他們的組gid分別為1111,2222,新增成員並將uid為1088,和uid為1066的成員為兩組的組長,其中一組組長有一個‘外號’laoda。
不同學習組進入需要密碼才能進入(設定密碼);
作為學習組長可以將指定成員拉進本組,和移出本組其他成員沒有權利干涉。
(只能一個一個拉和刪);即使密碼洩露也不希望組之外的成員進入。
建立組1和2,並搞出來兩個指定使用者
groupadd learn1 -g 1111
groupadd learn2 -g 2222
usermod -u 1088 -a -G 1111 redhat
usermod -u 1066 -a -G 2222 xiaoming
將指定使用者設定為組長
gpasswd -A redhat learn1
gpasswd -A xiaoming learn2
給一個組長起外號(改註釋)
[[email protected] test]# usermod -c "老大" xiaoming
給學習組設定密碼
[[email protected] test]# gpasswd learn2
Changing the password for group learn2
New Password:
Re-enter new password:
限制使用者登入組,只有組中使用者可以登入該組(密碼洩露其他人也無法登陸)
[[email protected] test]# gpasswd -R learn1
[[email protected] test]# gpasswd -R learn2
二、
建立使用者和使用者組
請按照以下要求建立使用者、使用者組:
1.新建一個名為adminuser的組,組id為40000
[[email protected] ~]# groupadd -g 40000 adminuser
2.新建一個名為natasha的使用者,並將adminuser作為其附屬組
[[email protected] ~]# useradd natasha -G adminuser
[[email protected] ~]# cat /etc/group | grep 40000
adminuser:x:40000:natasha
[[email protected] ~]# cat /etc/passwd | grep natasha
natasha:x:1001:1001::/home/natasha:/bin/bash
[[email protected] ~]# cat /etc/shadow | grep natasha
natasha:!!:17802:0:99999:7:::
[[email protected] ~]# id natasha
uid=1001(natasha) gid=1001(natasha) groups=1001(natasha),40000(adminuser)
3.新建一個名為harry的使用者,並將adminuser作為其基本組
[[email protected] ~]# useradd -g adminuser harry
[[email protected] ~]# id harry
uid=1003(harry) gid=40000(adminuser) groups=40000(adminuser)
4.新建一個名為sarah的使用者,其不屬於adminuser組,並將其shell設定為
不可登陸shell
[[email protected] ~]# useradd sarah -s /sbin/nologin
[[email protected] ~]# id sarah
uid=1002(sarah) gid=1002(sarah) groups=1002(sarah)
5.新增使用者maomao,修改它的家目錄為/maomao,要求切換maomao時命令提示
顯示正常。
[[email protected] ~]# useradd maomao -d /maomao
[[email protected] ~]# su - maomao
[[email protected] ~]$ id maomao
uid=1004(maomao) gid=1004(maomao) groups=1004(maomao)
[[email protected] ~]$ su - root
Password:
Last login: Fri Sep 28 12:20:05 CST 2018 from 192.168.75.1 on pts/1
[[email protected] ~]# cat /etc/passwd | grep maomao
maomao:x:1004:1004::/maomao:/bin/bash
6.建立std1 ,std2兩個組,要求std1組中有三個使用者,{1 2 3}
std2有四個使用者。{a b c d}
(此處可以批量建立使用者,編寫一個shell指令碼來實現)
先建立兩個組
[[email protected] test]# groupadd std1 -g 2000
[[email protected] test]# groupadd std2 -g 2002
[[email protected] test]# cat /etc/group | grep 2000
std1:x:2000:
[[email protected] test]# cat /etc/group | grep 2002
std2:x:2002:
編寫指令碼:在根下建立的test目錄中建立這樣幾個檔案
-rw-r--r--. 1 root root 8 Sep 28 12:58 user1.txt
-rw-r--r--. 1 root root 304 Sep 28 12:58 useradd.sh
-rw-r--r--. 1 root root 7 Sep 28 12:47 user.txt
指令碼
[[email protected] test]# vim useradd.sh
[[email protected] test]# vim useradd.sh
[[email protected] test]# cat /etc/passwd
1:x:1005:2000::/home/1:/bin/bash
2:x:1006:2000::/home/2:/bin/bash
3:x:1007:2000::/home/3:/bin/bash
a:x:1008:2002::/home/a:/bin/bash
b:x:1009:2002::/home/b:/bin/bash
c:x:1010:2002::/home/c:/bin/bash
d:x:1011:2002::/home/d:/bin/bash
7.natasha、harry和sarah三個使用者的密碼均設定為glegunge
改密碼的第一種方法:
[[email protected] test]# passwd natasha
Changing password for user natasha.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
第二種:
[[email protected] test]# echo "glegunge" | passwd --stdin harry
Changing password for user harry.
passwd: all authentication tokens updated successfully.
[[email protected] test]# echo "glegunge" | passwd --stdin sarah
Changing password for user sarah.
passwd: all authentication tokens updated successfully.
8.請建立一個名為alex的使用者,並滿足以下要求:
使用者id為3456,描述名為alian密碼為glegunge
[[email protected] test]# useradd -u 3456 -c "alian" alex
[[email protected] test]# echo glegunge | passwd --stdin alex
Changing password for user alex.
passwd: all authentication tokens updated successfully.
[[email protected] test]# id alex
uid=3456(alex) gid=3456(alex) groups=3456(alex)
[[email protected] test]# cat /etc/shadow | grep alex
alex:$6$6YOVSoLi$sfvEAhkMPrI7Oc4Q/RcKt98gQbjJoxaKBUCcZj6YO8SsEzU3eezMo55gF4/MyRMvXbfyIcq7pD9.GpXUO.ZLz/:17802:0:99999:7:::
9.建立g1組,要求建立一個屬於redhat使用者g1組的檔案redhat.txt
[[email protected] test]# groupadd g1
[[email protected] test]# id redhat
uid=1000(redhat) gid=1000(redhat) groups=1000(redhat)
建立檔案redhat.txt並檢視
[[email protected] test]# touch redhat.txt
[[email protected] test]# ll redhat.txt
-rw-r--r--. 1 root root 0 Sep 28 13:22 redhat.txt
更改檔案的所屬者和所屬組
[[email protected] test]# chown redhat redhat.txt
[[email protected] test]# chgrp g1 redhat.txt
嗚啦!嗚啦啦啦啦!!!可以看到所屬者變成了redhat所屬組變成了g1。
[[email protected] test]# ll redhat.txt
-rw-r--r--. 1 redhat g1 0 Sep 28 13:22 redhat.txt
三、
1、建立一個使用者xiaoming,其ID號為2002,基本組為one(組ID為3003),附加組為linux。
[[email protected] test]# groupadd -g 3003 one
[[email protected] test]# groupadd -g 3004 linux
[[email protected] test]# useradd -u 2002 -g 3003 -G linux xiaoming
[[email protected] test]# id xiaoming
uid=2002(xiaoming) gid=3003(one) groups=3003(one),3004(linux)
2、建立一個使用者fedora,其全名為Fedora Community,預設shell為tcsh。
[[email protected] test]# useradd -c "Fedora Community" -s /bin/tcsh fedora
[[email protected] test]# cat /etc/passwd | grep fedora
fedora:x:3457:3457:Fedora Community:/home/fedora:/bin/tcsh
3、修改xiaoming的ID號為4004,基本組為linux,附加組為one和fedora。
[[email protected] test]# usermod -u 4004 -g linux -G one,fedora xiaoming
[[email protected] test]# cat /etc/passwd | grep xiaomin
xiaoming:x:4004:3004::/home/xiaoming:/bin/bash
[[email protected] test]# id xiaoming
uid=4004(xiaoming) gid=3004(linux) groups=3004(linux),3003(one),3457(fedora)
4、將alex的預設shell改為/bin/bash。
[[email protected] test]# usermod -s /bin/bash alex
[[email protected] test]# cat /etc/passwd | grep alex
alex:x:3456:3456:alian:/home/alex:/bin/bash
5、新增系統使用者hbase,且不允許其登陸系統。
[[email protected] test]# useradd -r hbase -s /sbin/nologin