linux 基礎9-賬號與身份管理
阿新 • • 發佈:2018-12-03
閱讀目錄
1.1:賬戶名稱:/etc/passwd
1.2:賬戶密碼:/etc/shadow
1.3:群使用者:/etc/group
2、賬號管理
3、使用者使用者切換
3.1:su
3.2:sudo visudo
6、批量建立使用者
6.1:常用的一些命名
6.2:批量建立使用者流程
6.3:批量建立使用者指令碼
1. linux的賬號與群組
1.1 賬戶名稱:/etc/passwd:
head -n 5 /etc/password #取前5行
- 賬號名稱,對應UID
- 密碼,早期是在這裡,後來轉到了/etc/shadow中,所以這裡都是x
- UID的範圍:0系統管理員,1-99留給系統預設的賬號,100-499保留給一些服務來使用 ,500-65535給一般使用者
- GID這是使用者的初始化使用者組:使用這一登入系統,就擁有這個群組的相關許可權。
- 使用者說明,基本沒用
- 使用者家目錄
- 使用的shell,bash shell是linux預設使用的shell
uname -r可以來查詢系統核心
1.2 賬戶密碼:/etc/shadow:
head -n 5 /etc/shadow #取前5行
grep 'root' /etc/shadow |awk '{print substr($0,6,34)}' #取密碼段
- 賬號名稱
- 密碼 採用MD5進行加密的密碼,含有$1$的標誌的34位密碼組,如果密碼為*或者!,則表示這個賬號無法登陸。
- 最近修改密碼的日期,以1970年1月1日作為1
- 密碼不可更改日期,修改之後第幾天可以再次修改密碼
- 密碼需要重新更新的天數
- 密碼需要變更期限前的警告期限
- 密碼過期的寬限時間
- 賬號失效日期
- 保留
1.3 群使用者:/etc/group:
groups #檢視所屬的使用者組,有效使用者組為第一個,
newgrp 使用者組 #改變使用者組
exit #退出當前使用者組
- 使用者組
- 密碼,為x
- 使用者組的GID
- 該使用者組所支援的使用者
1.4 群使用者密碼:/etc/gshadow:
- 使用者組
- 密碼,如果為*和!則不可登入
- 使用者組管理員的賬號
- 該使用者組所屬(支援)的使用者,與/etc/group內容一樣
2. 賬號管理
2.1 新增與移除使用者、新增與移除群組:/etc/passwd:
passwd、usermod、useradd這三個命令都是root使用者使用的,普通使用者不能使用。
新增使用者
grep user3 /etc/passwd /etc/shadow /etc/group /etc/gshadow #查詢這四個檔案中有user3的部分。
刪除使用者
修改賬號資訊
插入圖2.1.4
初始化群組和有效群組的改變。
usermod -g test2 -G 502 test1 #-g修改初始化群組,-G修改有效使用者組
2.2 密碼管理:/etc/passwd:
passwd user #修改密碼
2.3 chfn chsh finger id:/etc/passwd:
chsh -s /bin/csh #root也可以用這個改,也可以用usermod
chsh -l #顯示當前可用的shell
修改自己的相關資訊
chfn #會依次讓你修改大名、辦公室房間號碼、辦公室電話號碼、家裡電話,改變的資訊是/etc/passwd的第五欄使用者的資訊說明欄
讀取修改的資訊,也就是用chfn修改的那些東西。
也可以用來查詢當前系統的登入資訊
id #uid、gid、所支援的群組。
2.4 新增和移除群組:/etc/passwd:
3. 使用者使用者切換
3.1 su:
3.2 sudo visudo:
sudo如果沒有制定切換的賬號,則預設使用root
sudo -u "#501" touch /home/test2/3.txt#必須用""包括起來。
sudo -u "#501" -p %H ls /home/test2 #完整的主機名稱
sudo -u "#501" -p %h ls /home/test2 #主機名稱
sudo -u test2 -p %U ls /home/test2 #切換到的賬戶名稱
sudo -u test2 -p %u ls /home/test2 #使用者賬戶的名稱
visudo是用來編輯/etc/sudoers,vi只可以可讀。
vigr 編輯/etc/group
vimpw 編輯/etc/passwd
find / -perm -4000 | xargs ls -lhd #查詢系統下所有包含s許可權的檔案。
4. 使用者的特殊shell與PAM模組
4.1 特殊的shell:
cat /etc/passwd | more
usermod -s /sbin/nologin test1 #給test1指定nologin的shell
vi /etc/nologin.txt #然後輸入不可登陸的資訊
記錄次數
#!/bin/awk -f
BEGIN{
FS=":"
}
{
if ($7=="/bin/bash") bash=bash+1
if ($7=="/sbin/nologin") nologin=nologin+1
}
END{
print "/bin/bash times is :" bash
print "/sbin/nologin times is :" nologin
}
使用bash shell
#!/bin/bash
read -p "please input the /etc/passwd :" path
if [ -z "$path" -o "$path" != "/etc/passwd -o ! -e "$path" ];then
fi
allshell='cut -d : -f 7 "$path"'
for oneshell in $allshell
do
if [ "$oneshell" = "/bin/bash" ];then
bash=$(($bash+1))
fi
if [ "$oneshell" = "/sbin/nologin" ];then
nologin=$(($nologin+1))
fi
done
echo "/bin/bash has $bash times"
echo "/sbin/nologin has $nologin times"
4.2 PAM模組:
在/etc/security/access.conf中加入
- : test1 : ALL EXCEPT 127.0.0.0/24 #僅限本地登陸,其他的ip都不能登陸。
5. 使用者的特殊shell與PAM模組
5.1 查詢使用者:
last -n 5 -d
last -f wtmp2 #預設讀取/var/log/wtmp,如果需要指定讀另外一個,需加上-f引數
5.2 使用者talk:
mesg y
mesg n
write test1
如果root傳送廣播資訊,即使其他使用者關閉資訊接收,也會收到資訊。
5.3 使用者郵箱:
stty erase ^H
mail -s "this is first test" [email protected]
this is first test
1. linux的賬號與群組
6.1 常用的一些命名:
6.2 建立使用者流程:
vi /etc/group #開啟/etc/group檔案進行編輯
test4:x:505: #加入test4使用者組
grpconv #group和gshadow同步
grep test4 /etc/gshadow #查詢gshadow,確認已經同步
vi /etc/passwd #開啟/etc/passwd檔案進行編輯
test4:x:505:505::/home/yesy4:/bin/bash #加入test4使用者
pwconv #passwd和shadow同步
grep test4 /etc/shadow #查詢shadow,確認已經同步
passwd test4 #輸入密碼 #輸入使用者的密碼(設定)
mkdir /home/test4 #建立使用者家目錄
ls -la /etc/skel #顯示/etc/skel下的所有檔案
cp -a /etc/skel/.[a-z]* /home/test4 #將/etc/skel下的所有檔案都拷貝到使用者家目錄中
ls -la /home/test4 #顯示使用者家目錄的所有檔案和屬性
chown -R test4:test4 /home/test4 #吧家目錄的屬性由root改為test4
ls -la /home/test4 #顯示使用者家目錄的所有檔案和屬性
vigr /etc/group #開啟/etc/group檔案進行編輯
test5:x:505: #加入test5使用者組
選擇y #進入編輯gshadow
test5:x:: #加入test5使用者組密碼
vipw /etc/passwd #開啟/etc/passwd檔案進行編輯
test5:x:505:505::/home/yesy4:/bin/bash #加入test5使用者
選擇y #進入編輯shadow
test5:!!:14519:0:99999:7:3:: #加入test5shadow密碼檔案
passwd test4 #輸入密碼 #輸入使用者的密碼(設定)
mkdir /home/test5 #建立使用者家目錄
ls -la /etc/skel #顯示/etc/skel下的所有檔案
cp -a /etc/skel/.[a-z]* /home/test5 #將/etc/skel下的所有檔案都拷貝到使用者家目錄中
ls -la /home/test5 #顯示使用者家目錄的所有檔案和屬性
chown -R test4:test5 /home/test5 #吧家目錄的屬性由root改為test5
ls -la /home/test5 #顯示使用者家目錄的所有檔案和屬性
6.3 批量建立使用者指令碼:
建立使用者
vi useradd.sh
#!/bin/bash
accountfile=user:passwd
test -e "$accountfile" && rm -f "$accountfile"; touch "$accountfile" || touch "$accountfile"
read -p "please input your zhuanye:" zhuanye
read -p "please input your grade:" grade
read -p "please input the start number:" start_nu
read -p "please input the user number:" user_nu
read -p "please choice passwd stardand:1)same as usernaem 2)random" pwd
if [ "$zhuanye" ="" ];then
echo "you should input the zhuanye!!"; exit 1
fi
testing1='echo $grade | grep [^0-9]'
testing2='echo $start_nu | grep [^0-9]'
testing3='echo $user_nu | grep [^0-9]'
if [ "$testing1" !="" -o "$testing2" !="" -o "$testing3" !="" ];then
echo "the grade or start number or user number has include characters!!"; exit 1
fi
for ((i=$start_nu;i<$(($start_nu+$user_nu));i++))
do
account=$zhuanye$grade$i
if [ "$pwd" = "1" ];then
password="$account"
else
password=""
test_nu=0
until [ "$test_nu" = "6" ]
do
temp_nu=$(($RANDOM*50/32767+30))
until [ "$test_nu" != "60" ]
do
temp_nu=$(($RANDOM*50/32767+30))
done
temp_ch='printf "\x$temp_nu"'
password=$password$temp_ch
temp_nu=$(($temp_nu+1))
done
fi
echo "$account":"$password" >>"$accountfile"
done
cat "$accountfile" | cut -d ":" | xargs -n 1 useradd
chpasswd < "$accountfile"
pwconv
echo "OK,successful"
刪除使用者
vi userdel.sh
#!/bin/bash
cat "user:passwd" | cut -d : -f1 | xargs -n 1 userdel -r