1. 程式人生 > >Linux基礎之許可權操作

Linux基礎之許可權操作

Linux許可權管理是Linux中一個十分重要的概念,也是系統安全性的重要保障。這裡主要介紹Linux的基本許可權和預設許可權,通過理論講解與實驗演示,可以詳細瞭解到許可權的相關操作及其重要性。

檔案許可權

[[email protected] ~]# ls -l /etc/passwd
-rw-r–r–. 1 root root 2133 Apr 16 11:33 /etc/passwd
[[email protected] ~]#

檔案的許可權:所有者,所屬組,其他人
rwx,讀、寫、執行,沒有許可權就是“-”
第一個組rwx:檔案所有者的許可權
第二個組rwx:檔案所屬組的許可權
第三個組rwx:檔案其他人的許可權

目錄的許可權:linux許可權管理
r:具有讀取目錄結構列表的許可權,可以檢視目錄下有哪些檔案
w:該許可權對於目錄來說是很大的,
1、可以在該目錄下新建新的檔案和目錄
2、可以刪除已經存在的檔案和目錄
3、將已經存在的檔案和目錄重新命名
4、移動目錄內的檔案和目錄的位置
x:是否可以進入該目錄

修改許可權

chown命令:修改檔案的擁有者,前提是要有該擁有者(-R 遞迴修改)

[[email protected] tmp]# tail -1 /etc/passwd
linux00:x:1001:1001::/home/linux00:/bin/bash
[[email protected] ~]# cd /tmp/
[

[email protected] tmp]# touch file1
[[email protected] tmp]# ll file1
-rw-r–r–. 1 root root 0 Apr 26 08:56 file1
[[email protected] tmp]# chown linux00 file1
[[email protected] tmp]# ll file1
-rw-r–r–. 1 linux00 root 0 Apr 26 08:56 file1
[[email protected] tmp]#

chgrp命令:修改檔案所屬組,前提是要有該組(-R 遞迴修改)

[[email protected] tmp]# tail -1 /etc/group
xiuyuan:x:1002:
[[email protected] tmp]# ll file1
-rw-r–r–. 1 linux00 root 0 Apr 26 08:56 file1
[[email protected] tmp]# chgrp raoxiuyuan file1
[[email protected] tmp]# ll file1
-rw-r–r–. 1 linux00 xiuyuan 0 Apr 26 08:56 file1
[[email protected] tmp]#

chmod命令:修改擁有者和所屬組的許可權
● 加減法:u/g/o +/-/= r/w/x
● 數字法:r=4,w=2,x=1

[[email protected] tmp]# ll file1
-rw-r–r–. 1 linux00 root 0 Apr 26 08:56 file1
[[email protected] tmp]# chmod g+w file1
[[email protected] tmp]# ll file1
-rw-rw-r–. 1 linux00 root 0 Apr 26 08:56 file1
[[email protected] tmp]# chmod g-w file1
[[email protected] tmp]# ll file1
-rw-r–r–. 1 linux00 root 0 Apr 26 08:56 file1
[[email protected] tmp]# chmod o=rwx file1
[[email protected] tmp]# ll file1
-rw-r–rwx. 1 linux00 root 0 Apr 26 08:56 file1
[[email protected] tmp]# chmod 777 file1
[[email protected] tmp]# ll file1
-rwxrwxrwx. 1 linux00 root 0 Apr 26 08:56 file1
[[email protected] tmp]# chmod 764 file1
[[email protected] tmp]# ll file1
-rwxrw-r–. 1 linux00 root 0 Apr 26 08:56 file1
[[email protected] tmp]#

預設許可權

umask:檢視當前使用者的umask許可權;-S選項
0022—拿走的許可權
第一個數字表示特殊許可權
022=rwxr-xr-x

預設建立檔案和目錄的許可權,檔案回拿走x許可權。

臨時的修改:umask 0000
永久的修改:/etc/bashrc(不建議)

普通使用者:002
root使用者:022

