天天看點

linux使用者權限管理, chmod, ln

1 /etc/passwd檔案

使用者名  密碼    UID        GID           Full Name                      主目錄                   預設的shell

asn  :x  :1000  :1000    :[email protected]        :/home/asn           :/bin/bash

sshd  :x      :116       :65534  :                                  :/var/run/sshd       :/usr/sbin/nologin

2 /etc/group檔案

adm:x:4:syslog,asn

cdrom:x:24:asn

sudo:x:27:asn

dip:x:30:asn

plugdev:x:46:asn

lpadmin:x:108:asn

asn:x:1000:

sambashare:x:124:asn

docker:x:125:asn

檢視 使用者asn所屬的組:

linux使用者權限管理, chmod, ln

組名     密碼 GID 

nogroup:x:65534:

檢視sshd所屬的組:

linux使用者權限管理, chmod, ln

3 檔案的讀、寫、執行權限

u 檔案所屬使用者

g 檔案所屬組

o 其他使用者

chmod -R g=rwx testDir

給testDir目錄所屬的組賦予讀、寫、執行權限

=============================

chmod - change file mode bits

文法:

chmod [OPTION]... MODE[,MODE]... FILE...

chmod [OPTION]... OCTAL-MODE FILE...

chmod [OPTION]... --reference=RFILE FILE...

描述:

chmod changes the file mode bits of each given file according to mode,

which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits.

The format of a symbolic mode is [ugoa...][[+-=][perms...]...], where perms is either zero or more letters from the set rwxXst, or a single letter from the set ugo.

Multiple symbolic modes can be given, separated by commas.

一個符号模式的格式:

A combination of the letters ugoa controls which users' access to the file will be changed:

the user who owns it (u), 檔案所有者為 o

other users in the file's group (g), 檔案所屬組中的其他使用者(排除了擁有者) g

other users not in the file's group (o), 不在檔案所屬組中的其他使用者 o

or all users (a). 所有使用者

        If none of these are given, the effect is as if a were given, but bits that are set in the umask are not affected. 如果未指定,即預設所有使用者

The operator + causes the selected file mode bits to be added to the existing file mode bits of each file; +号把目前選中的檔案模式位增加到每個檔案的現有檔案模式位

- causes them to be removed;

and = causes them to be added and causes unmentioned bits to be removed except that a directory's unmentioned set user and group ID bits are not affected.

=号,以前的模式位将被移除,除了目錄的未提到打使用者、組id位不受影響

The letters rwxXst select file mode bits for the affected users:

read (r),

write (w),

execute (or search for directories) (x),

execute/search only if the file is a directory or already has execute permission for some user (X), 僅當檔案是一個目錄、或該檔案對某些使用者已經有執行權限

set user or group ID on execution (s),

restricted deletion flag or sticky bit (t).

Instead of one or more of these letters, you can specify exactly one of the letters ugo: 确切地,你能夠指定字母ugo中的一個,而不能同時指定多個

        the permissions granted to the user who owns the file (u), 權限授予擁有該檔案的使用者

the permissions granted to other users who are members of the file's group (g), 權限授予檔案組成員中的其他使用者

and the permissions granted to users that are in neither of the two preceding categories (o). 權限授予不是上面2種情況的使用者

A numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1. 通過位4、2、1相加得到

Omitted digits are assumed to be leading zeros.

The first digit selects the set user ID (4) and set group ID (2) and restricted deletion or sticky (1) attributes.

The second digit selects permissions for the user who owns the file: read (4), write (2), and execute (1);

the third selects permissions for other users in the file's group, with the same values; and the fourth for other users not in the file's group, with the same values.

chmod never changes the permissions of symbolic links; chmod不能改變符号連結的權限 the chmod system call cannot change their permissions.

This is not a problem since the permissions of symbolic links are never used. 因為符号連接配接的權限永遠不會被使用,是以也無需改變符号連接配接的權限

However, for each symbolic link listed on the command line, chmod changes the permissions of the pointed-to file. 然而對于指令行上列出的每個符号連結,chmod改變的是其指向檔案的權限

In contrast, chmod ignores symbolic links encountered during recursive directory traversals. 相反,在遞歸周遊檔案的過程中,chmod會忽略遇到的符号連接配接

linux使用者權限管理, chmod, ln

SETUID AND SETGID BITS (setUID 和 setGID位)

chmod clears the set-group-ID bit of a regular file, if the file's group ID does not match the user's effective group ID or one of the user's supplementary group IDs,

        如果檔案的組ID與使用者的有效組ID(或使用者的服務組ID)不比對,chmod将會清除一個正常檔案的組ID設定位;

檔案sh01.sh屬于使用者asn,屬于組asn(組asn的ID是檔案的組ID)。檔案sh01.sh的組ID是檔案sh01.sh所屬使用者的有效組,是以此檔案的組ID設定位是有效的

        rw- r-- r--, 分别是,使用者ID設定位 --

unless the user has appro priate privileges.

舉例:

建立組shtestgroup,把檔案sh01.sh所屬組asn改為shtestgroup

Additional restrictions may cause the set-user-ID and set-group-ID bits of MODE or RFILE to be ignored.

This behavior depends on the policy and functionality of the underlying chmod system call. When in doubt, check the underlying system behavior.

chmod preserves a directory's set-user-ID and set-group-ID bits unless you explicitly specify otherwise.

