亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

公眾號:老油條IT記

 

我們知道,無論什么東西,涉及到安全性的,比如文件、文件夾、磁盤(就如window系統的磁盤,我們就可以通過bitlocker技術將磁盤給加密鎖起來)、服務器,等都需要設置權限管理,以保證安全性,接下來讓我們來探討以下linux的文件權限。

1.權限概述

權限是操作系統用來限制對資源訪問的機制,權限一般分為讀、寫、執行。系統中的每個文件都擁有特定的權限、所屬用戶及所屬組,通過這樣的機制來限制哪些用戶、哪些組可以對特定文件進行什么樣操作。

#Linux中權限基于UGO模型進行控制

u:代表user(用戶)

g:代表group(組)

o:代表other(其他)

#查看權限

[root@ctos3 ~]# ls -ld test

drwxr-xr-- 2 root root 6 Mar 9 01:37 test

#講解:第一個d是文件類型,后面9位3個為一組

掌握Linux文件權限,看這篇就夠了

 

#文件權限說明

文件或目錄的權限位是由9個權限位來控制的,每三位一組,分別是文件屬主(Owner)、用戶組(Group)、其他(Other)用戶的讀、寫、執行

其中

r(read)讀權限, 可以讀取文件內容,可以列出目錄內容 用數字表示為4

w(write)寫權限, 可以修改文件內容,可以在目錄中創建刪除文件 用數字表示為2

x(excute)執行權限,可以作為命令執行,可以訪問目錄內容 用數字表示為1

- 沒有權限, 用數字表示為0

2.修改文件所屬用戶、所屬組

#2.1.使用chown命令改變文件/目錄的所屬用戶

修改格式:

chown 用戶 文件名/目錄名

#例子

將test.txt的所屬用戶從root更改為demo用戶

[root@ctos3 ~]# ls -l test.txt

-rw-r--r-- 1 root root 0 Mar 9 01:36 test.txt

[root@ctos3 ~]# chown demo test.txt #更改

[root@ctos3 ~]# ls -l test.txt

-rw-r--r-- 1 demo root 0 Mar 9 01:36 test.txt

#參數介紹

-R 參數遞歸的修改目錄下的所有文件的所屬用戶

#例子將/test目錄下的所有文件和用戶所屬用戶修改成demo

[root@ctos3 ~]# chown -R demo /test/

[root@ctos3 ~]# ls -l /test/

drwxr-xr-x 3 demo root 16 Mar 9 01:55 aa

#2.2.使用chgrp改變文件/目錄的所屬組

命令格式

chgrp 用戶 文件/目錄名

#例子

[root@ctos3 ~]# chgrp demo /test/

[root@ctos3 ~]# ls -ld /test/

drwxr-xr-x 3 demo demo 16 Mar 9 01:55 /test/

#注意點:一般都是用chown修改用戶和組的了 格式chown -R 用戶.組 + 文件

#2.3.使用chmod命令修改文件/目錄的權限

命令格式

chmod +模式 +文件

模式為如下格式

1.u、g、o、分別代表用戶、組和其他

2.a可以代指ugo

3.+、-代表加入或刪除對應權限

4.r、w、x代表三種權限

#示例

chmod u+rw test.txt #給所屬用戶權限位添加讀寫權限

chmod g+rw test.txt #給所屬組權限位添加讀寫權限

chmod o+rw test.txt #給其他用戶權限位添加讀寫權限

chmod u=rw test.txt #設置所屬用戶權限位的權限位讀寫

chmod a-x test.txt #所有權限為去掉執行權限

#修改權限2

命令chmod也支持以數字方式修改權限,三個權限分別由三個數字表示:

r=4

w=2

x=1

使用數字表示權限時,每組權限分別對應數字之和:

rw=4+2=6

rwx=4+2+1=7

r-x=4+1=5

語法:chmod 755 文件或文件夾名字

例:[root@centos7 ~]# touch test.txt

[root@centos7 ~]# chmod 755 test.txt

