日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

【CyberSecurityLearning 30】Linux操作系统的用户和组、文件及目录权限

發布時間:2025/3/15 linux 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CyberSecurityLearning 30】Linux操作系统的用户和组、文件及目录权限 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Linux簡介

Linux 發展歷史

Linux系統誕生于1991年,由芬蘭大學李納斯(Linus Torvalds)和后來陸續加入的眾多愛好者共同開發完成,是UNIX的分支。

Linux是開源軟件,源代碼開發的UNIX

Linux Logo

Linux 內核版本

Linux內核官網:www.kernel.org

內核版本說明

2.6.8(2 主版本 6 次版本 8 末版本)

內核版和開發版區別

內核版(幾十M)是基礎,開發版是在內核的基礎上加入自己的桌面、應用程序,也就是說開發版每個公司都能開發,但是他們用的內核都是一樣的。

Linux主要的發行版本

?

主要的區別 軟件安裝,其他的基本一致

開源軟件

開源軟件特點,絕大多數開源軟件免費,可以獲得軟件源代碼(安全),可以自由傳播、改良甚至銷售

支撐互聯網的開源技術

LAMP

Linux 操作系統

Apache Web服務器

MySQL 數據庫

PHP 編程語言

Linux應用

基于Linux的企業服務器

www.netcraft.com (通過該網站我們可以查詢各大網站服務器所使用的系統,嗅探、踩點)

嵌入式應用(手機、平板)

電影娛樂行業A

用戶和組

Linux操作系統中的用戶分類

普通用戶? 權限比管理員低? 也可以登錄系統

root超級管理員

用戶的分類和組

/etc/passwd???? 保存了操作系統中所有用戶的信息

root : x : 0 : 0 : root : /root : /bin/bash HSP : x : 500 : 500 : : /home/HSP : /bin/bash 字段1:用戶名稱 字段2:密碼占位符 字段3:用戶的uid (0表示超級用戶,500-60000表示普通用戶,1-499表示程序用戶)程序用戶不允許登錄系統 字段4:基本組的gid(先有組才有用戶,先建立組再建立用戶) 字段5:用戶信息記錄字段(這個字段基本上廢棄了) 字段6:用戶的家目錄 字段7:用戶登錄系統后使用的命令解釋器

/etc/shadow?? 保存了用戶的密碼信息

root:$6$Lx3szebY4fAW2wFq$nI19XWtQZosCU0yoCyD05Qw7AHSwfJi0uh27mrwVhmtWo1IcspLDIHaSDp1FLqxuwCvV27mE6wZqyha2q4JeC1:18673:0:99999:7:::HSP:$6$N8BBW6c7uOPtmjZI$7o0ycutUSP/n7XgAPZ8WFdDrV8yRmTu6htR.qbk20z5AXWPXKq20XXCR4uGJPmkVkIzJNSyt0vXSYgnDVl0S31:18673:0:99999:7:::字段1:用戶名 字段2:用戶密碼加密后的字符串(sha加密) 字段3:距離1970年1月1日密碼最近一次的修改時間(UNIX誕生時間) 字段4:密碼的最短有效期(如果寫3表示用戶三天內不可以修改密碼,0就是不限制) 字段5:密碼的最長有效期(200多年,不建議這么長,建議設置為90天) 字段6:密碼過期前7天警告 字段7:密碼的不活躍期 字段8:用戶的失效時間



/etc/group?? 系統中所有組信息

建立及調整用戶屬性

注:我們這里通過幾個場景的配置,學習和了解用戶屬性的建立及調整
linux操作系統里面是先有組才有用戶的,所以要先建立組

1、建立一個名為class1的組,組id為1000,class2的組id為2000

[root@Waffle Desktop]# groupadd class1 建立組 [root@Waffle Desktop]# cat /etc/group class1:x:501: [root@Waffle Desktop]# groupmod -g 1000 class1 對class1重新修改組id [root@Waffle Desktop]# cat /etc/group class1:x:1000: [root@Waffle Desktop]# groupadd -g 2000 class2 新建class2,指定組id class2:x:2000:

2、建立tom用戶要求其基本組是class1組,附加組為class2組,tom用戶的uid為600

注:建立用戶時如果不指定基本組,系統會直接指定出一個tom組

