Linux使用者管理
- 使用者存在的意義
- 組存在的意義
- 使用者的檢視
-
-
-
- 檢視目前使用者
- 系統中使用者的檢視
-
-
- 使用者切換
- 使用者在系統中的存儲方式
-
-
-
- /etc/passwd
- /etc/group
- 使用者認證資訊——/etc/shadow
- /etc/skel/.*
- /home/username
-
-
- 使用者管理指令
-
-
-
- 使用者的删除
- 使用者的建立
- 更改使用者資訊 usermod
-
-
- 使用者密碼———passwd
- 使用者授權——/etc/sudoers
使用者存在的意義
使用者在系統中是分角色的,在Linux系統中,由于角色不同,權限和所完成的任務也不同。
目前我們接觸到的就是超級使用者root和普通使用者
原因:多使用者系統從系統管理角度來說更為友善。從安全角度來說,多使用者管理的系統更為安全,比如harry使用者下的某個檔案不想讓其它使用者看到,隻是設定一下檔案的權限,隻有harry這個使用者可讀可寫可編輯就行了,這樣一來隻有harry一個使用者可以對其私有檔案進行操作。Linux 在多使用者下表現最佳,Linux能很好的保護每個使用者的安全,是以很多伺服器用的都是linux系統而不是windows,而unix在安全方面做的更好,是以銀行等重要的資料存儲的伺服器就用的是unix。
組存在的意義
将使用者分組是Linux系統中對使用者進行管理及控制通路權限的一種手段,通過定義使用者組,在很大程度上簡化了管理工作。
就好比公司分成各個部門一樣,就是為了友善管理。
使用者組(group)就是讓某一類使用者(user)具有相同權限的集合體。
比如有時我們要讓多個使用者具有相同的權限,比如檢視、修改某一檔案或執行某個指令,這時我們需要使用者組,我們把使用者都定義到同一使用者組,我們通過修改檔案或目錄的權限,讓使用者組具有一定的操作權限,這樣使用者組下的使用者對該檔案或目錄都具有相同的權限,這是我們通過定義組和修改檔案的權限來實作的。
使用者的檢視
檢視目前使用者
[[email protected] /]$ whoami ##這個$表示普通使用者,whoami檢視目前使用者
kiosk
[[email protected] ~]# whoami ##這個#表示超級使用者
root
系統中使用者的檢視
[[email protected] /]$ id -u kiosk ##看使用者uid
1000
[[email protected] /]$ id -g kiosk ##看使用者gid
1000
[[email protected] /]$ id -G kiosk ##看使用者所屬組id
1000
[[email protected] /]$ id -n kiosk ##看使用者名字而不是id
id: cannot print only names or real IDs in default format
[[email protected] /]$ id kiosk ##看指定使用者id資訊
uid=1000(kiosk) gid=1000(kiosk) groups=1000(kiosk)
使用者切換
類似于windows的登出操作,直接登出圖形界面:
[[email protected] ~]# gnome-session-quit --force
直接登出,用其他使用者登入
在shell中切換使用者:
學了linux,當然要用指令切換使用者了:
[[email protected] ~]# su - tom
Last login: Tue Apr 2 22:00:34 CST 2019 on pts/1
[[email protected] ~]$
注意,su 後面要加-
su - 這個“-”表示在切換使用者的同時也切換目前使用者的環境!
su - 執行的時候,進階使用者到低級使用者不需要密碼,低級到進階、平級之間切換都要密碼。
注意:每次su切換到其他使用者之後記得退出,然後在進行切換不然背景會出現多個bash,可能引起系統錯亂!如下圖,圖中出現了三個bash。
使用者在系統中的存儲方式
/etc/passwd
使用者的資訊文本,類似于使用者的系統資料庫。
[[email protected] ~]$ cat /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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash
tom:x:1001:1001::/home/tom:/bin/bash
1 2 3 4 5 6 7
數字 | 代表意義 |
---|---|
1 | 使用者名 |
2 | 密碼 |
3 | uid |
4 | gid |
5 | 說明 |
6 | 家目錄 |
7 | 預設shell |
說明:預設的shell有哪些可以檢視/etc/shells檔案:
[[email protected] ~]$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
/etc/group
使用者組的說明檔案;
[[email protected] ~]$ cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
tom:x:1001:
tom:x:1001:
kiosk:x:1000:kiosk
1 2 3 4
數字 | 代表意思 |
---|---|
1 | 使用者名 |
2 | 組密碼 |
3 | 組id |
4 | 組成員 |
使用者認證資訊——/etc/shadow
記錄使用者認證資訊的檔案
[[email protected] ~]# cat /etc/shadow
root:$6$oZ1JGQiQ$0lau6DEHSV81iQ54SJyjhI7s9DvAz2Ly5JVwGxMXJqDodk61DXZnF79ibX.scEeHKIe1tBTqLSCIfYV2k3AuW1::0:99999:7:::
bin:*:16925:0:99999:7:::
daemon:*:16925:0:99999:7:::
adm:*:16925:0:99999:7:::
kiosk:$6$0jQUhc9N.t6FbjtD$9z3WWL7.0jfWiTdiisLWu212AjvdFuB0eExG6UdeqUh6T82ktwQVPmCdcmpB3wfb4icaaMOGcd.klgAxYuy64.::0:99999:7:::
tom:!!:17986:0:99999:7:::
1 2 3 4 5 6 7 8 9
數字 | 代表含義 |
---|---|
1 | 使用者名 |
2 | 密碼 |
3 | 使用者密碼最後一次更改時間:從1970-1-1開始的天數 |
4 | 密碼最短有效期,表示這個數字範圍天内不能改密碼 |
5 | 密碼最長有效期,必須有效期内更新密碼,超過這個時間則當機 |
6 | 密碼警告期限,在過期前的天數會發送警告 |
7 | 使用者非活躍天數,預設為空,若設定數值,表示在最長有效期過後仍可使用的天數 |
8 | 使用者到期日,預設為空,表示帳号一定會被當機的時間點 |
9 | 使用者自定義列,暫未啟用 |
注意:這裡的密碼都是對稱加密後的密碼
檢視密碼相關資訊(如加密協定)可以用:
[[email protected] ~]# passwd -S kiosk
kiosk PS 1969-12-31 0 99999 7 -1 (Password set, SHA512 crypt.)
/etc/skel/.*
預設開啟shell的配置,是使用者的骨檔案,.*表示所有的隐藏檔案。
[[email protected] ~]# ls -la /etc/skel/
total 28
drwxr-xr-x. 3 root root 78 Mar 24 21:04 .
drwxr-xr-x. 137 root root 12288 Apr 4 19:46 ..
-rw-r--r--. 1 root root 18 Jul 12 2016 .bash_logout
-rw-r--r--. 1 root root 193 Jul 12 2016 .bash_profile
-rw-r--r--. 1 root root 231 Jul 12 2016 .bashrc
drwxr-xr-x. 4 root root 39 Mar 24 21:03 .mozilla
/home/username
使用者的家目錄,通常使用者目錄挂載于此:
[[email protected] ~]# ls -l /home/
total 4
drwxr--r--. 17 kiosk kiosk 4096 Apr 3 11:14 kiosk
drwx------. 5 tom tom 128 Apr 2 21:57 tom
drwxrwsr-x+ 4 root root 44 Apr 4 11:12 westos
使用者管理指令
使用者的删除
userdel
userdel username ———— 表示删除使用者但不删除使用者的配置檔案
userdel -r username ———— 表示删除使用者并删除使用者的配置檔案
[[email protected] ~]# userdel -r tom
[[email protected] ~]# ls -l /home/
total 4
drwxr--r--. 17 kiosk kiosk 4096 Apr 3 11:14 kiosk
drwxrwsr-x+ 4 root root 44 Apr 4 11:12 westos
使用者的建立
為了更清晰的看使用者建立的過程,我們可以用watch指令監控使用者的建立
[[email protected] ~]# watch -n 1 " tail -n 3 /etc/passwd /etc/group; ls -l /home"
[[email protected] ~]# useradd -u 7777 tom
[[email protected] ~]# useradd -g 21 harry
[[email protected] ~]# useradd -G 21 -c "fuck" -d /home/lee -s /bin/sh jerry
參數 | 含義 |
---|---|
-u | 指定uid |
-g | 指定組id,該使用者組必須存在 |
-G | 指定附加組id,該使用者組必須存在 |
-c | 說明 |
-s | 預設shell |
-d | 指定家目錄 |
關于組的建立
指令 | 說明 |
---|---|
groupadd groupname | 建立使用者組 |
groupadd -g 888 tom | 建立使用者組并指定使用者組id |
groupdel | 删除使用者組 |
更改使用者資訊 usermod
使用者建立好了,萬一需要修改相關資訊怎麼辦?
指令 | 含義 |
---|---|
usermod -l newname username | 更改使用者名稱 |
usermod -u 6666 username | 更該使用者uid |
usermod -g 23 username | 更改使用者gid,也就是初始組 |
usermod -G 24 username | 更改使用者附加組 |
usermod -aG 21 username | 添加使用者附加組 |
usermod -G “” username | 删除使用者所有附加組身份 |
usermod -c “fuck” username | 指定使用者說明文字 |
usermod -d /home/lee username | 更改使用者的家目錄指向,實際沒更改 |
usermod -md /home/lee username | 更改使用者的家目錄 |
usermod -s /bin/sh username | 更改使用者shell |
usermod -L | 當機使用者 |
usermod -U | 解鎖使用者 |
使用者密碼———passwd
指令 | 含義 |
---|---|
passwd tom | 更改tom密碼 |
passwd -l tom | lock the password for the named account (root only)加鎖 出現“!!”鎖兩次 |
passwd -u tom | 解鎖 |
passwd -d tom | delete the password for the named account (root only)清空密碼 |
usermod -L tom | lock the user account加鎖 出現“!”鎖一次 |
usermod -U tom | unlock the user account解鎖 |
注意:設定密碼規則
普通使用者修改密碼時:
1.必須知道目前使用者初始密碼
2.密碼不能和賬戶名相似
3.密碼不能純數字或字母
4.密碼不能有序
根據之間的使用者認證資訊/etc/shadow,關于密碼的一些設定:
指令 | 作用 |
---|---|
passwd -e tom | 改變使用者最後一次更改密碼時間為0 |
chage -d 0 tom | 改變使用者最後一次更改密碼時間為0,使用者在登入時會強制更改密碼 |
passwd -n 1 tom | tom使用者在1天之内不能修改密碼 |
chage -m 1 tom | 同上 |
passwd -x 30 tom | 設定使用者必須在30天内該密碼 |
chage -M 30 tom | 功能同上 |
passwd -w 2 tom | 密碼過期前兩天警告 |
chage -W 2 tom | 同上 |
passwd -i 1 tom | 密碼過期後仍可登入系統的天數 |
chage -I 1 tom | 同上 |
chage -E 2019-11-11 tom | 使用者在2019-11-11時會被當機 |
使用者授權——/etc/sudoers
/etc/sudoers 權力下放檔案
這個檔案可以直接用vim編輯,但不提供文法檢測,打錯了不提示,最好用visudo編輯這個檔案,visudo提供文法檢測
vim /etc/sudoers
或者 visudo
怎麼下放權限?
[[email protected] ~]# visudo
進入後,按:100進入100行左右
發現:
添加上
tom localhost=(root) /usr/sbin/useradd ##tom可以在localhost以root身份執行useradd
測試一下:
[[email protected] ~]$ useradd hello
-bash: /usr/sbin/useradd: Permission denied
發現還是不行,因為你沒有用sudo
[[email protected] ~]$ sudo useradd hello
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for tom:
又發現要密碼,不知道密碼咋辦??還是跟剛才一下進入visudo,修改成這樣就不要密碼了!
tom localhost=(root) NOPASSWD: /usr/sbin/useradd
在試一下:
[t[email protected] ~]$ sudo useradd hello
[sudo] password for tom:
發現還是不行???排查原因!
發現:
[[email protected] ~]$ hostname
foundation2.ilt.example.com
是以我們寫的localhost根本不對,當然就不會生效!!
是以修改visudo檔案:
tom foundation2.ilt.example.com=(root) NOPASSWD:/usr/sbin/useradd
結果:成功!!!
[[email protected] ~]$ sudo useradd fuckass