3.默認權限

每一個終端都擁有一個umask屬性,來確定新建文件、文件夾的默認權限

/etc/profile文件可以看到設置的umask值

if [ $UID -gt 199 ] && [ "/usr/bin/id -gn" = "/usr/bin/id -un" ]; then

umask 002

else

umask 022

fi

#注釋:如果UID大于199并且用戶的組名和用戶名一樣,umask值為002,否則就為022

#注釋:gt在shell腳本中是大于,id -gn:顯示組名,id -un:顯示用戶名

#UID小于199并且用戶的組名和用戶名一樣

目錄創建的默認權限為777-umask 就是755

文件創建的默認權限為777-umask 就是644

#例子:

#root用戶創建文件,權限為644,uid為0

[root@centos7 ~]# touch test.txt

[root@centos7 ~]# ls -l test.txt

-rw-r--r--. 1 root root 0 Jul 13 00:30 test.txt

#切換到普通用戶創建文件,權限為664,uid大于199

[root@centos7 ~]# su - test

Last login: Mon Jul 13 00:07:25 EDT 2020 on pts/0

[test@centos7 ~]$ touch test1.txt

[test@centos7 ~] ls -l test1.txt

-rw-rw-r--. 1 test test 0 Jul 13 00:31 test1.txt

#可以使用umask查看設置的umask值

[root@ctos3 ~]# umask0022

#如果想要創建的文件權限多少,可以自己定義

[root@ctos3 ~]# umask 035 #設置默認umask為035,創建出來的文件默認權限為642

[root@ctos3 ~]# touch fil035

[root@ctos3 ~]# ls -l fil035

-rw-r---w- 1 root root 0 Mar 9 02:25 fil035

#注意為什么是642,而不是631呢,因為是奇數的話就會加1,從之前默認權限6就會加到7,用7-3就是4,7-5就是2,0為偶數所以還是6,所以為642

[root@ctos3 ~]# umask 022 #設置022,創建文件的權限為644

[root@ctos3 ~]# touch fil022

[root@ctos3 ~]# ls -l fil022

-rw-r--r-- 1 root root 0 Mar 9 02:25 fil022

4.特殊權限

Linux系統中的基本權限位為9位權限,加上3位特殊權限位,共12位權限 文件的特殊權限:suid,sgid,sticky

suid:是針對二進制可執行程序上的,對目錄設置無效suid作用:讓普通用戶可以以root(或其他)的用戶角色運行只有root才能運行的程序或命令suid數字表示為4,在文件所有者權限的第三位為小寫的s,就代表擁有suid屬性

sgid:既可以針對文件也可以針對目錄設置sgid作用:在設置了sgid權限的目錄下建立文件時,新創建的文件的所屬組會繼承上級目錄的所屬組sgid數字表示為2,在文件所屬組權限的第三位為小寫的s,就代表擁有sgid屬性

sticky:設置sticky可以將自己的文件保護起來sticky數字表示為1,在其他用戶權限位的第三位為小寫t#示例[root@centos7 ~]# chmod o+t /data/[root@centos7 ~]# ls -ld /data/drwxr-xr-t. 2 root root 6 Jul 13 03:20 /data/

#查找系統中的有特殊權限位文件

[root@centos7 ~]# find /usr/bin -type f -perm 4755 -exec ls -l {} ;

-rwsr-xr-x. 1 root root 32096 Oct 30 2018 /usr/bin/fusermount

-rwsr-xr-x. 1 root root 73888 Aug 8 2019 /usr/bin/chage

-rwsr-xr-x. 1 root root 78408 Aug 8 2019 /usr/bin/gpasswd

[root@centos7 ~]# find /usr/bin -type f -perm 2755 -exec ls -l {} ;

-rwxr-sr-x. 1 root tty 19544 Apr 1 00:51 /usr/bin/write

[root@centos7 ~]# ls -ld /tmp/

drwxrwxrwt. 12 root root 4096 Jul 13 08:14 /tmp/

