一.用戶管理linux
1.userdel [option]...logincentos
-r:刪除用戶家目錄bash
實例:ide
刪除gentoo帳戶,並刪除家目錄ui
[root@localhost home]# ls gentoo wangcai zhang zhang1 zhang2 [root@localhost home]# userdel -r gentoo [root@localhost home]# ls wangcai zhang zhang1 zhang2
2.id[option]...[user]centos7
-u:UIDblog
-g:GID繼承
-G:GROUPS遞歸
-n:NAMErem
實例:
(1)顯示zhang2的附加組
[root@localhost testdir]# id -Gn zhang3 -n顯示名字 zhang3 zhang2
3.su
su username:非登錄式切換,不會讀取目標用戶的配置文件,不改變當前的工做目錄
su - username:登錄式切換,會讀取目標用戶的配置文件,切換至家目錄,徹底切換
換身份執行命令:
su [-] username -c 'COMMAND'
選項:
-| --login
su -| username 至關於su -username
4.chage:用於密碼實效管理,該是用來修改賬號和密碼的有效期限
chage [option]...login
選項;
-d:指定密碼的最後修改日期
-E:密碼到期日期,過了這天,次帳戶將不可用
-I:密碼過時後,鎖定帳號的天數
-l:列出用戶以及密碼的有效期
-M:密碼保持 的最大天數
-W:密碼過時前,提早收到的警告信息的天數
實例:
(1)下一次登錄強制重設密碼
chage -d 0 tom
5. groupadd 建立一個新的工做組
groupadd [option]...group_name
-g:指定組id號
-r:建立系統組.centos<500
centos<1000
6.groupmod 組屬性的修改
groupmod[option]...group
選項:
-n:新名字
-g:新的GID
實例:
(1)修改zhang3的組名和GID
[root@localhost testdir]# groupmod -n tom zhang3 -g 666
7.groupdel 組刪除
groupdel group
8.gpasswd
gpasswd [optiom]group
選項:
-a user:將user添加到指定組
-d user:從指定組中移除用戶user
-A user1,user2...:設置有管理權限的用戶列表
實例:
(1)把user1設爲sales組管理員
[root@localhost ~]# gpasswd -A user1 sales
(2)把user2從sales組裏刪除
[root@localhost ~]# gpasswd -d user1 sales Removing user user1 from group sales
9.groupmems更改和修改組成員
group [options][action]
選項:
-g:更改成指定組(root)
action:
-a:指定用戶加入組
-d:從組中刪除用戶
-p: 從組中清除全部成員
-l :顯示組成員列表
實例:
(1)把zhang3用戶加到zhang4組裏
[root@localhost testdir]# usermod -aG zhang4 zhang3
或[root@localhost testdir]# groupmems -g zhang4 -a zhang3 (zhang3用戶是G或g選項後面跟的zhang4組的成員)
或[root@localhost testdir]#gpasswd -a zhang3 zhang4
用groupmems -lg zhang4 查看組成員
(2)查看zhang4組的成員
[root@localhost testdir]# groupmems -l -g zhang4 zhang3
二.文件權限
1.chown 修改文件的屬主和屬組
chown[OPTION]... [OWNER][:[GROUP]] FILE...
用法:
chown[-R][用戶名][文件或目錄]
chown[-R][用戶名:組名][文件或目錄]
chown[-R][:組名][文件或目錄]
chown[-R][用戶名:][文件或目錄]
chown [options]--refrence=refile file... file的權限和refile同樣
2.chgrp 更改屬組
chgrp[option]...group file
chgrp[option]... refrence=refile file...
選項:
-R:遞歸
實例:
(1)將bb文件的屬主和屬組改成user1和sales
[root@localhost testdir]# ll bb -rw-r--r--. 1 root root 0 Aug 3 10:31 bb [root@localhost testdir]# chown user1:sales bb [root@localhost testdir]# ll bb -rw-r--r--. 1 user1 sales 0 Aug 3 10:31 bb
(2)將bb的屬組改成user1
[root@localhost testdir]# chgrp user1 bb [root@localhost testdir]# ll bb -rw-r--r--. 1 user1 user1 0 Aug 3 10:31 bb
3.chmod 修改文件權限
chmod[option]...mode files
mode:
修改一類用戶的全部權限:
u= g= 0= ug= a= u= g=
修改一類用戶某位或某些權限
u+ u-g+ g-o+ o- a+ a- + -
chmod[OPTION]... --reference=RFILE FILE...參考RFILE文件的權限,將FILE的修改成同RFILE;
實例:
(1)給aa文件添加執行權限
[root@localhost testdir]# chmod u+x aa [root@localhost testdir]# ll aa -rwxr--r--. 1 user2 user2 0 Aug 3 10:31 aa
(2) 給aa的屬主分配讀、寫、執行的權限,給aa的所在組分配讀、執行的權限,給其餘用戶分配執行的權限
[root@localhost testdir]# chmod 751 aa [root@localhost testdir]# ll aa -rwxr-x--x. 1 user2 user2 0 Aug 3 10:31 aa 或 [root@localhost testdir]# chmod u=rwx,g=rx,o=x aa [root@localhost testdir]# ll aa -rwxr-x--x. 1 user2 user2 0 Aug 3 10:31 aa
4.umask
umask值 能夠用來保留在建立文件權限
新建FILE權限: 666-umask
若是所得結果某位存在執行(奇數)權限,則將其權限+1
新建DIR權限: 777-umask
非特權用戶umask是002
root的umask是022
umask: 查看
umask#: 設定
umask002
umask–S 模式方式顯示
umask–p 輸出可被調用
全局設置:/etc/bashrc用戶設置:~/.bashrc
三.linux文件系統上的特殊權限
SUID, SGID, Sticky
1.suid
suid屬性只能用在二進制可執行文件上,對目錄無效
權限設定:
chmod u+s file..
chmod u-s file..
實例:
(1)在zhang用戶上查看/etc/shadow
[root@centos7 testdir]# ll /bin/cat -rwxr-xr-x. 1 root root 54048 Nov 20 2015 /bin/cat [root@centos7 testdir]# su - zhang Last login: Wed Aug 3 19:28:16 CST 2016 on pts/0 [zhang@centos7 ~]$ cat /etc/shadow cat: /etc/shadow: Permission denied [zhang@centos7 ~]$ su Password: [root@centos7 zhang]# chmod u+s /bin/cat [root@centos7 zhang]# su - zhang Last login: Wed Aug 3 19:30:27 CST 2016 on pts/0 [zhang@centos7 ~]$ cat /etc/shadow root:$6$aHlQXG.A$G85xT17R4KPL.DiCTFaeE3j7Ua0eOoVZqSUM7exQBU/Q4QM04Vw7fsnnJBzrwTPnW90eLu3MXpHYMuiMkh4cK1:17008:0:99999:7::: bin:*:16659:0:99999:7::: daemon:*:16659:0:99999:7::: adm:*:16659:0:99999:7::: lp:*:16659:0:99999:7::: ......
2.sgid
chmod g+s file
chmod g-s file
sgid 應用在目錄上時,該目錄中所創建的文件或子目錄的所屬組都是該目錄的所屬組
sgid應用在可執行文件上時,其餘用戶在使用執行文件時就會擁有該執行文件的所屬組.
3.Sticky
chmod o+s file
chmod o-s file
sticky只能應用在有w+x權限目錄上,當用戶擁有sticky屬性,全部該目錄的文件或子目錄,不管什麼權限只有文件的擁有者和root用戶才能作刪除動做.
實例:
禁止其餘用戶刪除有寫權限的目錄下的文件
[zhang@localhost testdir]$ mkdir dir [zhang@localhost testdir]$ cd dir [zhang@localhost dir]$ touch aa bb [zhang@localhost dir]$ chmod o+t /testdir/dir [zhang@localhost dir]$ ll -d /testdir/dir drwxrwxr-t. 2 zhang zhang 4096 Aug 3 21:36 /testdir/dir [root@localhost dir]# su tom [tom@localhost dir]$ rm -f /testdir/dir/aa rm: cannot remove `/testdir/dir/aa': Permission denied
四.設置文件的特定屬性(能夠保護一些重要的文件,避免誤操做)
chattr +i 不能刪除,更名,更改
chattr+a 只能增長
lsattr 顯示特定屬性
五.ACL的使用
1.什麼是AClL
它主要目的是提供owner,group,others的read,write,execute權限以外的權限設置
它主要針對如下幾個項目:
用戶,用戶組,
默認屬性(mask):在該目錄下新建文件/目錄時設置的默認權限
2.如何啓動ACL
CentOS7.0默認建立的xfs和ext4文件系統有ACL功能。
CentOS7.X以前版本,默認手工建立的ext4文件系統無ACL功能,需手動增長.
(1)查看掛載參數的功能 mount
(2)手動添加ACL功能
tune2fs –o acl/dev/sdb1
或
mount –o acl/dev/sdb1 /mnt
3.ACL的使用
getfacl:取得某個文件/目錄的ACL設置項目
setfacl:設置某個目錄/文件的ACL規定
(1)setfacl用法
setfacl [-bkRd] [{-m|-x} acl參數] file
-m:設置acl權限
-x:刪除acl權限,不可與m合用
-b:刪除全部acl權限
-R:遞歸設置acl,包括子目錄都會被設置起來
-d:設置默認acl參數,支隊目錄有效,在該目錄新建的文件會繼承目錄的默認值
實例:
(1)針對特定用戶zhang
[root@centos7 zhang]# cat /etc/issue > /testdir/f2 [root@centos7 zhang]# ll /testdir/f2 -rw-r--r--. 1 root root 44 Aug 4 21:11 /testdir/f2 [root@centos7 zhang]# setfacl -m u:zhang:- /testdir/f2 把zhang用戶的讀權限禁用掉 [root@centos7 zhang]# su - zhang Last login: Thu Aug 4 21:07:14 CST 2016 on pts/1 [zhang@centos7 ~]$ cat /testdir/f2 cat: /testdir/f2: Permission denied 權限不夠
(2)清除設置在zhang用戶的acl 權限
[root@centos7 zhang]# setfacl -b /testdir/f2 [root@centos7 zhang]# ll /testdir/f2 -rw-r--r--. 1 root root 44 Aug 4 21:11 /testdir/f2
(3)setfacl -M file.acl file|directory
(4)setfacl -m d:u:wang:rx directory
4.getfacl 查看acl 權限l列表
getfacl file
實例:
[root@centos7 zhang]# getfacl /testdir/f2 getfacl: Removing leading '/' from absolute path names # file: testdir/f2 # owner: root # group: root user::rw- user:zhang:rw- group::r-- mask::rw- other::r--
5.mask
用戶或組所設置的權限必需要存在於mask的權限設置範圍內纔會生效,此即有效權限
實例:
能夠設置mask的權限來修改其餘用戶在文件上的權限
6.訪問控制列表
ACL :靈活的實現 權限控制
經常使用用法
mount -o acl /directory getfacl file |directory setfacl -m u:wang:rwx file|directory setfacl -Rm g:sales:rwX directory setfacl -M file.acl file|directory setfacl -m g:salesgroup:rw file| directory setfacl -m d:u:wang:rx directory setfacl -x u:wang file |directory setfacl -X file.acl directory