You can set or clear the bits with symbolic modes like u+s and g-s, and you can set (but not clear) the bits with a numeric mode.

RESTRICTED DELETION FLAG OR STICKY BIT (restricted deletion flag or sticky bit)

The restricted deletion flag or sticky bit is a single bit, whose interpretation depends on the file type. 受限删除标志是一個單獨的位,它的解釋取決于檔案的類型

For directories, it prevents unprivileged users from removing or renaming a file in the directory unless they own the file or the directory;

對于目錄,該位會能夠防止未授權的使用者(什麼樣的使用者就是被授權了的)移除或重命名目錄中的一個檔案,除非該未授權的使用者擁有該檔案或目錄

this is called the restricted deletion flag for the directory, and is commonly found on world-writable directories like /tmp. 這就叫做目錄的限制删除标志位,通常在如tmp這種人人可寫的目錄上發現。

For regular files on some older systems, the bit saves the program's text image on the swap device so it will load more quickly when run; this is called the sticky bit.

對于某些系統上的普通常檔案,該位在swap交換區裝置上儲存了程式的檔案鏡像,所有運作該程式是能夠更快地加載 --- 稱為粘結位

OPTIONS

Change the mode of each FILE to MODE. With --reference, change the mode of each FILE to that of RFILE.

-c, --changes

like verbose but report only when a change is made 僅當改變做出時報告

-f, --silent, --quiet

-v, --verbose

output a diagnostic for every file processed

--no-preserve-root

do not treat '/' specially (the default) 不對根目錄特别對待

--preserve-root

fail to operate recursively on '/' 使不能再根目錄上進行遞歸操作

--reference=RFILE

use RFILE's mode instead of MODE values

-R, --recursive

change files and directories recursively 遞歸地改變檔案、目錄的模式

--help display this help and exit

--version

output version information and exit

Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

列出系統中的所有組名:

linux使用者權限管理, chmod, ln

linux上ln指令詳細說明

ln是linux中又一個非常重要指令,它的功能是為某一個檔案在另外一個位置建立一個連結,這個指令最常用的參數是-s,具體用法是:ln –s 源檔案目标檔案。

當我們需要在不同的目錄,用到相同的檔案時,我們不需要在每一個需要的目錄下都放一個必須相同的檔案,

我們隻要在某個固定的目錄,放上該檔案,然後在其它的目錄下用ln指令連結(link)它就可以

例如:ln –s /bin/less /usr/local/bin/less

  -s 是代号(symbolic)的意思。

  

這裡有兩點要注意:

1)ln指令會保持每一處連結檔案的同步性,也就是說,不論你改動了哪一處,其它的檔案都會發生相同的變化;

2)ln的連結分為軟連結和硬連結兩種, 無論是軟連結還是硬連結,檔案都保持同步變化

軟連結就是ln –s ** **,它隻會在你標明的位置上生成一個檔案的鏡像,不會占用磁盤空間

硬連結ln ** **,沒有參數-s,它會在你標明的位置上生成一個和源檔案大小相同的檔案

  如果你用ls察看一個目錄時,發現有的檔案後面有一個@的符号,那就是一個用ln指令生成的檔案,用ls –l指令去察看,就可以看到顯示的link的路徑了。

  指令名稱 : ln

  使用權限 : 所有使用者

  使用方式 : ln [options] source dist

  options選項: [-bdfinsvF] [-S backup-suffix] [-V {numbered,existing,simple}] [--help] [--version] [--]

說明 : Linux/Unix 檔案系統中,有所謂的連結(link),我們可以将其視為檔案的别名,而連結又可分為兩種 : 硬連結(hard link)與軟連結(symbolic link),

硬連結的意思是一個檔案可以有多個名稱,而軟連結的方式則是産生一個特殊的檔案,該檔案的内容是指向另一個檔案的位置。

硬連結是存在同一個檔案系統中,而軟連結卻可以跨越不同的檔案系統。

  

ln source dist産生一個連接配接檔案dist(連接配接到source),至于使用硬連結或軟鍊結則由參數決定

  不論是硬連結或軟鍊結都不會将原本的檔案複制一份,隻會占用非常少量的磁碟空間。

  -f         鍊結時先将與 dist 同檔名的檔案删除

  -d         允許系統管理者硬鍊結自己的目錄

  -i         在删除與 dist 同檔名的檔案時先進行詢問

  -n        在進行軟連結時,将 dist 視為一般的檔案

  -s        進行軟鍊結(symbolic link)

  -v         在連結之前顯示其檔名

  -b         将在鍊結時會被覆寫或删除的檔案進行備份

  -S SUFFIX     将備份的檔案都加上 SUFFIX 的字尾

  -V METHOD    指定備份的方式

  --help        顯示輔助說明

  --version        顯示版本

  範例 :

     從檔案sh01.sh産生一個軟連接配接檔案sh01-slink.sh --- 産生的軟連接配接檔案(大小為7個位元組),

$ ln -s sh01.sh sh01-slink.sh

從檔案sh01.sh産生一個硬連接配接檔案sh01-hlink.sh --- 硬連接配接,拷貝源檔案sh01.sh的内容作為新檔案sh01-hlink.sh(就是一個普通的檔案,與源檔案進行了關聯 - 内容互相同步更新)的内容

ln sh01.sh sh01-hlink.sh

檢視檔案:

linux使用者權限管理, chmod, ln

轉載于:https://www.cnblogs.com/asnjudy/p/4637153.html

繼續閱讀