[[email protected] tmp]# umask //檢視使用者的預設許可權(Linux許可權管理)
0022
[[email protected] tmp]# touch f1
[[email protected] tmp]# mkdir dir1
[[email protected] tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 May 15 02:59 dir1 //建立的目錄
-rw-r--r--. 1 root root 0 May 15 02:59 f1 //建立的檔案,預設沒有x許可權
[[email protected] tmp]# umask
0022
[[email protected] tmp]# umask 000 //修改使用者的umask
[[email protected] tmp]# umask
0000
[[email protected] tmp]# touch f2
[[email protected] tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 May 15 02:59 dir1
-rw-r--r--. 1 root root 0 May 15 02:59 f1
-rw-rw-rw-. 1 root root 0 May 15 03:01 f2
[[email protected] tmp]# chmod u+x f2
[[email protected] tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 May 15 02:59 dir1
-rw-r--r--. 1 root root 0 May 15 02:59 f1
-rwxrw-rw-. 1 root root 0 May 15 03:01 f2
[[email protected] tmp]# mkdir dir2
[[email protected] tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 May 15 02:59 dir1
drwxrwxrwx. 2 root root 6 May 15 03:03 dir2
-rw-r--r--. 1 root root 0 May 15 02:59 f1
-rwxrw-rw-. 1 root root 0 May 15 03:01 f2
[[email protected] tmp]# umask 022
[[email protected] tmp]# umask
0022
[[email protected] tmp]# vim /etc/bashrc //永久修改umask
[[email protected] tmp]#
-S:以符號方式輸出許可權掩碼

[[email protected] tmp]# umask -S
u=rwx,g=rx,o=rx
[[email protected] tmp]#

問:假設你的umask為003,請問該umask情況下,建立的檔案和目錄的許可權分別為多少?
答:umask為003,所以拿掉的許可權為——–wx,因此:
檔案:(-rw-rw-rw-)-(——–wx)=(-rw-rw-r–)
目錄:(drwxrwxrwx)-(d——-wx)=(drwxrwxr–)問:在什麼情況下,要使用umask
答:假設你和同時在同一個目錄下進行同一個專案的工作。如果使用預設許可權022,那麼你建立的檔案,你的同事將無法編輯。因此我們可以將許可權調整為002

檔案的特殊許可權(SUID、SGID、SBIT)

[[email protected] ~]# ls -ld /tmp/; ls -l /usr/bin/passwd
drwxrwxrwt. 11 root root 4096 May 15 03:38 /tmp/
-rwsr-xr-x. 1 root root 27832 Jan 29 2014 /usr/bin/passwd
[[email protected] ~]#
當s出現在擁有者的x許可權位置時候,表示擁有者有SUID的許可權(Set UID)[Linux許可權管理]
當s出現在所屬組的x許可權位置時候,表示所屬組有SGID的許可權(Set GID)
當t出現在其他人的x許可權位置時候,表示其他人有SBIT的許可權(Sticky Bit)

SUID,臨時獲取檔案所有者的許可權—-只能針對檔案
SGID,即可以作用於目錄,也可以作用於檔案
作用於檔案:SUID一樣
作用於目錄:繼承父級目錄—目錄會不停繼承
SBIT,只有檔案的擁有者,才能刪除,修改該目錄下的檔案—只針對目錄

S和T都有大寫和小寫之分
大寫說明:沒有x許可權
小寫說明:有x許可權
SUID=4 SGID=2 SBIT=1

賬戶管理

/etc/passwd:記錄linux上的所有賬號
/etc/shadow:記錄賬戶對應的密碼
/etc/group:記錄所有的組

[[email protected] ~]# vim /etc/passwd

/etc/passwd每條記錄詳解
linux00:使用者名稱
X:早期這個部分放的是使用者登入密碼,現在密碼放入/etc/shadow中
UID:0表示系統管理員,1-999保留給系統使用的ID,1000以上給一般使用者
GID:0表示系統管理員,1-999保留給系統使用的ID,1000以上給一般使用者
Linux00:使用者資訊說明
/home/linux00:使用者家目錄,使用者登入時,所在的目錄
/bin/bash:使用者在登入的時候,是否可以使用shell,如果不能使用shell,則會顯示/sbin/nologin

賬戶和組名:給使用者看的
UID和GID:linux系統識別的

id命令:檢視當前登入使用者的UID和GID

[[email protected] ~]# id

如果隨意修改/etc/passwd下的使用者UID會發生什麼情況(當我們使用ls -l的時候,系統會根據/etc/passwd和/etc/group檔案的內容,找到UID和GID對應的名稱,修改顯示)

/etc/shadow每條記錄詳解
1.Root:使用者名稱
2.一串紅色的字:經過加密的密碼
3.最近更改過密碼的日期:linux中的日期,是通過1970年1月1日開始累加的日期
4.密碼不能改修改的天數:0表示隨時可以修改
5.密碼需要重新被修改的天數:通過修改該值,可以強制使用者修改密碼
6.密碼需要變更的告警天數:7天哪系統會向用戶發出告警
7.密碼到期還可以使用時間:密碼到期後,賬號可以使用的時間
8.賬號實效日期:通過1970年1月1日開始累加的日期,到了時間後,無論密碼是過期,該賬號已經不能使用
9.保留

/etc/group每條記錄詳解
1.linux00:組名
2.X:組密碼,一般不需要
3.1000:GID
4.組使用者(附屬組)
這些檔案不建議直接進行修改,建議使用命令進行修改。

useradd命令:用於建立使用者

[[email protected] ~]# useradd linux00

使用useradd命令建立使用者後,預設進行一下幾個操作:
1.在/etc/passwd下建立相關的資料
2.在/etc/shadow下寫入密碼的相關的引數
3.在/etc/group中寫入和賬號名一樣的組
4.在/home/下建立使用者的家目錄

usermod命令:可以對已存在賬戶的資訊進行修改

[[email protected] ~]# usermod -l linux00 user1

passwd命令:用於修改密碼(所有人都可以用該命令修改自己的密碼)

[[email protected] ~]# passwd linux00 //修改linux00使用者的密碼

chage命令:可以修改和密碼有關的時間引數(-l:檢視一個賬戶和密碼有關的時間引數)

[[email protected] ~]# chage -l linux00

userdel命令:刪除一個使用者

要徹底刪除一個賬號,比較麻煩:
1.刪除/etc/passwd;/etc/shadow檔案中的內容
2.刪除/etc/group;/etc/gshadow檔案中的內容
3.刪除/home/username;/var/spool/mail/username
4.刪除該使用者曾經來該linux中建立的檔案(先用find命令進行查詢,再刪除)

[[email protected] ~]# userdel linux00 //刪除1、2條中的資訊3、4條中的資訊依然存在
[[email protected] ~]# rm -rf /home/linux00 //手動刪除家目錄
[[email protected] ~]# rm -rf /var/spool/mail/linux00 //手動刪除郵件目錄

-r引數:連同家目錄和郵箱一起刪除

[[email protected] ~]# userdel -r linux00 //1、2、3內容全刪除

ACL許可權

ACL(Access Control List)訪問控制列表,ACL的功能是應對複雜使用者環境的許可權問題。(Linux許可權管理)

那麼這玩意有啥用處呢?下面我們考慮一種場景:

假設我Jimmy建立了一個專案資料夾,我和我的開發團隊Rocket對該目錄均具備 rwx 的許可權,因為我該資料夾下有原始碼等,不能對外開放,所以對其他使用者的許可權為0,即我的資料夾目錄為 drwxrwx—;
然而有一天,我的一個灰常好的盆友Sherry想要看一下我的專案,看看我寫的程式碼有多流弊,然而我不好意思拒絕,這個時候我有三種選擇:

 

1.讓其成為所有者(這個當然不可能)
2.讓其成為專案組成員(然而她並沒有參與開發,再說她也基本看不懂程式碼,萬一不小心刪掉了部分程式碼咋弄?)
3.給其它人賦以 r-x 的許可權(然而,other使用者太多了吧,萬一我程式碼沒釋出就被競爭對手給copy了呢?)
看來,以上三種手段都不靠譜啊,怪就怪other的使用者全太大了,唉,這可咋整?

那麼,我們為什麼不為Sherry開小灶呢?就是讓其不屬於任何一個組,只是以單使用者的身份被賦予特定許可權。
這種“開小灶”的方式,其實就是ACL許可權!

 

ACL可以針對單一使用者、單一檔案或目錄來進行r、w、x的許可權設定,對於需要特殊許可權的使用狀況非常有幫助

setfacl命令: 設定檔案或目錄的ACL設定資訊

[[email protected] tmp]# mkdir -m 770 project //建立其他人無許可權操作的目錄
[[email protected] tmp]# ll -d project/
drwxrwx---. 2 root root 6 May 16 23:39 project/
[[email protected] tmp]# su - linux00 //切換使用者linux00
Last login: Wed May 16 23:37:52 EDT 2018 on pts/0
[[email protected] ~]$ cd /tmp/project/ //檢視管理員建立的目錄/tmp/project/
-bash: cd: /tmp/project/: Permission denied
[[email protected] ~]$ su -
Password:
Last login: Wed May 16 23:38:31 EDT 2018 on pts/0
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# setfacl -m u:linux00:rx project/ //設定acl許可權
[[email protected] tmp]# ll
total 4
drwxrwx---+ 2 root root 6 May 16 23:39 project //目錄許可權第11位有一個 + 號,代表的就是ACL許可權
[[email protected] tmp]# su - linux00
Last login: Wed May 16 23:39:56 EDT 2018 on pts/0
[[email protected] ~]$ cd /tmp/project/ //linux00現在已經有許可權可以訪問project目錄
[[email protected] project]$
getfacl命令: 獲取檔案或目錄的ACL設定資訊

[[email protected] tmp]$ getfacl project/
# file: project/
# owner: root
# group: root
user::rwx
user:linux00:r-x
group::rwx
mask::rwx
other::—

 

[[email protected] tmp]$

user:linux00:r-x: 表示linux00具備的ACL許可權為 r-x
mask::rwx: 表示ACL的最大許可權,用你分配給使用者的許可權與mask相與(類似於子網掩碼)

setfacl -b命令:刪除acl許可權

[[email protected] tmp]# setfacl -b project/
[[email protected] tmp]# getfacl project/
# file: project/
# owner: root
# group: root
user::rwx
group::rwx
other::—

 

[[email protected] tmp]#