使用者管理
使用者帳号 與 組帳号 唯一辨別: UID(使用者的ID) GID(組的ID)
管理者的UID為:0
使用者
/etc/passwd #使用者基本資訊
添加使用者
useradd
-u 設定賬戶UID
-d 設定賬戶家目錄 預設為/home/使用者名
-g 設定賬戶所屬基本組
-e 設定賬戶的失效日期 格式為YYYY-MM-DD
-G 設定賬戶所屬附加組
-s 設定賬戶登陸Shell
例:
[root@server0 ~]# grep 'nsd01' /etc/passwd
nsd01:x:1001:1001::/home/nsd01:/bin/bash
使用者名:密碼占位符:UID:GID:使用者的描述資訊:家目錄:解釋器
[root@server0 /]# useradd nsd01
[root@server0 /]# id nsd01
[root@server0 /]# grep 'nsd' /etc/passwd
[root@server0 /]# useradd -u 1100 nsd02 #指定UID
[root@server0 /]# grep 'nsd' /etc/passwd
[root@server0 /]# useradd -d /op/haha nsd03 #指定家目錄
[root@server0 /]# grep 'nsd' /etc/passwd
指定登陸的解釋器程式
[root@server0 /]# useradd -s /sbin/nologin nsd04
[root@server0 /]# grep 'nsd' /etc/passwd
如果使用者的解釋器程式為/sbin/nologin則不能登陸作業系統
[root@server0 /]# useradd -g nsd01 nsd09
[root@server0 /]# id nsd09
[root@server0 /]# useradd -G nsd01 nsd10
[root@server0 /]# id nsd10
修改使用者資訊
usermod
-u 修改賬戶UID
-d 修改賬戶家目錄
-g 修改賬戶所屬基本組
-G 修改賬戶所屬附加組
-e 修改賬戶的失效日期
-s 修改賬戶登陸Shell
顯示賬戶資訊
id 使用者名
删除使用者
userdel
-r 遞歸删除 删除使用者家目錄
臨時切換使用者身份
su - 使用者名
賬戶密碼
/etc/shadow #使用者密碼資訊
[root@server0 ~]# head -1 /etc/shadow
root:$6$UiGI4Tc2$htsXYn5cJnOqv3P1VLcUSgfjDu2pL5yiJBuua6foZAHdwqeuLHfYUfS/vBn27Wjvoel8EJgtdsMjyquqvKAmf1:16261:0:99999:7:::
使用者名 : 密碼 : 上一次修改時間 : 密碼最短使用時間 : 密碼最長使用時間 : 提前多少時間警告:::
修改密碼:
passwd 使用者名
-l 鎖定賬戶
-u 解鎖賬戶
#管理者模式下
echo '密碼' | passwd --stdin 使用者名 #非互動式更改密碼
組賬戶管理
組賬戶:
友善管理使用者帳号(權限)把相同權限使用者加入到同一個組中
分類:
基本組
附加組(公共組 從屬組)
/etc/group #組基本資訊
建立組帳号
groupadd
[root@server0 ~]# useradd kenji
[root@server0 ~]# useradd tom
[root@server0 ~]# useradd kaka
[root@server0 ~]# useradd henter
[root@server0 ~]# groupadd tarena
[root@server0 ~]# grep 'tarena' /etc/group
gpasswd
gpasswd -a #加入組成員
gpasswd -d #删除組成員
gpasswd -M #覆寫組成員
gpasswd - M '' 組名 #删除該組全部成員
[root@server0 ~]# grep 'tarena' /etc/group #檢視組資訊
[root@server0 ~]# gpasswd -a kenji tarena #加入組成員
[root@server0 ~]# id kenji
[root@server0 ~]# gpasswd -a tom tarena
[root@server0 ~]# grep 'tarena' /etc/group
[root@server0 ~]# gpasswd -a kaka tarena
[root@server0 ~]# grep 'tarena' /etc/group
[root@server0 ~]# gpasswd -d kenji tarena #删除組成員
[root@server0 ~]# grep 'tarena' /etc/group
[root@server0 ~]# gpasswd -M 'kenji,henter' tarena
[root@server0 ~]# grep 'tarena' /etc/group
[root@server0 ~]# gpasswd -M 'kenji,kaka,tom,henter' tarena
[root@server0 ~]# grep 'tarena' /etc/group
[root@server0 ~]# gpasswd -M '' tarena
[root@server0 ~]# grep 'tarena' /etc/group
使用者安全:
passwd
-l鎖定 -u解鎖 -S 看狀态
[root@server0 ~# useradd test
[root@server0 ~]# echo 123 | passwd --stdin test
更改使用者 test 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
[root@server0 ~]# passwd -S test
test PS 2018-01-01 0 99999 7 -1 (密碼已設定,使用 SHA512 算法。)
[root@server0 ~]# passwd -l test
鎖定使用者 test 的密碼 。
passwd: 操作成功
[root@server0 ~]# passwd -S test
test LK 2018-01-01 0 99999 7 -1 (密碼已被鎖定。)
[root@server0 ~# passwd -u test
解鎖使用者 test 的密碼。
passwd: 操作成功
[root@server0 ~]# passwd -S test
test PS 2018-01-01 0 99999 7 -1 (密碼已設定,使用 SHA512 算法。)
chage #檢視使用者資訊
選項:
-d, --lastday 最近日期 将最近一次密碼設定時間設為“最近日期” 0 強制修改密碼
-E, --expiredate 過期日期 将帳戶過期時間設為“過期日期” yyyy/mm/dd 指定失效日期 (-1 取消)
-h, --help 顯示此幫助資訊并推出
-I, --inactive INACITVE 過期 INACTIVE 天數後,設定密碼為失效狀态
-l, --list 顯示帳戶年齡資訊
-m, --mindays 最小天數 将兩次改變密碼之間相距的最小天數設為“最小天數”
-M, --maxdays 最大天數 将兩次改變密碼之間相距的最大天數設為“最大天數”
-R, --root CHROOT_DIR chroot 到的目錄
-W, --warndays 警告天數 将過期警告天數設為“警告天數”
[root@server0 ~]# chage -E 2018/01/31 tom
[root@server0 ~]# chage -l tom
最近一次密碼修改時間 :1月 02, 2018
密碼過期時間 :從不
密碼失效時間 :從不
帳戶過期時間 :1月 31, 2018
兩次改變密碼之間相距的最小天數 :0
兩次改變密碼之間相距的最大天數 :99999
在密碼過期之前警告的天數 :7
與使用者相關的配置問檔案
/etc/passwd
/etc/shadow
/etc/login.defs #添加系統使用者的預設配置檔案
/etc/skel/ #存儲使用者系統環境配置檔案
[root@test ~]# ls /etc/skel/ -a
. .. .bash_logout .bash_profile .bashrc .mozilla
[root@test ~]# useradd yy1
[root@test ~]# ls /home/yy1/ -a
. .. .bash_logout .bash_profile .bashrc .mozilla
[root@test ~]# touch /etc/skel/test.txt
[root@test ~]# useradd yy2
[root@test ~]# ls -a /home/yy2/
. .. .bash_logout .bash_profile .bashrc .mozilla test.txt
基本權限的類型
通路方式(權限)
讀取:允許檢視内容 -read r
寫入:允許修改内容 -write w
可執行:允許運作和切換 -execuit x
對于文本檔案:
r: cat head tail less
w: vim
x: 運作
權限适用對象(歸屬)
所有者:擁有此檔案/目錄的使用者-user u
所屬組:擁有此檔案/目錄的組-group g
其他使用者:除所有者,所屬組以外的使用者-other o
[root@server0 ~]# ls -ld /etc/
drwxr-xr-x. 133 root root 8192 10月 30 10:14 /etc/
[root@server0 ~]# ls -l /etc/passwd
-rw-r--r--. 1 root root 2089 10月 30 10:14 /etc/passwd
[root@server0 ~]# ls -l /etc/rc.local
lrwxrwxrwx. 1 root root 13 5月 7 2014 /etc/rc.local -> rc.d/rc.local
權限位 硬連接配接數 屬主 屬組 大小 最後修改時間 檔案/目錄名稱
權限位(9位)
檔案類型(1) User(屬主)(3) Group(屬組)(3) Other(其他人)(3)
檔案類型
-
代表普通檔案 d 代表目錄 l 代表連結檔案
三位一組分别為 所有者權限,所屬組權限,其他賬戶權限
設定基本權限
chmod (隻root能用)
chmod 歸屬關系+-=權限類别 文檔
-R 遞歸将權限應用于所有的子目錄與子檔案
[root@server0 ~]# chmod u-x /nsd01/
[root@server0 ~]# ls -ld /nsd01/
drw-r-xr-x. 2 root root 6 10月 30 10:41 /nsd01/
[root@server0 ~]# chmod g+w /nsd01/
[root@server0 ~]# ls -ld /nsd01/
drw-rwxr-x. 2 root root 6 10月 30 10:41 /nsd01/
[root@server0 ~]# chmod u=rwx,g=rx,o=r /nsd01/
[root@server0 ~]# ls -ld /nsd01/
drwxr-xr--. 2 root root 6 10月 30 10:41 /nsd01/
如何判斷使用者具備權限
1.判斷使用者所屬的身份 是否是所有者 是否在所有組 所有者>所屬組 >其他人 比對及停止
2.看相應權限位的權限劃分
目錄的r 權限:能夠ls 浏覽此目錄内容
目錄的w 權限:能夠執行 rm/mv/cp/mkdir/touch/..等更改目錄内容的操作
目錄的x 權限:能夠cd切換到此目錄
設定檔案歸屬
使用chown指令
chown [所有者][:[所屬組]] 檔案或目錄
[root@server0 /]# chown :tedu /nsd02
[root@server0 /]# ls -ld /nsd02
drwxr-xr-x. 2 root tedu 6 10月 30 11:42 /nsd02
[root@server0 /]# useradd dc
[root@server0 /]# chown dc:tedu /nsd02
[root@server0 /]# ls -ld /nsd02
drwxr-xr-x. 2 dc tedu 6 10月 30 11:42 /nsd02
[root@server0 /]# chown student /nsd02
[root@server0 /]# ls -ld /nsd02
drwxr-xr-x. 2 student tedu 6 10月 30 11:42 /nsd02
特殊權限(附權重限)
Set UID
附加在屬主的x位上
屬主的權限辨別會變成s
适用于可執行檔案,Set UID 可以讓使用者具有檔案屬組的身份及部分權限
(傳遞所有者身份) 如 手持尚方寶劍
[root@server0 /]# cp /usr/bin/touch /usr/bin/hahach
[root@server0 /]# chmod u+s /usr/bin/hahach
[root@server0 /]# ls -l /usr/bin/hahach
-rwsr-xr-x. 1 root root 62432 10月 30 14:14 /usr/bin/hahach
[root@server0 /]# su - dc
[dc@server0 ~]$ touch a.txt
[dc@server0 ~]$ hahach b.txt
[dc@server0 ~]$ ls -l
total 0
-rw-rw-r--. 1 dc dc 0 Oct 30 14:15 a.txt
-rw-rw-r--. 1 root dc 0 Oct 30 14:15 b.txt
Set GID
附加在屬組的x位
屬組的權限辨別變成為s
适用于可執行檔案,功能于Set UID類似
适用于目錄,Set GID可以是目錄下新增的文檔自動設定與父目錄相同的屬組
為了使在這個目錄下新增的檔案或目錄的所屬組和父目錄相同(隻針對未來生成的檔案)
[root@server0 /]# ls -l /test/1.txt
-rw-r--r--. 1 root root 0 10月 30 14:37 /test/1.txt
[root@server0 /]# chown :tedu /test/
[root@server0 /]# chmod g+s /test/
[root@server0 /]# ls -ld /test
drwxr-sr-x. 2 root tedu 18 10月 30 14:37 /test
[root@server0 /]# mkdir /test/testone
[root@server0 /]# ls -l /test/
-rw-r--r--. 1 root root 0 10月 30 14:37 1.txt
drwxr-sr-x. 2 root tedu 6 10月 30 14:39 testone
Sticky Bit
附加在其他人的x位
其他人的權限辨別變成為t
适用于開放w 權限的目錄,可以阻止使用者濫用w 寫入權限
(禁止操作别人的文檔)
[root@server0 /]# chmod u+w,g+w,o+w /public/
[root@server0 /]# chmod o+t /public/
[root@server0 /]# ls -ld /public/
drwxrwxrwt. 2 root root 6 10月 30 14:43 /public/
[zhangsan@server0 /]$ ls -l /public/
-rw-rw-r--. 1 dc dc 0 Oct 30 14:53 dc.txt
-rw-rw-r--. 1 test test 0 Oct 30 14:52 test.txt
-rw-rw-r--. 1 zhangsan zhangsan 0 Oct 30 14:52 zhangsan.txt
[zhangsan@server0 /]$ rm -rf /public/test.txt
rm: cannot remove ‘/public/test.txt’: Operation not permitted
[dc@server0 ~]$ cd /NB/
bash: cd: /NB/: Permission denied
[root@server0 /]# ls -ld /NB/
drwxrwx---. 2 root cw 6 10月 30 15:27 /NB/
[root@server0 /]# setfacl -m u:dc:rx /NB/
[root@server0 /]# ls -ld /NB/
drwxrwx---+ 2 root cw 6 10月 30 15:27 /NB/
[root@server0 /]# getfacl /NB/
getfacl: Removing leading '/' from absolute path names
# file: NB/
# owner: root
# group: cw
user::rwx
user:dc:r-x
group::rwx
mask::rwx
other::---
[dc@server0 ~]$ cd /NB/
[dc@server0 NB]$
例如:
[root@room1pc32 桌面]# mkdir /test01
[root@room1pc32 桌面]# ls -ld /test01
drwxr-xr-x. 2 root root 6 11月 11 09:26 /test01
[root@room1pc32 桌面]# chmod 007 /test01
[root@room1pc32 桌面]# ls -ld /test01
d------rwx. 2 root root 6 11月 11 09:26 /test01
[root@room1pc32 桌面]# chmod 777 /test01
[root@room1pc32 桌面]# ls -ld /test01
drwxrwxrwx. 2 root root 6 11月 11 09:26 /test01
[root@room1pc32 桌面]# chmod 4750 /test01/
[root@room1pc32 桌面]# ls -ld /test01/
drwsr-x---. 2 root root 6 11月 11 09:26 /test01/