#設置特殊權限

#1.設置suid針對用戶的

命令格式:chmod 4755 file 或者 chmod u+s file

#例子:

[root@centos7 ~]# useradd user1

[root@centos7 ~]# su - user1

[user1@centos7 ~]$ less /etc/shadow

/etc/shadow: Permission denied

[user1@centos7 ~]$ su - root

[root@centos7 ~]# chmod u+s /usr/bin/less #添加suid權限

[root@centos7 ~]# su - user1

Last login: Mon Jul 13 08:19:26 EDT 2020 on pts/0

[user1@centos7 ~]$ less /etc/shadow #可以查看

[root@centos7 ~]# ll /usr/bin/less

-rwsr-xr-x. 1 root root 158240 Jul 30 2015 /usr/bin/less

[root@centos7 ~]# chmod 4755 /usr/bin/less #相當于u+s

#2.設置sgid(針對組的)

命令格式:chmod 2755 file 或者 chmod g+s file

#例子

[root@centos7 ~]# mkdir test

[root@centos7 ~]# ls -ld test

drwxr-xr-x. 2 root root 6 Jul 13 08:28 test

[root@centos7 ~]# chmod g+s test #設置sgid權限

[root@centos7 ~]# ls -ld test

drwxr-sr-x. 2 root root 6 Jul 13 08:28 test

[root@centos7 ~]# chgrp test1 test/

[root@centos7 ~]# ls -ld test/

drwxr-sr-x. 2 root test1 20 Jul 13 08:29 test/

[root@centos7 ~]# touch test/aa.txt #創建新文件的所屬組會繼承上級目錄的

[root@centos7 ~]# ls -l test/aa.txt

-rw-r--r--. 1 root test1 0 Jul 13 08:29 test/aa.txt

#3.設置sticky(可以將自己文件保護起來)

命令格式:chmod o+t file

#例子:

[root@ctos3 ~]# touch 3.txt

[root@ctos3 ~]# chmod o+t 3.txt

[root@ctos3 ~]# ls -ld 3.txt

-rw-r--r-T 1 root root 0 Mar 9 02:38 3.txt

#有關suid和sgid總結

1.suid是針對命令和二進制程序的

2.suid作用是讓普通用戶以root(或其他)的用戶角色運行只有root(或其他)賬號才能運行的程序或命令,或程序命令對應本來沒有權限操作的文件等

3.sgid與suid不同的是,sgid既可以針對文件也可以針對目錄設置

4.sgid是針對用戶組權限位的

5.查看和修改文件屬性命令lsattr,chattr

lsattr:顯示文件屬性

chattr:修改文件屬性

參數:

-a:只能追加內容,

-i:不能被修改

+a :(Append)只能追加內容,如echo “111” >> test.txt

+i :(Immutable:不可改變) 系統不允許對這個文件進行任何的修改

-a:移除a參數

-i:移除i參數

#例子:

root@ctos3 ~]# mkdir attribute

[root@ctos3 ~]# cd attribute/

[root@ctos3 attribute]# echo "file attribution" > attribution.txt

[root@ctos3 attribute]# lsattr---------------- ./attribution.tx

t#根據上面操作。使用lsattr查看沒有賦予任何屬性,下面就使用chattr來為文件添加屬性

[root@ctos3 attribute]# chattr +i attribution.txt

[root@ctos3 attribute]# lsattr----i----------- ./attribution.txt

#提示:添加i屬性到文件之后,即使是root用戶也不能修改、刪除文件,可以加a權限,但是添加了也不能刪除文件,知道將這兩個權限刪除,才能刪除修改文件

[root@ctos3 attribute]# chmod 655 attribution.txt

chmod: changing permissions of ‘attribution.txt’: Operation not permitted

[root@ctos3 attribute]# rm attribution.txt

rm: remove regular file ‘attribution.txt’? yrm: cannot remove ‘attribution.txt’: Operation not permitted

分享到:
標簽:權限 文件 Linux
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定