[root@Waffle Desktop]# useradd -g class1 tom 新建tom用戶 這里寫class1也行寫1000也行 [root@Waffle Desktop]# id tom 觀察tom用戶當前信息 uid=501(tom) gid=1000(class1) groups=1000(class1)[root@Waffle Desktop]# user 按tab后可以查看user開頭的命令 useradd userdel userformat userhelper userinfo usermod usermount usernetctl userpasswd users [root@Waffle Desktop]# usermod -G 2000 -u 600 tom 這里2000寫class2也行 指定基本組是-g,-G是指定附加組 -u指定uid [root@Waffle Desktop]# id tom uid=600(tom) gid=1000(class1) groups=1000(class1),2000(class2)

3、建立一個程序用戶uid為250用戶名為testuser沒有家目錄
程序用戶特點:不能登錄操作系統、沒有家目錄

[root@Waffle Desktop]# useradd -u 250 -M -s /sbin/nologin testuser -M表示沒有家目錄 程序用戶不能登錄系統:-s /sbin/nologin(/sbin/nologin代表這個用戶登錄時使用的命令解釋器,這個就是不允許登錄的命令)[root@Waffle Desktop]# id testuser uid=250(testuser) gid=2001(testuser) groups=2001(testuser)[root@Waffle Desktop]# su - testuser 驗證不能登錄系統 su: warning: cannot change directory to /home/testuser: No such file or directory This account is currently not available. [root@Waffle Desktop]# cd /home/ 驗證沒有家目錄 [root@Waffle home]# ls HSP tom

4、為tom用戶設定密碼為123,并設定密碼最長有效期為90天,將用戶密碼進行鎖定使其無法登陸系統
注:只有超級管理員才能為指定用戶設置密碼
?

[root@Waffle home]# passwd tom Changing password for user tom. New password: BAD PASSWORD: it is WAY too short BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully.[root@Waffle home]# cat /etc/shadow 驗證 testuser:!!:18683:0:99999:7:::[root@Waffle home]# man chage -M, --maxdays MAX_DAYSSet the maximum number of days during which a password is valid. When MAX_DAYS plus LAST_DAY is less than thecurrent day, the user will be required to change his/her password before being able to use his/her account.This occurrence can be planned for in advance by use of the -W option, which provides the user with advancewarning.Passing the number -1 as MAX_DAYS will remove checking a password′s validity.[root@Waffle home]# chage -M 90 tom -M后跟密碼最長有效期 [root@Waffle home]# id tom uid=600(tom) gid=1000(class1) groups=1000(class1),2000(class2) [root@Waffle home]# passwd -S tom -S查看狀態 tom PS 2021-02-25 0 90 7 -1 (Password set, SHA512 crypt.)[root@Waffle Desktop]# passwd -l tom -l鎖定tom用戶 Locking password for user tom. passwd: Success [root@Waffle Desktop]# passwd -S tom tom LK 2021-02-25 0 90 7 -1 (Password locked.)[root@Waffle Desktop]# passwd -u tom -u表示unlock 解鎖 Unlocking password for user tom. passwd: Success

?

5、刪除tom用戶和testuser用戶,刪除class1組和class2組

-r的目的是刪除這個用戶信息的時候連同它的家目錄一塊刪,否則他不會刪除它的家目錄和它的文件[root@Waffle Desktop]# userdel -r testuser userdel: testuser home directory (/home/testuser) not found testuser本來就沒有家目錄 [root@Waffle Desktop]# user -r tom bash: user: command not found [root@Waffle Desktop]# userdel -r tom [root@Waffle Desktop]# id tom id: tom: No such user [root@Waffle Desktop]# groupdel class1 [root@Waffle Desktop]# groupdel class2

調整文件及目錄權限

權限? 文件或者目錄屬于誰,屬于哪個組,不同的用戶能對該文件進行什么樣的操作。

現在在tmp目錄下建立兩個文件(一個是文件一個是目錄)

