# [Linux] 指令 - 權限 ###### tags: `linux` `指令` [TOC] ## 權限說明 | 代號 | 說明 | 權重 | |-|-|-| |d | 目錄 || |- | 檔案 || |l | 連結檔 || |b |可以隨機存取的裝置(隨身碟)|| |c |一次性讀取的設備(滑鼠、鍵盤)|| |r | 讀取 |4| |w | 寫入 |2| |x | 執行 |1| 格式: ``` [-][rwx][r-x][r--] ``` ## 檔案 |指令|說明| |-|-| |chgrp|所屬群組| |chown|擁有者| |chmod|權限| |ll|查看所在資料夾內的檔案權限| |df|查看磁碟使用與剩餘空間| |history|歷史紀錄| |!xxx|重新執行歷史紀錄中第xxx行的指令(需搭配history)| |rm -rf xxx|刪除xxx檔案(不確認)| |rm xxx|刪除xxx檔案| ### 查看所在資料夾內的檔案權限 ```=$ [winnie@myhome /]$ ll total 4 drwxr-xr-x. 2 root root 4096 Nov 3 15:34 newfolder ``` ### 變更檔案所屬群組(chgrp) ```=$ $ chgrp [-R] <group> <file/directory> ``` ### 變更檔案擁有者(chown) ```=$ $ sudo chown 使用者名稱:群組名稱 路徑 $ sudo chown winnie:myhome path $ sudo chown winnie path ``` ### 變更檔案權限(chmod) ```=$ $ chmod 754 test.txt ``` 擁有者 owner(user)-->u / 所屬群組之使用者 group-->g / 其他使用者 others-->o ```=$ $ chmod u=rwx,go=rx test.txt ``` 一次設定所有身分 ```=$ $ chmod a=rwx $ chmod ugo=rwx ``` ## 群組 ### 查看所有系統帳號 ```=$ $ cat /etc/passwd root:x:0:0:root:/root:/bin/bash ...略... tony:x:1000:1000::/home/tony:/bin/bash simon:x:1001:1001::/home/simon:/bin/bash ``` #欄位的說明 * `Username` 帳號,最多 32 個字元。 * `Password` 以 X 表示加密過的密碼,而加密的密碼儲存在`/etc/shadow`。 * `UserID` UID,0 是 root,1~99 預留給系統的帳號。 * `GroupID` GID,每個帳號對應的群組ID,同 `/etc/group` 中的 group ID。 * `UserID Info` 其它描述,非必要(即可為空)。 * `Home Directory` 顯示帳號的家目錄位置,預設在 /home 目錄下會以帳號名稱建立一個目錄作為家目錄。 * `Shell` 顯示出帳號使用的 Shell。 ### 查看群組 ```=$ $ cat /etc/group ``` ### 查看群組的所有用戶 ```=$ $ getent group <group-name> ``` * `Group name` 群組名稱 * `Group Password` 群組密碼,加密的密碼儲存在`/etc/gshadow`。 * `Group ID` GID,群組ID ### 查看目前帳號擁有的群組,第一個是主要群組 ```=$ $ groups ``` ### 切換目前帳號的主要群組 ```=$ $ newgrp <group-name> ``` ### 查看用戶資訊 ```=$ $ id <username> ``` ```=$ $ id tony uid=1000(tony) gid=1000(tony) groups=1000(tony),0(root) $ id frank id: frank: no such user ``` ### 新增群組 ```=$ $ groupadd <option> <groupname> ``` option : * `-g` 指定特定的 GID。 * `-p` 指定一組**已加密**的密碼,此處不常用。 ### 修改群組 ```=$ $ groupmod <option> <groupname> ``` option : * `-g` 修改 GID。 * `-n` 修改群組名稱。 * `-p` 修改群組密碼。 ### 刪除群組 ```=$ $ groupdel <groupname> ``` ### 群組管理員 ```=$ $ gpasswd <option> <groupname> ``` option : * `-a` 加入使用者到群組。 * `-d` 將使用者從群組中移除。 * `-r` 刪除群組密碼。 * `-R` 限制群組的存取權僅限群組成員。 * `-M` 設定組員名單,格式為 user1,user2,...,可以一次指定多個,需root。 * `-A` 設定群組管理員,格式為 admin1, admin2,...,可以一次指定多個,需root。 ## 帳號 ### 新增用戶 ```=$ $ useradd <option> <username> ``` option: * `-u` 指定 UID * `-g` 指定主要群組(在這裡是寫群組名稱) * `-c` UserID Info,此帳號的說明。 * `-d` Home Directory 指定目錄為家目錄。 * `-s` Shell,預設是 /bin/bash。 * `-e` Account Experiation Date,指定帳號失效的日期,格式為 YYYY-MM-DD。 會建立的資料: * `/etc/passwd` 帳號資訊 * `/etc/shadow` 密碼資訊 * `/etc/group` 群組資訊 * `/etc/gshadow` 群組密碼資訊 * `/home/<user name>` 家目錄,使用者的檔案 * `/var/spool/mail/<user name>` 郵件 ### 修改密碼 ```=$ $ passwd <option> <username> ``` ### 刪除用戶 會將新增用戶時建立的資料刪除。若只需停用,將 Account Experiation Date設定為 0 即可。 ```=$ $ userdel <option> <username> ``` option: * `-r` 連同家目錄一起刪除。 --- 參考資料: [Linux (三) - 檔案的基本屬性與權限](https://hackmd.io/@tienyulin/linux-file-chgrp-chown-chmod) [Linux (六) - 使用者和群組管理](https://blog.tienyulin.com/linux-6_user-group-management/)