天天看點

使用者和權限管理-RHCEday2

使用者、權限管理

使用者管理 

使用者相關檔案:

/etc/passwd:存儲使用者資訊 

格式:

使用者登入名:密碼占位符:uid:gid:描述資訊:宿主目錄:SHELL

/etc/shadow:存儲使用者密碼資訊

使用者:

基本組

附加組

相關操作:

1、建立使用者

# useradd [選項] <username>

選項:

-u <uid> 指定使用者uid 

-g <gid> 指定使用者的基本組

-G <gid> 指定使用者的附加組

-s <shell> 指定使用者的SHELL名稱,預設為/bin/bash

-M 不建立使用者宿主目錄

-d <dir_name> 指定使用者的宿主目錄

-e <YYYY-MM-DD> 指定使用者的過期時間

-c <comment> 指定使用者的描述資訊

2、設定使用者密碼

# passwd [選項] [username]

選項: 

-S   檢視使用者密碼狀态

-l 鎖定使用者密碼

-u 解鎖使用者密碼

-f:強制

-e 強制使用者密碼過期

3、修改使用者的屬性

# usermod [選項] <username>

選項: 

-u <uid>

-g <gid>

-G <gid>

-aG:用于為使用者指定多個附加組

-s <shell>

-L 鎖定使用者密碼

-U 解鎖使用者密碼

-l 更改使用者名

4、删除使用者

# userdel [-r] <username>

-r:同時删除使用者宿主目錄

5、檢視使用者

# id [選項] <username> 

# id userA 

# id -u userA 

# id -g userA 

# id -G userA 

# id -n -u userA 

# id -n -g userA 

# id -n -G userA 

每建立一個使用者,系統會自動從/etc/skel目錄下複制環境變量配置檔案到使用者的宿主目錄

.bashrc:使用者登入系統時

.bash_profile:使用者在打開一個新終端 

.bash_logout:使用者登出時

/etc/bashrc 

/etc/profile

使用者組:

/etc/group:存儲使用者組資訊

/etc/gshadow:存儲使用者組密碼資訊

1、建立使用者組

# groupadd [-g <number>] <groupname>

2、删除使用者組

# groupdel <groupname>

重置root使用者密碼:

1、重新開機系統,在grub菜單按上、下鍵停止,選擇第一項按字母"e"進入編輯啟動配置檔案

2、找到linux16開頭的行,在行末添加rd.break console=tty0;按ctrl+x再次啟動

3、以讀寫的方式重新挂載根目錄,并以/sysroot為根

# mount -o remount,rw /sysroot 

# chroot /sysroot 

4、使用passwd指令重置root使用者密碼

5、在根目錄下建立隐藏檔案,以便重置檔案的SELINUX安全上下文 

# touch /.autorelabel

6、兩次exit退出并重新啟動

權限管理 

正常權限:r讀取  4, w寫入   2 , x執行   1

檔案:

r:檢視檔案内容(cat/more/less/head/tail)

w:檢視、修改檔案内容(vim)

x:SHELL腳本/python腳本

目錄:

r:檢視目錄中檔案(ls /test)

w:檢視、修改目錄下的檔案

x:可切換到該目錄(cd )

檢視檔案權限:

# ls -l /1.txt 

-rw-r--r--. 1 tom caiwu 76 Apr 15 23:38 /1.txt

檢視目錄權限

# ls -ld /etc/

drwxr-xr-x. 134 root root 8192 Apr 15 23:45 /etc/

修改檔案/目錄權限:

1、修改權限---chmod 

1) # chmod {augo}{+-=}{rwx} <filename> 

a:all所有使用者

u:user 屬主

g:group 屬組

o:other 其他使用者

# chmod g+wx /tmp/1.txt

# chmod u+r,g-x,g+r /tmp/1.txt

2) # chmod nnn <filename> 

# chmod 600 /tmp/1.txt

4 r

2 w

1 x

rwx = 4+2+1 = 7

2、修改檔案所屬-----chown 

# chown <user>.<group> <filename>

# chown tom.caiwu /caiwu/1.txt 

# chown tom /caiwu/2.txt 

# chown tom. /caiwu/3.txt 

# chown .caiwu /caiwu/4.txt

專用于修改屬組的指令:

# chgrp tom /caiwu/4.txt

共同選項:

-R 遞歸修改

# chmod -R 700 /caiwu/

特殊權限:

suid, sgid, sticky bit 

1、suid 4

針對指令、腳本設定

當一個指令擁有suid權限時,普通使用者在執行指令的過程中,會臨時擷取到指令屬主使用者的權限 

# ls -l /usr/bin/passwd 

-rwsr-xr-x. 1 root root 27832 Jan 29  2014 /usr/bin/passwd

設定suid權限

# chmod u+s <filename>

2、sgid 2

針對目錄進行設定

當一個目錄擁有sgid時,在目錄下建立檔案,檔案會自動繼承目錄的屬組

# ls -ldh /test/

drwxr-sr-x. 2 root caiwu 42 Apr 16 01:54 /test/

設定sgid權限

# chmod g+s /test 

3、sticky bit 1

針對目錄進行設定

當一個目錄擁有sticky bit權限時,目錄下的檔案隻有檔案屬主、目錄屬主、root使用者可以删除 

# ls -ld /tmp/

drwxrwxrwt. 9 root root 4096 Apr 16 02:00 /tmp/

設定sticky bit 權限

# chmod o+t /test 

/project 

經理:jl

員工:tom,jerry 

facl------檔案通路控制清單 

1、設定facl權限 

# setfacl -m u:<username>:<rwx>  <filename> 

# setfacl -m g:<groupname>:<rwx>  <filename> 

2、檢視facl權限

# getfacl /1.txt

getfacl: Removing leading '/' from absolute path names

# file: 1.txt

# owner: user1

# group: user2

user::rw-

user:user4:r--

group::r-x

mask::r-x

other::rwx

3、删除facl權限

# setfacl -x u:<username> <filename>

# setfacl -x g:<groupname> <filename>