Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 核心指令速查手冊 (四):passwd、su、umask、chgrp 系統安全與權限管理指令完整參數詳解

編輯:Linux 教學

passwd — 密碼管理

使用者自行修改

$ passwd
Changing password for user alice.
Current password: ********
New password: ********
Retype new password: ********
passwd: all authentication tokens updated successfully.

管理員功能 (需 root)

參數功能範例
-l鎖定帳號 (在 /etc/shadow 密碼前加 !!)passwd -l alice
-u解鎖帳號passwd -u alice
-d刪除密碼 (無密碼登入,極度危險)passwd -d alice
-n DAYS最小密碼壽命 (天)passwd -n 7 alice
-x DAYS最大密碼壽命 (天)passwd -x 90 alice
-w DAYS過期前警告天數passwd -w 14 alice
-i DAYS過期後寬限天數 (鎖定前)passwd -i 30 alice
-S顯示密碼狀態passwd -S alice

su — 切換使用者身分

常用模式

su -                    # 切換至 root 並載入完整環境 (推薦)
su - username           # 切換至指定用戶並載入環境
su -c "command"         # 以目標用戶執行單一指令後返回
su -s /bin/zsh username # 指定 Shell

參數對照

參數長選項功能
---login模擬完整登入 (載入環境變數、切換家目錄)
-c--command執行指令後自動返回原用戶
-m--preserve-environment保留當前環境變數 (不載入目標用戶環境)
-f--fast不讀取啟動檔 (僅 csh/tcsh 有效)

umask — 預設權限遮罩

核心概念

  • umask 定義「建立檔案/目錄時不給予的權限」
  • 檔案預設最大權限 666 (rw-rw-rw-);目錄 777 (rwxrwxrwx)
  • 最終權限 = 預設最大權限 - umask (位元運算)

常見數值對照

umask檔案權限目錄權限適用場景
022644 (rw-r--r--)755 (rwxr-xr-x)傳統預設、團隊協作
002664 (rw-rw-r--)775 (rwxrwxr-x)共享專案目錄 (配合 SGID)
077600 (rw-------)700 (rwx------)高安全、個人敏感資料
007660 (rw-rw----)770 (rwxrwx---)群組專案、隱私保護

永久生效配置

# 全域:/etc/profile 或 /etc/bashrc
umask 022

# 個人:~/.bash_profile 或 ~/.bashrc
umask 077

# 立即生效
source ~/.bash_profile

查看當前 umask

$ umask
0022
$ umask -S
u=rwx,g=rx,o=rx

chgrp — 變更檔案/目錄所屬群組

語法

chgrp [選項] 群組 檔案...
chgrp [選項] --reference=參考檔案 檔案...

關鍵參數

參數長選項功能
-R--recursive遞歸處理目錄樹
-v--verbose顯示每個檔案處理結果
-c--changes僅顯示實際變更的檔案
-f--silent抑制錯誤訊息
-h--no-dereference影響符號連結本身而非目標
--reference=FILE複製參考檔案的群組

實戰範例

# 將 /data/project 遞歸變更為 dev 群組
chgrp -R dev /data/project

# 複製 /etc/passwd 的群組屬性給 /etc/shadow
chgrp --reference=/etc/passwd /etc/shadow

# 搭配 find 精準控制
find /data -type f -name "*.conf" -exec chgrp config {} +

權限管理三劍客關聯

指令管理對象典型組合
chown擁有者 + 群組chown user:group file
chgrp群組chgrp group file
chmod權限位元chmod 750 file

最佳實踐: 目錄啟用 SGID (chmod g+s /shared) 確保新建檔案自動繼承目錄群組,配合 umask 002 實現團隊無縫協作。

Copyright © Windows教程網 All Rights Reserved