# 查詢當前登錄人 [root@localhost /]# whoami root # 查詢當前登陸人用戶組 [root@localhost /]# groups root # 查詢組下用戶 [root@localhost /]# groups kong kong : kong # 查詢登陸人的uid/gid [root@localhost /]# id uid=0(root) gid=0(root) 組=0(root) # 查詢其餘人的uid/gid [root@localhost /]# id kong uid=1008(kong) gid=1008(kong) 組=1008(kong) # 查詢更多組信息 root@localhost /]# more -l /etc/group root::0:custom-user bin::1: daemon::2: root::0:custom-user # 查詢更多用戶信息 [root@localhost /]# less /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
與用戶(user)相關的配置文件;
與用戶組(group)相關的配置文件;
說明
組數據結構
在/etc/group 中的每條記錄分四個字段:linux
# 添加用戶組 test-group-01 groupadd test-group-01 # 查看剛纔建立的用戶組信息 tail -n 1 /etc/group # 其餘選項 用法:groupadd [選項] 組 選項: -f, --force 若是組已經存在則成功退出 而且若是 GID 已經存在則取消 -g -g, --gid GID 爲新組使用 GID -h, --help 顯示此幫助信息並推出 -K, --key KEY=VALUE 不使用 /etc/login.defs 中的默認值 -o, --non-unique 容許建立有重複 GID 的組 -p, --password PASSWORD 爲新組使用此加密過的密碼 -r, --system 建立一個系統帳戶 -R, --root CHROOT_DIR chroot 到的目錄
# 刪除用戶組 test-group-01 groupdel test-group-01 # 其餘選項 用法:groupdel [選項] 組 選項: -h, --help 顯示此幫助信息並推出 -R, --root CHROOT_DIR chroot 到的目錄
# 修改用戶組名稱 groupmod -n test-group-02 test-group-01 # 查看修改信息 tail -n 1 /etc/group # 修改用戶組的GID groupmod -g 1002 test-group-02 # 其餘選項 用法:groupmod [選項] 組 選項: -g, --gid GID 將組 ID 改成 GID -h, --help 顯示此幫助信息並推出 -n, --new-name NEW_GROUP 更名爲 NEW_GROUP -o, --non-unique 容許使用重複的 GID -p, --password PASSWORD 將密碼更改成(加密過的) PASSWORD -R, --root CHROOT_DIR chroot 到的目錄
grpck grpconv
grpunconv
用戶數據結構
在/etc/passwd 中的每條記錄分七個字段:git
用戶的角色區分github
用戶在系統中是分角色的,在Linux 系統中,因爲角色不一樣,權限和所完成的任務也不一樣;值得注意的是用戶的角色是經過UID和識別的,特別是UID;在系統管理中,系統管理員必定要堅守UID 惟一的特性;
adduser: 會自動爲建立的用戶指定主目錄、系統shell版本,會在建立時輸入用戶密碼。 useradd:須要使用參數選項指定上述基本設置,若是不使用任何參數,則建立的用戶無密碼、無主目錄、沒有指定shell版本。
# 添加用戶 ,會同事建立用戶組 useradd 或 adduser useradd test-user-01 # 添加用戶,指定用戶組 useradd -g test-group-02 test-user-03 或 useradd -g 1002 test-user-04 # 添加用戶,不建立用戶主目錄 useradd -M test-user-05 # 添加用戶,建立一個系統帳戶 useradd -r test-user-06 # 添加用戶,爲用戶設置密碼 passwd custom-user # 其餘選項 用法:useradd [選項] 登陸 useradd -D useradd -D [選項] 選項: -b, --base-dir BASE_DIR 新帳戶的主目錄的基目錄 -c, --comment COMMENT 新帳戶的 GECOS 字段 -d, --home-dir HOME_DIR 新帳戶的主目錄 -D, --defaults 顯示或更改默認的 useradd 配置 -e, --expiredate EXPIRE_DATE 新帳戶的過時日期 -f, --inactive INACTIVE 新帳戶的密碼不活動期 -g, --gid GROUP 新帳戶主組的名稱或 ID -G, --groups GROUPS 新帳戶的附加組列表 -h, --help 顯示此幫助信息並推出 -k, --skel SKEL_DIR 使用此目錄做爲骨架目錄 -K, --key KEY=VALUE 不使用 /etc/login.defs 中的默認值 -l, --no-log-init 不要將此用戶添加到最近登陸和登陸失敗數據庫 -m, --create-home 建立用戶的主目錄 -M, --no-create-home 不建立用戶的主目錄 -N, --no-user-group 不建立同名的組 -o, --non-unique 容許使用重複的 UID 建立用戶 -p, --password PASSWORD 加密後的新帳戶密碼 -r, --system 建立一個系統帳戶 -R, --root CHROOT_DIR chroot 到的目錄 -s, --shell SHELL 新帳戶的登陸 shell -u, --uid UID 新帳戶的用戶 ID -U, --user-group 建立與用戶同名的組 -Z, --selinux-user SEUSER 爲 SELinux 用戶映射使用指定 SEUSER
# 刪除用戶 test-user-05 userdel test-user-05 #其餘選項 用法:userdel [選項] 登陸 選項: -f, --force force some actions that would fail otherwise e.g. removal of user still logged in or files, even if not owned by the user -h, --help 顯示此幫助信息並推出 -r, --remove 刪除主目錄和郵件池 -R, --root CHROOT_DIR chroot 到的目錄 -Z, --selinux-user 爲用戶刪除全部的 SELinux 用戶映射
# 修改用戶 將test-user-04修改爲test-user-05,可是沒有修改信箱 usermod -l test-user-05 test-user-04 # 將用戶custom-user附加組root usermod -G root custom-user # 從新登陸查看custom-user分組 groups #結果:custom-group root # 鎖定用戶 usermod -L custom-user # 解鎖用戶 usermod -U custom-user #其餘選項 用法:usermod [選項] 登陸 選項: -c, --comment 註釋 GECOS 字段的新值 -d, --home HOME_DIR 用戶的新主目錄 -e, --expiredate EXPIRE_DATE 設定賬戶過時的日期爲 EXPIRE_DATE -f, --inactive INACTIVE 過時 INACTIVE 天數後,設定密碼爲失效狀態 -g, --gid GROUP 強制使用 GROUP 爲新主組 -G, --groups GROUPS 新的附加組列表 GROUPS -a, --append GROUP 將用戶追加至上邊 -G 中提到的附加組中, 並不從其它組中刪除此用戶 -h, --help 顯示此幫助信息並推出 -l, --login LOGIN 新的登陸名稱 -L, --lock 鎖定用戶賬號 -m, --move-home 將家目錄內容移至新位置 (僅於 -d 一塊兒使用) -o, --non-unique 容許使用重複的(非惟一的) UID -p, --password PASSWORD 將加密過的密碼 (PASSWORD) 設爲新密碼 -R, --root CHROOT_DIR chroot 到的目錄 -s, --shell SHELL 該用戶賬號的新登陸 shell -u, --uid UID 用戶賬號的新 UID -U, --unlock 解鎖用戶賬號 -Z, --selinux-user SEUSER 用戶帳戶的新 SELinux 用戶映射
權限數據結構shell
權限名稱 | 符號 | 權限值 |
---|---|---|
read | r | 4 |
write | w | 2 |
execute | x | 1 |
每種身份(owner/group/others)各自的三個權限(r/w/x)分數是須要累加的,例如當權限爲:
符號對應關係表數據庫
命令 | 身份 | 設置 | 權限 | 文件夾/文件 |
---|---|---|---|---|
chmod | u(user) | + (加入) | r (read) | file |
g(group) | - (除去) | w (wirte) | ||
o(other) | = (設置) | x (execute) | ||
a(all) |
ls -l 輸出bash
[權限屬性信息] [鏈接數] [擁有者] [擁有者所屬用戶組] [大小] [最後修改時間] [文件名]
權限屬性列表爲10個字符數據結構
第一個字符表示文件類型,app
二、三、4個字符表示全部者權限,五、六、7個字符表示全部者同組用戶權限,八、九、10爲其餘用戶權限**
[root@localhost kong]# ls -l 總用量 4 -rw-rw-r-- 1 kong kong 13 3月 7 20:59 readme
# 查看命令 chsh -l # 指定登陸的 shell,能夠經過tail -l /etc/passwd 查看變動結果,以下圖 chsh -s /bin/sh # 其餘選項 用法: chsh [選項] [用戶名] 選項: -s, --shell <shell> 指定登陸 shell -l, --list-shells 打印 shell 列表並退出 -u, --help 顯示此幫助並退出 -v, --version 輸出版本信息並退出
# 查看本身的uid和gid id # 查看test-user-03的uid和gid id test-user-03 #其餘選項 Usage: id [OPTION]... [USER] Print user and group information for the specified USER, or (when USER omitted) for the current user. -a ignore, for compatibility with other versions -Z, --context print only the security context of the current user -g, --group print only the effective group ID -G, --groups print all group IDs -n, --name print a name instead of a number, for -ugG -r, --real print the real ID instead of the effective ID, with -ugG -u, --user print only the effective user ID -z, --zero delimit entries with NUL characters, not whitespace; not permitted in default format --help 顯示此幫助信息並退出 --version 顯示版本信息並退出 若是不附帶任何選項,程序會顯示一些可供識別用戶身份的有用信息。
# 修改文件組信息 chgrp custom-group 1.txt # 其餘選項 用法:chgrp [選項]... 用戶組 文件... 或:chgrp [選項]... --reference=參考文件 文件... Change the group of each FILE to GROUP. With --reference, change the group of each FILE to that of RFILE. -c, --changes like verbose but report only when a change is made -f, --silent, --quiet suppress most error messages -v, --verbose output a diagnostic for every file processed --dereference affect the referent of each symbolic link (this is the default), rather than the symbolic link itself -h, --no-dereference affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink) --no-preserve-root do not treat '/' specially (the default) --preserve-root fail to operate recursively on '/' --reference=RFILE use RFILE's group rather than specifying a GROUP value -R, --recursive operate on files and directories recursively The following options modify how a hierarchy is traversed when the -R option is also specified. If more than one is specified, only the final one takes effect. -H if a command line argument is a symbolic link to a directory, traverse it -L traverse every symbolic link to a directory encountered -P do not traverse any symbolic links (default) --help 顯示此幫助信息並退出 --version 顯示版本信息並退出 示例: chgrp staff /u 將 /u 的屬組更改成"staff"。 chgrp -hR staff /u 將 /u 及其子目錄下全部文件的屬組更改成"staff"。
# 修改文件的所屬者 chown custom-user 1.txt # 修改文件所屬者及組 chown -R root:root 1.txt # 其餘選項 -R recursive 遞歸的意思
# 使用符號或數字來表述權限 # 給文件執行權限 chmod u+x 3.sh # 給文件全部權限 chmod 777 2.txt # 使用符號設置權限 chmod u=rw-,g=rw-,o=--- 2.txt # 單獨設置 user的execute權限 chmod u+x 2.txt # 單獨設置 other的read權限 chmod o+r 2.txt # 單獨去除 other的read權限 chmod o-r 2.txt # 設置全部身份的read權限 chmod a+r 2.txt # 刪除全部身份的write權限 chmod a-w 2.txt # 其餘選項 用法:chmod [選項]... 模式[,模式]... 文件... 或:chmod [選項]... 八進制模式 文件... 或:chmod [選項]... --reference=參考文件 文件... 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 suppress most error messages -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 顯示此幫助信息並退出 --version 顯示版本信息並退出 Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.