[root@Waffle Desktop]# vim /tmp/test.txt [root@Waffle Desktop]# mkdir /tmp/testdir [root@Waffle Desktop]# cd /tmp [root@Waffle tmp]# ls keyring-L37Uzg virtual-root.IpFBks vmware-fonts-2554.0 orbit-gdm virtual-root.qVMQEf vmware-root orbit-root vmware-config-11898.0 vmware-root_1301-4248680502 pulse-BULfnWe2TdkQ vmware-config-22916.0 vmware-root_1646-591958445 pulse-M80xbZuu61w5 vmware-config-2554.0 vmware-root_22728-735758538 pulse-ponS681MlrXk VMwareDnD yum.log testdir vmware-file-mod-11995.0 test.txt vmware-file-mod-1578.0 [root@Waffle tmp]# ls -l test.txt -rw-r--r--. 1 root root 36 Feb 25 11:44 test.txt [root@Waffle tmp]# ls -ld testdir 看目錄要加d,因為目錄沒辦法直接看它的權限,如果不加d是看它里面內容 drwxr-xr-x. 2 root root 4096 Feb 25 11:44 testdir [root@Waffle tmp]# 文件:-rw-r--r--. 1(節點) root(所屬者) root(所屬組) test.txt(前面那個root是用戶名,表示這個文件屬于哪個用戶,后面那個root是組名,表示這個文件屬于哪個組) 目錄:drwxr-xr-x. 2(文件中的子目錄數) root root testdir.表示這個文件只受到selinux文件程序管理

?

-? rw-?? r-- ? r--

d? rwx? r-x? r-x

字段1:文件類型? ?-表示普通文件??? d表示目錄?? l表示符號鏈接??? b表示塊設備(比如硬盤)
字段2:文件所屬者對該文件的權限
字段3:文件所屬組的權限
字段4:其他用戶的權限(既不是文件的所有者也不是文件所屬組中的用戶)

?? ? ? ? ? ? ? ?r? ? ? ? ? ? ? w? ? ? ? ? ? ?x文件目錄
? ? read讀取文件? ? ? write寫入文件? ? ?可執行權限
? 可以查看目錄文件? ? ? 可以增刪文件? ?? 可以進入目錄

?

?

?

?

chmod 修改權限

chmod 對象? 算數運算符? 權限? 文件

對象:u(所屬者)?g(所屬組) o(其他用戶的權限) a(all)?

算數運算符:-?? + ? =

權限:r w x(讀? 寫? 執行)
例:chmod? o-r? /tmp/test.txt

改變文件所屬者為tom,所屬組為tom組
chown?? chgrp
chown? 用戶? 文件
chgrp?? 組????? 文件
?

[root@Waffle tmp]# chmod o-r /tmp/test.txt [root@Waffle tmp]# ll /tmp/test.txt -rw-r-----. 1 root root 36 Feb 25 11:44 /tmp/test.txt [root@Waffle tmp]# chown tom /tmp/test.txt [root@Waffle tmp]# chgrp tom /tmp/test.txt [root@Waffle tmp]# ll test.txt -rw-r-----. 1 tom tom 36 Feb 25 11:44 test.txt

8進制賦權法:

rwx rw- r--? 764

rwxr-----? ?740

用法:

chmod 740 文件名

注:只需記三個,其他的用這三個相加即可。

0000- - -
1001- - x
2010- w -
3011- w x
4100r - -
5101r - x
6110r w -
7111r w x

?

?

?

?

?

?

?

?

?

改變文件的所屬者,所屬組(chown,chgrp)

chown 用戶 文件

chgrp 組 文件

粘滯位、sgid權限、suid權限

粘滯位針對目錄進行賦權,目錄中創建的文件只有建立者可以刪除

創建tom和Jerry兩個用戶: [root@Waffle Desktop]# useradd tom [root@Waffle Desktop]# useradd jerry創建文件夾并賦權: [root@Waffle Desktop]# mkdir /tmp/test [root@Waffle Desktop]# chmod 777 /tmp/test [root@Waffle Desktop]# ll -d /tmp/test drwxrwxrwx. 2 root root 4096 Feb 25 16:10 /tmp/test切換到tom用戶 在tmp目錄的test文件夾下創建tom.txt文件: [root@Waffle Desktop]# su tom [tom@Waffle Desktop]$ cd /tmp/test [tom@Waffle test]$ touch tom.txt [tom@Waffle test]$ ll total 0 -rw-rw-r--. 1 tom tom 0 Feb 25 16:11 tom.txt [tom@Waffle test]$ 登錄Jerry用戶,問這個文件Jerry能不能刪?可以 [tom@Waffle test]$ exit exit [root@Waffle Desktop]# su jerry [jerry@Waffle Desktop]$ cd /tmp/test [jerry@Waffle test]$ ls tom.txt [jerry@Waffle test]$ touch jerry.txt [jerry@Waffle test]$ ls jerry.txt tom.txt [jerry@Waffle test]$ rm -rf jerry.txt [jerry@Waffle test]$ rm -rf tom.txt [jerry@Waffle test]$ ls賦粘滯位:針對目錄賦權,目錄中創建的文件只有創建者可以刪除 [root@Waffle Desktop]# cd /tmp/ [root@Waffle tmp]# chmod o+t test [root@Waffle tmp]# ll -d test drwxrwxrwt. 2 root root 4096 Feb 25 16:16 test [root@Waffle tmp]# su tom [tom@Waffle tmp]$ cd test [tom@Waffle test]$ touch tom.txt [tom@Waffle test]$ exit exit [root@Waffle tmp]# su jerry [jerry@Waffle tmp]$ cd test [jerry@Waffle test]$ ls tom.txt [jerry@Waffle test]$ rm -rf tom.txt rm: cannot remove `tom.txt': Operation not permitted [jerry@Waffle test]$ touch jerry.txt [jerry@Waffle test]$ ls jerry.txt tom.txt [jerry@Waffle test]$ rm -rf jerry.txt [jerry@Waffle test]$

