權限體系的核心是安全 3A:Authentication(認證)、Authorization(授權)、Accounting(審計)。
Linux 使用者以 Username 與 UID 表示。管理員 root 的 UID 為 0(凡 UID 為 0 者皆具管理權限);普通使用者為 1–65535,其中系統帳號 CentOS 6 為 1–499、CentOS 7 為 1–999,負責分配守護行程的資源權限;登入帳號 CentOS 6 為 500 以上、CentOS 7 為 1000 以上,用於互動式登入。群組以 Groupname 與 GID 表示,範圍與使用者對應。
一個使用者可屬於多個群組:主組(私有組)組名與使用者名相同且只含一人,一個使用者只能有一個主組;主組以外的都是附加組。
groups # 查看目前所屬群組 groups bin # 查看指定使用者的主組與附加組 id bin # uid=1(bin) gid=1(bin) groups=1(bin),2(daemon),3(sys) id -u makangbo getent passwd makangbo
執行中的程式叫行程,它以發起者的身份運行。因此行程能存取的資源取決於「誰啟動它」,而不是檔案屬於誰——同一支 /bin/cat,root 執行與 xiaomage 執行的結果完全不同。
/etc/passwd 使用者及其屬性 login name:passwd:UID:GID:GECOS:home directory:shell xiaomage:x:3010:3010::/home/xiaomage:/bin/bash /etc/shadow 密碼及其相關屬性 帳號:加密密碼:最後改密碼日期(1970/1/1起算天數):最小使用天數(0為隨時可改): 最大使用天數(99999為永不過期):過期前警告天數(預設一週):過期後鎖定天數:帳號失效日:保留 /etc/group 群組及其屬性 群組名稱:群組密碼(通常不設,實際在 gshadow):GID:以本組為附加組的成員列表 /etc/gshadow 群組密碼及其相關屬性 群組名稱:群組密碼:群組管理員列表:成員列表
單向加密又稱雜湊演算法,原文不同則密文必不同,相同演算法定長輸出且不可逆推原始資料,並具雪崩效應——初始條件的微小改變會造成結果的巨大差異。sha256 為 256 bits、sha512 為 512 bits。更改演算法:
authconfig --passalgo=sha256 --update pwunconv # 密碼從 shadow 轉回 passwd pwconv # 還原為投影密碼
vipw / vigr # 編輯 passwd/group,加 -s 編輯 shadow/gshadow,會自動上鎖 pwck / grpck # 檢查密碼與群組檔完整性 useradd -u UID -g 主組 -G 附加組 -c 註解 -d 家目錄 -s SHELL -m -r 登入名 usermod -u/-g/-G/-aG/-s/-c/-d/-m/-l/-L/-U/-e/-f 登入名 userdel -r 登入名 groupadd -g GID 群組 groupmod -n/-g 群組 groupdel 群組
useradd testuser -u 1234 -g bin -G root,ftp -m -d /home/testdir/testuser -s /bin/csh
依序是:建立帳號、-u 指定 UID、-g 主組、-G 附加組、-d 家目錄路徑、-m 配合 -d 實際建立、-s 登入 shell。
usermod -u 4321 -g root -G nobody -l test -m -d /home/test testuser
-u 改 UID、-g 改主組、-G 改附加組、-l 改登入名、-d 新家目錄、-m 把舊家目錄資料搬過去。
先寫帳號檔(格式同 /etc/passwd),再用 newusers 一次建完:
vim u.txt user:x:3000:3000::/home/testdir/user:/bin/csh user1:x:3001:3001::/home/testdir/user1:/bin/csh ... user9:x:3009:3009::/home/testdir/user9:/bin/csh newusers u.txt ll /home/testdir/ # 確認家目錄都建好了 cat /etc/passwd # 確認帳號都寫入了
接著寫密碼檔(格式為 使用者:密碼),用管線交給 chpasswd:
vim m.txt user:userpass user1:user1pass ... cat m.txt | chpasswd cat /etc/shadow # 可看到已加密的 sha512 密碼;未設密碼者顯示 !!
兩邊都完成後帳號才算真正可用——只有帳號沒有密碼,等於不能登入。
作者:小馬哥(xiaomag) 本文出自「小馬哥Linux系統運維」部落格,轉載請與作者聯繫!