sgid針對目錄建立的權限,在該目錄建立的文件所屬組繼承父目錄的屬組

[root@Waffle tmp]# chmod g+s test [root@Waffle tmp]# ll -d test drwxrwsrwt. 2 root root 4096 Feb 25 16:20 test [root@Waffle tmp]# su tom [tom@Waffle tmp]$ cd test [tom@Waffle test]$ ls tom.txt [tom@Waffle test]$ rm -rf tom.txt [tom@Waffle test]$ touch tom [tom@Waffle test]$ ll total 0 -rw-rw-r--. 1 tom root 0 Feb 25 16:26 tom 可以看到這個文件的所屬組變成root(所屬組會繼承父目錄的所屬組)

suid 對可執行文件建立。如果建立了suid,運行這個進程或者運行這個程序的用戶就會繼承這個文件所屬者的權限
誰運行改文件就具有該文件所屬者的權限

[root@Waffle tmp]# su tom [tom@Waffle tmp]$ cd /etc [tom@Waffle etc]$ ll /etc/passwd -rw-r--r--. 1 root root 1685 Feb 25 16:09 /etc/passwd (TOM能看passwd文件) [tom@Waffle etc]$ ll /etc/shadow ----------. 1 root root 1092 Feb 25 16:09 /etc/shadow (什么權限都沒有)權限 限制不了超級管理員[root@Waffle tmp]# which vim /usr/bin/vim [root@Waffle tmp]# ll /usr/bin/vim -rwxr-xr-x. 1 root root 1847752 Apr 5 2012 /usr/bin/vim [root@Waffle tmp]# chmod u+s /usr/bin/vim [root@Waffle tmp]# ll /usr/bin/vim -rwsr-xr-x. 1 root root 1847752 Apr 5 2012 /usr/bin/vim [root@Waffle tmp]# su tom [tom@Waffle tmp]$ vim /etc/shadow 查看后把waffle的密碼刪了,:wq!退出(雖然還是沒什么權限,但他依靠root的權限完成了刪除) [tom@Waffle tmp]$ ll /etc/shadow ----------. 1 root root 986 Feb 25 16:42 /etc/shadow[root@Waffle tmp]# ll -d test drwxrwsrwt. 2 root root 4096 Feb 25 16:26 test [root@Waffle tmp]# chmod g-s,o-t test 撤銷粘滯位,suid,sgid的方法 [root@Waffle tmp]# ll -d test drwxrwxrwx. 2 root root 4096 Feb 25 16:26 test

安全權限

1、不再允許添加新用戶的請求

/etc/group

/etc/passwd

/etc/shadow

/home/xxxx
不允許再變動---鎖上

chattr?? +i?? 文件(解鎖只要把+i變成-i即可)

2、umask??

0022

目錄的最高權限 0777-0022=0755

文件的最高權限 666-002=644(x一般不賦予,所以是666)

為什么umask里目錄是022,文件是002,原因在下列文件里,里面有一個if語句控制著

/etc/profile /etc/bashrc

3、修改默認的密碼最長有效期

vim /etc/login.defs

?

總結

以上是生活随笔為你收集整理的【CyberSecurityLearning 30】Linux操作系统的用户和组、文件及目录权限的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。