linux用戶權(quán)限設置有哪些

1. 權(quán)限的基本概述

什么是權(quán)限   系統(tǒng)對用戶所能執(zhí)行的功能的限制        為什么要有權(quán)限  為了保護每個用戶的自己的工作環(huán)境和隱私    權(quán)限跟用戶有什么關(guān)系    屬主    User        u  屬組    Group       g  其他人  others      o            a    所有            對應了三個基礎權(quán)限  r    可讀    readable        4  w    可寫    writable        2  x    可執(zhí)行    executable      1  -    沒有權(quán)限  權(quán)限位占位符      0  [root@qls ~]# ls -l total 61152 -rw-r--r--. 1 root root       39 Jul 17 19:16 123.txt -rw-r--r--. 1 root root  9272936 Jul 17 12:01 access.log   -               rw-     r--     r--     644  文件的類型       屬主     屬組     匿名   每三個為一組  第一個對應 可讀  r  第二個對應  可寫 w  第三個對應  可執(zhí)行 x  沒有此權(quán)限則用-代替    為什么要設置權(quán)限,如何設置權(quán)限   設置某個用戶對于系統(tǒng)的某個資源擁有什么樣管理權(quán)力   chmod    #設置權(quán)限的命令   選項:      -R    #針對目錄設置權(quán)限  賦予目錄及目錄以下所有文件的權(quán)限

2. 權(quán)限的設置案例

#添加權(quán)限  -    #收回某個權(quán)限  =    #覆蓋之前的權(quán)限    #添加權(quán)限  [root@qls ~]# ll total 4 -rw-r--r-- 1 root root 158 Jul 23 09:06 hosts [root@qls ~]# chmod  u+x  hosts  [root@qls ~]# ll total 4 -rwxr--r-- 1 root root 158 Jul 23 09:06 hosts [root@qls ~]# chmod g+wx hosts [root@qls ~]# ll total 4 -rwxrwxr-- 1 root root 158 Jul 23 09:06 hosts [root@qls ~]# chmod  o+w  hosts  [root@qls ~]# ll total 4 -rwxrwxrw- 1 root root 158 Jul 23 09:06 hosts   [root@qls ~]# chmod a+x  hosts  [root@qls ~]# ll total 4 -rwxrwxrwx 1 root root 158 Jul 23 09:06 hosts   #收回權(quán)限  #a可以省略   [root@qls ~]# chmod   -x  hosts  [root@qls ~]# ll total 4 -rw-rw-rw- 1 root root 158 Jul 23 09:06 hosts   [root@qls ~]# chmod u-w  hosts  [root@qls ~]# ll total 4 -r--rw-rw- 1 root root 158 Jul 23 09:06 hosts [root@qls ~]# chmod  g-w,o-rw  hosts [root@qls ~]# ll total 4 -r--r----- 1 root root 158 Jul 23 09:06 hosts [root@qls ~]#   #覆蓋之前的所有權(quán)限  [root@qls ~]# chmod a=rw  hosts  [root@qls ~]# ll total 4 -rw-rw-rw- 1 root root 158 Jul 23 09:06 hosts   [root@qls ~]# chmod  o=-  hosts [root@qls ~]# ll total 4 -rw-rw---- 1 root root 158 Jul 23 09:06 hosts    根據(jù)數(shù)字進行修改    會把原來的權(quán)限全部覆蓋掉   -R    #給目錄的權(quán)限及目錄以下的所有文件或者子目錄都設置統(tǒng)一權(quán)限  [root@qls ~]# chmod 644  hosts  [root@qls ~]# ll total 4 -rw-r--r-- 1 root root 158 Jul 23 09:06 hosts  [root@qls ~]# mkdir  data [root@qls ~]# ll total 4 drwxr-xr-x 2 root root   6 Jul 23 09:35 data -rw-r--r-- 1 root root 158 Jul 23 09:06 hosts [root@qls ~]# cp /etc/hosts  data/ [root@qls ~]# ll data/ total 4 -rw-r--r-- 1 root root 158 Jul 23 09:35 hosts [root@qls ~]# ll -d data/ drwxr-xr-x 2 root root 19 Jul 23 09:35 data/ [root@qls ~]# chmod 700  data/ [root@qls ~]# ll -d data/ drwx------ 2 root root 19 Jul 23 09:35 data/ [root@qls ~]# ll data/ total 4 -rw-r--r-- 1 root root 158 Jul 23 09:35 hosts [root@qls ~]# chmod -R  755  data/ [root@qls ~]# ll -d data/ drwxr-xr-x 2 root root 19 Jul 23 09:35 data/ [root@qls ~]# ll data/ total 4 -rwxr-xr-x 1 root root 158 Jul 23 09:35 hosts   #權(quán)限設置案例  /opt/test   針對于此目錄      屬主為root  屬組為dev     dev01  dev02    屬主擁有所有權(quán)限  屬組擁有可讀 可寫權(quán)限  其他人沒有任何權(quán)限   [root@qls ~]# mkdir  /opt/test [root@qls ~]# ll /opt/ total 0 drwxr-xr-x 2 root root 6 Jul 23 09:40 test [root@qls ~]# groupadd   dev [root@qls ~]# chgrp  dev  /opt/test/ [root@qls ~]# ll /opt/ total 0 drwxr-xr-x 2 root dev 6 Jul 23 09:40 test [root@qls ~]# chmod 760  /opt/test/ [root@qls ~]# ll /opt/ total 0 drwxrw---- 2 root dev 6 Jul 23 09:40 test

3. 權(quán)限對文件或者目錄的影響

cat head tail               瀏覽目錄及子目錄的列表  ls tree         w    可以新增,修改文件內(nèi)容的權(quán)利 vim echo > >>    可以新建或者刪除,移動目錄中的文件的權(quán)利   x    可以執(zhí)行文件的權(quán)利 腳本                         可以進入目錄   cd

4. 文件權(quán)限設置案例

[root@qls ~]# echo  "hostname"  >> /opt/file.txt  [root@qls ~]# ll /opt/ total 4 -rw-r--r-- 1 root root 9 Jul 23 09:57 file.txt  [root@qls ~]# useradd   qls01 [root@qls ~]#  [root@qls ~]# echo "1" | passwd  --stdin  qls01 Changing password for user qls01. passwd: all authentication tokens updated successfully.   [root@qls ~]# su  -  qls01 Last login: Thu Jul 23 09:59:14 CST 2020 on pts/0 [qls01@qls ~]$ ll /opt/ total 4 -rw-r--r-- 1 root root 9 Jul 23 09:57 file.txt drwxrw---- 2 root dev  6 Jul 23 09:40 test [qls01@qls ~]$ cat /opt/file.txt  hostname [qls01@qls ~]$ head /opt/file.txt hostname [qls01@qls ~]$ tail /opt/file.txt hostname   [qls01@qls ~]$ vim  /opt/file.txt [qls01@qls ~]$ echo "123"  >> /opt/file.txt -bash: /opt/file.txt: Permission denied [qls01@qls ~]$ /opt/file.txt  -bash: /opt/file.txt: Permission denied   #文件只有r權(quán)限時,是可以正常查看文件內(nèi)容的,不可以修改或者執(zhí)行文件的權(quán)限   w權(quán)限   [root@qls ~]# chmod  o=w  /opt/file.txt  [root@qls ~]# ll /opt/file.txt -rw-r---w- 1 root root 9 Jul 23 09:57 /opt/file.txt  [root@qls ~]# su - qls01 Last login: Thu Jul 23 10:00:14 CST 2020 on pts/0 [qls01@qls ~]$ cat /opt/file.txt  cat: /opt/file.txt: Permission denied [qls01@qls ~]$ vim /opt/file.txt [qls01@qls ~]$ cat /opt/file.txt cat: /opt/file.txt: Permission denied [qls01@qls ~]$ /opt/file.txt -bash: /opt/file.txt: Permission denied  [root@qls ~]# cat  /opt/file.txt  uirethruie  [qls01@qls ~]$ echo "test"  >> /opt/file.txt  [qls01@qls ~]$ echo "test"  > /opt/file.txt   [root@qls ~]# cat  /opt/file.txt  uirethruie test [root@qls ~]# cat  /opt/file.txt  test   #只有w權(quán)限時, 無法查看和執(zhí)行文件的權(quán)限  使用vim編輯文件文件時,無法查看里面的內(nèi)容,可以進行編輯,但是需要強制保存,但是保存之后,原來的內(nèi)容被覆蓋了  可以使用echo命令進行追加或者重定向內(nèi)容進去   x權(quán)限  [root@qls ~]# chmod o=x  /opt/file.txt  [root@qls ~]# ll /opt/file.txt -rw-r----x 1 root root 5 Jul 23 10:09 /opt/file.txt [root@qls ~]# su - qls01 Last login: Thu Jul 23 10:08:38 CST 2020 on pts/0 [qls01@qls ~]$ ll /opt/file.txt  -rw-r----x 1 root root 5 Jul 23 10:09 /opt/file.txt [qls01@qls ~]$ cat /opt/file.txt cat: /opt/file.txt: Permission denied [qls01@qls ~]$ echo "123" >> /opt/file.txt -bash: /opt/file.txt: Permission denied [qls01@qls ~]$ /opt/file.txt bash: /opt/file.txt: Permission denied  #文件只有x權(quán)限  什么都干不了     rw權(quán)限   [root@qls ~]# chmod   o=rw  /opt/file.txt  [root@qls ~]# ll /opt/file.txt -rw-r--rw- 1 root root 5 Jul 23 10:09 /opt/file.txt [root@qls ~]# su - qls01 Last login: Thu Jul 23 10:11:27 CST 2020 on pts/0 [qls01@qls ~]$ cat /opt/file.txt  test [qls01@qls ~]$ echo "hostname"  > /opt/file.txt [qls01@qls ~]$ vim /opt/file.txt [qls01@qls ~]$ cat /opt/file.txt hostname pwd [qls01@qls ~]$ /opt/file.txt -bash: /opt/file.txt: Permission denied   #經(jīng)過測試,文件w權(quán)限需要r權(quán)限的配合  才能正常的修改文件內(nèi)容     rx權(quán)限   [root@qls ~]# chmod  o=rx  /opt/file.txt  [root@qls ~]# ll /opt/file.txt -rw-r--r-x 1 root root 13 Jul 23 10:14 /opt/file.txt [root@qls ~]# su - qls01 Last login: Thu Jul 23 10:14:16 CST 2020 on pts/0 [qls01@qls ~]$ cat /opt/file.txt  hostname pwd [qls01@qls ~]$ echo "123" > /opt/file.txt -bash: /opt/file.txt: Permission denied [qls01@qls ~]$ vim /opt/file.txt [qls01@qls ~]$ /opt/file.txt qls /home/qls01    #經(jīng)過測試,文件的x權(quán)限需要r權(quán)限的配合    wx權(quán)限  沒有什么用處    rwx權(quán)限      權(quán)限太大      Permission denied    #權(quán)限不足 沒有權(quán)限

5. 目錄權(quán)限設置案例

權(quán)限對目錄的影響    r權(quán)限     具有可以瀏覽目錄及其子目錄下的列表  屬性信息     [root@qls ~]# chmod o=r  /opt/test/ [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 root root 13 Jul 23 10:14 file.txt drwxrw-r-- 2 root root  6 Jul 23 09:40 test [root@qls ~]# touch  /opt/test/data.{txt,log,sh} [root@qls ~]# mkdir  /opt/test/oldboy{01..03} [root@qls ~]# ll /opt/test/ total 0 -rw-r--r-- 1 root root 0 Jul 23 10:51 data.log -rw-r--r-- 1 root root 0 Jul 23 10:51 data.sh -rw-r--r-- 1 root root 0 Jul 23 10:51 data.txt drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy01 drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy02 drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy03  [root@qls ~]# su  -  qls01 Last login: Thu Jul 23 10:51:09 CST 2020 on pts/0  [qls01@qls ~]$ ls   /opt/test/ ls: cannot access /opt/test/data.txt: Permission denied ls: cannot access /opt/test/data.log: Permission denied ls: cannot access /opt/test/data.sh: Permission denied ls: cannot access /opt/test/oldboy01: Permission denied ls: cannot access /opt/test/oldboy02: Permission denied ls: cannot access /opt/test/oldboy03: Permission denied data.log  data.sh  data.txt  oldboy01  oldboy02  oldboy03   [qls01@qls ~]$ ls -l  /opt/test/ ls: cannot access /opt/test/data.txt: Permission denied ls: cannot access /opt/test/data.log: Permission denied ls: cannot access /opt/test/data.sh: Permission denied ls: cannot access /opt/test/oldboy01: Permission denied ls: cannot access /opt/test/oldboy02: Permission denied ls: cannot access /opt/test/oldboy03: Permission denied total 0 -????????? ? ? ? ?            ? data.log -????????? ? ? ? ?            ? data.sh -????????? ? ? ? ?            ? data.txt d????????? ? ? ? ?            ? oldboy01 d????????? ? ? ? ?            ? oldboy02 d????????? ? ? ? ?            ? oldboy03  [qls01@qls ~]$ rm -f  /opt/test/data.log  rm: cannot remove ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ touch /opt/test/data.conf touch: cannot touch ‘/opt/test/data.conf’: Permission denied [qls01@qls ~]$ mv /opt/test/data.log  /tmp mv: cannot stat ‘/opt/test/data.log’: Permission denied  [qls01@qls ~]$ cd  /opt/test/ -bash: cd: /opt/test/: Permission denied  [qls01@qls ~]$ tree  /opt/test/  #沒有任何統(tǒng)計  /opt/test/  0 directories, 0 files   #目錄只有r權(quán)限,使用ls -l命令查看目錄下的列表,會出現(xiàn)一堆的權(quán)限不足,但是文件名和文件類型顯示出來了,其他的屬性信息都是問號   不能對此目錄下的文件進行新建或者刪除及其移動的操作    也不能切換到這個目錄    w權(quán)限   [root@qls ~]# chmod  o=w  /opt/test/ [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 root root  13 Jul 23 10:14 file.txt drwxrw--w- 5 root root 101 Jul 23 10:51 test   [root@qls ~]# su  -  qls01 Last login: Thu Jul 23 10:52:22 CST 2020 on pts/0 [qls01@qls ~]$ ls /opt/test/ ls: cannot open directory /opt/test/: Permission denied [qls01@qls ~]$ ls -l /opt/test/ ls: cannot open directory /opt/test/: Permission denied [qls01@qls ~]$ cd /opt/test/ -bash: cd: /opt/test/: Permission denied [qls01@qls ~]$ touch  /opt/test/123.txt touch: cannot touch ‘/opt/test/123.txt’: Permission denied [qls01@qls ~]$ rm -f  /opt/test/data.log rm: cannot remove ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ mv /opt/test/data.log  /tmp/ mv: cannot stat ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ cp /opt/test/data.log /tmp cp: cannot stat ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ cp /opt/test/data.log /tmp cp: cannot stat ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ mv /opt/test/data.log  /tmp/ mv: cannot stat ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ rm -f  /opt/test/data.log rm: cannot remove ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ ll -d /opt/ drwxrwxrwx 3 root root 34 Jul 23 09:57 /opt/  #只有w權(quán)限時,目錄什么都做不了    x權(quán)限    進入目錄  [root@qls ~]# su - qls01 Last login: Thu Jul 23 10:57:51 CST 2020 on pts/0 [qls01@qls ~]$ cd  /opt/test/ [qls01@qls test]$ ls ls: cannot open directory .: Permission denied [qls01@qls test]$ ls -l ls: cannot open directory .: Permission denied [qls01@qls test]$ rm -f  data.log rm: cannot remove ‘data.log’: Permission denied [qls01@qls test]$ touch  data.conf touch: cannot touch ‘data.conf’: Permission denied [qls01@qls test]$ mv data.log /tmp/ mv: cannot move ‘data.log’ to ‘/tmp/data.log’: Permission denied [qls01@qls test]$ cp data.log  /tmp/ [qls01@qls test]$ ll /tmp/ total 4 drwxr-xr-x. 2 root  root   51 Jul 17 17:59 data1 drwxr-xr-x. 2 root  root   51 Jul 17 17:59 data2 drwxr-xr-x. 2 root  root   51 Jul 17 17:59 data3 -rw-r--r--  1 qls01 qls01   0 Jul 23 11:03 data.log  #目錄只有x權(quán)限時,可以切換到目錄中,無法查看目錄列表信息  也無法進行刪除、新建、移動等操作   可以進行復制操作    rw權(quán)限       [root@qls ~]# chmod o=rw  /opt/test/ [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 root root  13 Jul 23 10:14 file.txt drwxrw-rw- 5 root root 101 Jul 23 10:51 test [root@qls ~]# su - qls01 Last login: Thu Jul 23 11:06:21 CST 2020 on pts/0 [qls01@qls ~]$ cd /opt/test/ -bash: cd: /opt/test/: Permission denied [qls01@qls ~]$ ls /opt/test/ ls: cannot access /opt/test/data.txt: Permission denied ls: cannot access /opt/test/data.log: Permission denied ls: cannot access /opt/test/data.sh: Permission denied ls: cannot access /opt/test/oldboy01: Permission denied ls: cannot access /opt/test/oldboy02: Permission denied ls: cannot access /opt/test/oldboy03: Permission denied data.log  data.sh  data.txt  oldboy01  oldboy02  oldboy03 [qls01@qls ~]$ ls -l /opt/test/ ls: cannot access /opt/test/data.txt: Permission denied ls: cannot access /opt/test/data.log: Permission denied ls: cannot access /opt/test/data.sh: Permission denied ls: cannot access /opt/test/oldboy01: Permission denied ls: cannot access /opt/test/oldboy02: Permission denied ls: cannot access /opt/test/oldboy03: Permission denied total 0 -????????? ? ? ? ?            ? data.log -????????? ? ? ? ?            ? data.sh -????????? ? ? ? ?            ? data.txt d????????? ? ? ? ?            ? oldboy01 d????????? ? ? ? ?            ? oldboy02 d????????? ? ? ? ?            ? oldboy03 [qls01@qls ~]$ touch  /opt/test/data.conf touch: cannot touch ‘/opt/test/data.conf’: Permission denied [qls01@qls ~]$ rm -f /opt/test/data.log rm: cannot remove ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ mv /opt/test/data.log /tmp/ mv: cannot stat ‘/opt/test/data.log’: Permission denied [qls01@qls ~]$ cp /opt/test/data.txt  /tmp/ cp: cannot stat ‘/opt/test/data.txt’: Permission denied   #rw權(quán)限 跟只有r權(quán)限作用是一樣的     rx權(quán)限     [root@qls ~]# chmod o=rx  /opt/test/ [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 root root  13 Jul 23 10:14 file.txt drwxrw-r-x 5 root root 101 Jul 23 10:51 test [root@qls ~]# su - qls01 Last login: Thu Jul 23 11:10:14 CST 2020 on pts/0 [qls01@qls ~]$ cd  /opt/test/ [qls01@qls test]$ ls data.log  data.sh  data.txt  oldboy01  oldboy02  oldboy03 [qls01@qls test]$ ls -l total 0 -rw-r--r-- 1 root root 0 Jul 23 10:51 data.log -rw-r--r-- 1 root root 0 Jul 23 10:51 data.sh -rw-r--r-- 1 root root 0 Jul 23 10:51 data.txt drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy01 drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy02 drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy03 [qls01@qls test]$ touch  data.conf touch: cannot touch ‘data.conf’: Permission denied [qls01@qls test]$ rm -f data.log  rm: cannot remove ‘data.log’: Permission denied [qls01@qls test]$ mv data.txt /tmp/ mv: cannot move ‘data.txt’ to ‘/tmp/data.txt’: Permission denied [qls01@qls test]$ cp  data.txt  /tmp/ [qls01@qls test]$ ll /tmp/ total 4 drwxr-xr-x. 2 root  root   51 Jul 17 17:59 data1 drwxr-xr-x. 2 root  root   51 Jul 17 17:59 data2 drwxr-xr-x. 2 root  root   51 Jul 17 17:59 data3 -rw-r--r--  1 qls01 qls01   0 Jul 23 11:03 data.log -rw-r--r--  1 qls01 qls01   0 Jul 23 11:13 data.txt   #目錄擁有rx權(quán)限時,可以正常的查看目錄列表信息,屬性信息,也可以進入目錄,可以復制文件到其他目錄,但是不能執(zhí)行新建、刪除、移動等操作     wx權(quán)限  [root@qls ~]# chmod o=wx  /opt/test/ [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 root root  13 Jul 23 10:14 file.txt drwxrw--wx 5 root root 101 Jul 23 10:51 test [root@qls ~]# su - qls01 Last login: Thu Jul 23 11:12:31 CST 2020 on pts/0 [qls01@qls ~]$ cd /opt/test/ [qls01@qls test]$ ls ls: cannot open directory .: Permission denied [qls01@qls test]$ touch  123.txt [qls01@qls test]$ ls ls: cannot open directory .: Permission denied [qls01@qls test]$ rm -f data.log [qls01@qls test]$ mv data.txt  /tmp/ [qls01@qls test]$ ls ls: cannot open directory .: Permission denied   #目錄擁有wx權(quán)限時,可以進入目錄,可以新建,刪除、移動文件的權(quán)利  但是查看不了目錄的列表及屬性信息

6. 屬主屬組設置

chown  #設置屬主屬組   只有root管理員才可以進行設置      選項:    -R    #遞歸設置 設置目錄及其目錄以下的所有文件  [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 root root 13 Jul 23 10:14 file.txt drwxrw--wx 5 root root 84 Jul 23 11:17 test [root@qls ~]# chown qls01   /opt/file.txt     #默認設置的是屬主   [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 qls01 root 13 Jul 23 10:14 file.txt drwxrw--wx 5 root  root 84 Jul 23 11:17 test  [root@qls ~]# chown  .qls01  /opt/file.txt    #設置屬組  [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 qls01 qls01 13 Jul 23 10:14 file.txt drwxrw--wx 5 root  root  84 Jul 23 11:17 test  [root@qls ~]# chown  root.root  /opt/file.txt   #同時設置屬主屬組  [root@qls ~]# ll /opt/ total 4 -rw-r--r-x 1 root root 13 Jul 23 10:14 file.txt drwxrw--wx 5 root root 84 Jul 23 11:17 test  [root@qls ~]# chown  qls01.qls01  /opt/test/    #只設置目錄 [root@qls ~]# ll -d /opt/test/ drwxrw--wx 5 qls01 qls01 84 Jul 23 11:17 /opt/test/ [root@qls ~]# ll /opt/test/ total 0 -rw-rw-r-- 1 qls01 qls01 0 Jul 23 11:17 123.txt -rw-r--r-- 1 root  root  0 Jul 23 10:51 data.sh drwxr-xr-x 2 root  root  6 Jul 23 10:51 oldboy01 drwxr-xr-x 2 root  root  6 Jul 23 10:51 oldboy02 drwxr-xr-x 2 root  root  6 Jul 23 10:51 oldboy03 [root@qls ~]# chown -R  qls01.qls01  /opt/test/   #遞歸設置  [root@qls ~]# ll /opt/test/ total 0 -rw-rw-r-- 1 qls01 qls01 0 Jul 23 11:17 123.txt -rw-r--r-- 1 qls01 qls01 0 Jul 23 10:51 data.sh drwxr-xr-x 2 qls01 qls01 6 Jul 23 10:51 oldboy01 drwxr-xr-x 2 qls01 qls01 6 Jul 23 10:51 oldboy02 drwxr-xr-x 2 qls01 qls01 6 Jul 23 10:51 oldboy03   chgrp    #設置屬組      [root@qls ~]# chgrp   root  /opt/test/ [root@qls ~]# ll -d /opt/test/

7. Umask控制權(quán)限

[root@qls ~]# ll total 4 drwxr-xr-x 2 root root   6 Jul 23 11:21 123 -rw-r--r-- 1 root root   0 Jul 23 11:21 123.txt  系統(tǒng)中為什么新創(chuàng)建的目錄的權(quán)限為755,文件的權(quán)限為644  都是由系統(tǒng)的控制權(quán)限所控制的  umask    #控制權(quán)限的命令   [root@qls ~]# umask 0022   系統(tǒng)中是如何計算權(quán)限   系統(tǒng)新創(chuàng)建的目錄的權(quán)限由最大權(quán)限777減去umask控制權(quán)限022得到的就是755,所以說新創(chuàng)建的目錄的權(quán)限為755,新創(chuàng)建文件的權(quán)限由文件最大權(quán)限666減去umask控制權(quán)限022,得到644權(quán)限,所以說新創(chuàng)建的文件的權(quán)限為644,當文件權(quán)限遇到奇數(shù)時,在奇數(shù)為加一      [root@qls ~]# umask 0022 [root@qls ~]# umask  033 [root@qls ~]# umask  0033 [root@qls ~]# mkdir  oldboy [root@qls ~]# ll total 4 drwxr-xr-x 2 root root   6 Jul 23 11:21 123 -rw-r--r-- 1 root root   0 Jul 23 11:21 123.txt drwxr--r-- 2 root root   6 Jul 23 12:02 oldboy [root@qls ~]# touch oldboy.txt [root@qls ~]# ll

8. 用戶練習題

1.刪除用戶基本組shanghai03。發(fā)現(xiàn)無法正常刪除,怎樣才能將其刪除掉,不能刪除用戶。  這個組是某個用戶的基本組,所以刪除不了   跟這里面的用戶換個組再刪除     2.打開多個xshell窗口連接登錄同一虛擬機,使用不同的用戶登錄多次,分別使用w和who命令顯示當前系統(tǒng)上的所有已經(jīng)登錄的用戶,取出用戶名這一列,注意:同一個用戶登錄多次,則只顯示一次即可。  [root@qls ~]# w  12:10:05 up 3 days, 25 min,  5 users,  load average: 0.04, 0.03, 0.05 USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT root     pts/0    10.0.0.1         Wed09   50:37   0.30s  0.00s -bash root     pts/1    10.0.0.1         12:03    5.00s  0.00s  0.00s w root     pts/2    10.0.0.1         12:09   34.00s  0.00s  0.00s -bash root     pts/3    10.0.0.1         12:09   32.00s  0.00s  0.00s -bash qls01    pts/4    10.0.0.1         12:10    4.00s  0.00s  0.00s -bash [root@qls ~]# w | awk 'NR>2' root     pts/0    10.0.0.1         Wed09   50:49   0.30s  0.00s -bash root     pts/1    10.0.0.1         12:03    1.00s  0.00s  0.00s w root     pts/2    10.0.0.1         12:09   46.00s  0.00s  0.00s -bash root     pts/3    10.0.0.1         12:09   44.00s  0.00s  0.00s -bash qls01    pts/4    10.0.0.1         12:10   16.00s  0.00s  0.00s -bash [root@qls ~]# w | awk 'NR>2{print $1}' root root root root qls01 [root@qls ~]# w | awk 'NR>2{print $1}' | sort  qls01 root root root root [root@qls ~]# w | awk 'NR>2{print $1}' | sort  |uniq -c       1 qls01       4 root [root@qls ~]#      3.創(chuàng)建用戶olddir,其ID號為1005,基本組為old  groupadd  old  useradd  -u1005  -g old  olddir     4.顯示當前登錄的用戶的UID號,你有幾種方法實現(xiàn)?  id  grep  'user'  /etc/passwd   echo $UID  5.對于用戶來說,組有幾種類別?有什么區(qū)別?  基本組   每個用戶只能有一個基本組  附加組  用戶可以擁有多個附加組   6./etc/group文件以:為分隔符,分為四列,解釋其每列的含義? 7./etc/gshadow文件以:為分隔符,分為四列,解釋其每列的含義? 8.如果想從root用戶切換到普通用戶,使用什么命令?  su -  username    9.切換到普通用戶后,想返回到root用戶,怎么做?有幾種方法,有什么區(qū)別?  su  -   Ctrl +  D    logout  exit    10.Shell主要分為哪幾類?  交互式  非交互式   登錄式  非登錄式  11.Bash Shell的個人及全局配置文件有哪些?哪個優(yōu)先級別高?  個人高    12.登錄式Shell配置文件執(zhí)行順序? 13.非登錄式shell配置文件執(zhí)行順序?   14.在root用戶下,以普通用戶身份執(zhí)行"pwd"命令。  [root@qls ~]# su  -  qls01  -c "pwd" /home/qls01   15.什么是sudo?  用來提權(quán)  給普通用戶提權(quán)   16.可以根據(jù)哪個文件對sudo提權(quán)?   /etc/sudoers   17.登錄到普通用戶,查看/etc/shadow文件的內(nèi)容,發(fā)現(xiàn)查看不了,怎么辦?  提權(quán)   切換到root用戶查看  設置權(quán)限   18.登錄到普通用戶,刪除/opt目錄,若無法刪除,該怎么解決?  提權(quán)  切換到root用戶刪除   設置權(quán)限  要刪除一個目錄,要看上一級目錄的權(quán)限   要將一個文件移動或者拷貝到另一個目錄時,也需要查看另一個目錄的權(quán)限    19.登錄到普通用戶,使用sudo命令時,不想輸出密碼,怎么解決?  NOPASSWD:    20.怎樣查看普通用戶的sudo權(quán)限?  sudo  -l   21.配置好了sudo授權(quán),怎么檢查語法是否正確?  visudo  -c   22.禁止root用戶遠程登錄,怎么實現(xiàn)?   [root@qls ~]# grep  'RootLogin'  /etc/ssh/sshd_config PermitRootLogin no  23.修改遠程連接端口號為2222,然后進行登錄?  [root@qls ~]# grep  'Port'  /etc/ssh/sshd_config #Port 22 #GatewayPorts no [root@qls ~]# sed  -i  '/^#Port/s#.*#Port 2222#g'  /etc/ssh/sshd_config [root@qls ~]# grep  'Port'  /etc/ssh/sshd_config Port 2222 #GatewayPorts no [root@qls ~]#   [root@qls ~]# systemctl  restart  sshd    [root@qls ~]# getenforce  Disabled [root@qls ~]# setenforce  0 setenforce: SElinux is disabled [root@qls ~]# ll /etc/sysconfig/selinux  -rw-r--r--. 1 root root 543 Jul 15 20:28 /etc/sysconfig/selinux  [root@qls ~]# systemctl  stop  firewalld    ssh  qls01@10.0.0.100 2222   24.把對sshd的設置修改成原來的設置。  [root@qls ~]# sed  -i  '/^Port/s#.*##Port 22#g'  /etc/ssh/sshd_config [root@qls ~]# !grep grep  'Port'  /etc/ssh/sshd_config #Port 22 #GatewayPorts no [root@qls ~]# systemctl  restart sshd   25.說出下面幾個特殊符號的含義  * > >>  # .. . ^  $ `` !    *  #所有  >  #標準輸出重定向  會清空文件的內(nèi)容之后再將你的內(nèi)容重定向進去  >>  #標準輸出追加重定向 不會清空文件的內(nèi)容,將文件內(nèi)容追加到文件的底部    # #注釋  管理員命令行提示符   ..  #當前目錄的上一級目錄  .  #當前目錄   ^  #邏輯概念 開頭       $ #結(jié)尾 最后一行  ``  #優(yōu)先執(zhí)行反引號里面的命令,把命令的執(zhí)行結(jié)果交給外面的命令  里面必須是命令  跟  $()  !  #非  取反

9. 用戶權(quán)限練習題

1.用戶基礎權(quán)限為9位,每三位為一組,每組代表著誰的權(quán)限?  屬主    屬組    其他用戶    2.權(quán)限中的rwx-,每個字符所代表什么意思?對應的數(shù)字是什么?  r    可讀    4  w    可寫    2  x    可執(zhí)行    1  -    沒有權(quán)限  0   3.-rwxr-xr-x,寫出對應數(shù)字權(quán)限  755  4.-rwxr--r--,寫出對應數(shù)字權(quán)限  744   5.-r-xr-x--x,寫出對應數(shù)字權(quán)限  551  6.-rw-r-xr-x,寫出對應數(shù)字權(quán)限  655  7.-r--r--r--,寫出對應數(shù)字權(quán)限  444  8.-r-xr-----,寫出對應數(shù)字權(quán)限  540  9.---x-w-r--,寫出對應數(shù)字權(quán)限  124  10.-rwxr--rw-,寫出對應數(shù)字權(quán)限  746   11.-rw-r--r--,寫出對應數(shù)字權(quán)限  644  12.---xr--rwx,寫出對應數(shù)字權(quán)限  147  13.777,寫出對應字母權(quán)限  -rwxrwxrwx  14.545,寫出對應字母權(quán)限  -r-xr--r-x   15.744,寫出對應字母權(quán)限  -rwxr--r--  16.600,寫出對應字母權(quán)限  -rw-------  17.641,寫出對應字母權(quán)限  -rw-r----x  18.711,寫出對應字母權(quán)限  -rwx--x--x  19.700,寫出對應字母權(quán)限  -rwx------  20.555,寫出對應字母權(quán)限  -r-xr-xr-x  21.733,寫出對應字母權(quán)限  -rwx-wx-wx  22.713,寫出對應字母權(quán)限  -rwx--x-wx  23.建一個目錄/test,查看這個目錄的默認權(quán)限是?  755    24.進入/test目錄中,建一個文件abc,查看其默認的權(quán)限為?  644   25.創(chuàng)建一個文件test.txt,并其將權(quán)限改為600.  chmod   600    test.txt  26.將test.txt文件的權(quán)限改為755.  chmod  755  test.txt  27.將test.txt文件的權(quán)限改為000.  chmod  000   test.txt  28.修改test.txt文件的權(quán)限為644.  chmod  644   test.txt  29.給test.txt文件的屬主加上x權(quán)限。   chmod  u+x  test.txt  30.給test.txt文件的其他用戶加上x權(quán)限。  chmod  o+x  test.txt  31.去除test.txt文件的所有執(zhí)行權(quán)限。  chmod   -x  test.txt  32.給/test目錄及目錄下的所有文件或目錄的權(quán)限統(tǒng)一改為744。  chmod  -R  744  /test   33.給/test目錄及目錄下的所有文件或目錄加上x權(quán)限(屬主、屬組、其他用戶都執(zhí)行權(quán)限)。   chmod -R  +x  /test  34.使用stat命令查看test.txt文件的權(quán)限,并使用awk將權(quán)限取出來,比如:755  [root@qls ~]# stat  /etc/hosts   File: ‘/etc/hosts’   Size: 158        Blocks: 8          IO Block: 4096   regular file Device: 803h/2051d  Inode: 67125348    Links: 1 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root) Access: 2020-07-27 16:29:57.708000982 +0800 Modify: 2013-06-07 22:31:32.000000000 +0800 Change: 2020-07-06 02:13:36.028996182 +0800  Birth: - [root@qls ~]# stat  /etc/hosts | awk -F '[(/]' 'NR==4{print  $2}' 0644    35.使用stat命令查看test.txt文件的權(quán)限,并使用sed將權(quán)限取出來,比如:755  [root@qls ~]# stat  /etc/hosts | sed  -nr  '4s#(^.*()(.*)(/[-d].*$)#2#gp' 0644   [root@qls ~]# stat  /etc | sed  -nr  '4s#(^.*()(.*)(/[-d].*$)#2#gp'

10. 用戶權(quán)限練習題

1.創(chuàng)建目錄/web01,所有者是user01,所屬組是user02;創(chuàng)建目錄/web02,所有者是user02,所屬組是user01。  useradd   user01  useradd   user02  mkdir   /web01   /web02  chown  user01.user02   /web01  chown  user02.user01  /web02    2.復制/etc/fstab文件到/opt下,設置文件所有者為tomcat讀寫權(quán)限,所屬組為apps組有讀寫權(quán)限,其他人無權(quán)限   cp   /etc/fstab   /opt  groupadd  apps  useradd  tomcat   chown  tomcat.apps   /opt/fstab   chmod  660  /opt/fstab   3.將以下權(quán)限翻譯成數(shù)字,將數(shù)字權(quán)限用字母表示  rw-r-xr--  654 rw-r--r--  644 rwx--x--x  711 rw-------  600 rwxr--r--  744 rw-rw-r--  664 rwxrwxrwx  777 751     rwxr-x--x   771    rwxrwx--x   632    rw--wx-w-   551    r-xr-x--x   622    rw--w--w-   746    rwxr--rw-   644    rw-r--r--     755    rwxr-xr-x     4.假設公司研發(fā)部的用戶david和peter屬于組A,財務部的用戶life和laowang屬于組B,根據(jù)要求創(chuàng)建用戶及組。  groupadd  A   groupadd  B  useradd  -g A david  useradd  -g A peter  useradd  -g  B  life  useradd  -g  B  laowang     5.接第4題,建立目錄file1,該目錄里面的文件只能由研發(fā)部人員讀取、增加、刪除、修改以及執(zhí)行,其他用戶不能對該目錄進行任何操作  mkdir -m  770   file1  chown  .A  file1 或 chgrp  A  file1  6.接第4題,建立目錄file2,該目錄里面的文件只能由財務部人員讀取、增加、刪除、修改以及執(zhí)行,其他用戶不能對該目錄進行任何操作  mkdir file2    chown  .B  file2    chmod  770  file2  7.接第4題,建立目錄file3,該目錄里面的文件研發(fā)部人員可以讀取、增加、刪除、修改以及執(zhí)行,其他部門只能做查看操作  mkdir  -m   775   file3  chgrp  A  file3     8.接第4題,建立目錄file4,該目錄里面的文件只有研發(fā)部的經(jīng)理David擁有所有操作權(quán)限,研發(fā)部的其他人只有查看權(quán)限,其他部門不能進行任何操作   mkdir  -m  750  file4  chown  David.A    file4   9.新建用戶組,shengchan,demo,要求如下:   01、tom是shengchan組的附加成員   02、leo是demo組的附加成員   03、新建用戶admin不屬于以上任何一個組    groupadd  shengchan  groupadd  demo  useradd  -g  shengchan  tom  useradd   -g  demo   leo  useradd  admin       10.新建目錄要求如下:   01、/pub/目錄為公共存儲目錄,所有用戶可以讀寫執(zhí)行   02、/sc目錄是生產(chǎn)部的目錄,生產(chǎn)部的成員讀寫執(zhí)行,并且生產(chǎn)部人員建立的文件自動歸屬到     shengchan組中   03、admin用戶可以在/sc目錄中可以讀  mkdir -m 777  /pub  mkdir  -m  2770  /sc  chgrp  shengchan  /sc  chmod  o=rx  /sc     11.新建目錄/www01,/www02,/www03  mkdir  /www{01..03}  12.接第11題,更改/www01目錄的權(quán)限,使其他用戶對它沒有任何權(quán)限;  chmod  o=-   /www01   13.接第11題,更改/www02目錄的權(quán)限,使所屬組對它擁有讀寫執(zhí)行權(quán)限;  chmod  g=rwx  /www02   14.接第11題,更改/www03目錄的權(quán)限,任何用戶都可以讀寫,但是在/www03目錄中創(chuàng)建的任何文件都屬于grp1組  chmod   2777  /www03    groupadd  grp1  chgrp  -R  grp1  /www03   15.新建用戶zhangsan,lisi,wangergou,三個用戶都屬于同一個用戶組f4,密碼都為oldboy  groupadd  f4   useradd  -g f4  zhangsan  useradd  -g f4  lisi  useradd  -g f4  wangergou    echo "oldboy" |passwd  --stdin  zhangsan  echo "oldboy" |passwd  --stdin  lisi  echo "oldboy" |passwd  --stdin  wangergou  16.接15題,上述用戶和組都能在/data/code目錄,訪問,創(chuàng)建,刪除文件,其他用戶無法訪問該目錄  mkdir  -p  -m  770  /data/code  chown  -R  .f4  /data/code   17.接15題,/data/code目錄下創(chuàng)建的所有文件自動歸屬于f4組所有  chmod  g+s   /data/code   18.接15題,現(xiàn)在新增了一批用戶,屬于默認組,需要開放其他用戶在code目錄的讀權(quán)限  chmod  o=rx  /data/code   19.接15題,新增的所有其他用戶在code目錄下創(chuàng)建的文件自動歸屬f4組  chmod  o=rwx   /data/code   20.有兩個用戶組,分別為python組、linux組,python組的人可以修改讀取python組的文件,但不能讓linux組的人讀取;linux組的人可以修改讀取linux組的文件,但不能讓python組的人讀取。  groupadd   linux  grouadd   python    touch   python.txt    chmod  770  python.txt  chgrp  python  python.txt   touch   linux.txt    chmod  770  linux.txt  chgrp  python  linux.txt    21.在當前目錄中新建文件text,假設該文件的權(quán)限為614?,F(xiàn)要求設置該文件屬主(u)增加執(zhí)行權(quán)限,屬組(g)增加寫權(quán)限,其他用戶(o)刪除讀權(quán)限,應該如何操作,另外修改后的權(quán)限用字母應該如何表示  touch  text    chmod  614  text  chmod 730  text   -rwx-wx---    22.在當前目錄中創(chuàng)建目錄aaa,并把該目錄的權(quán)限設置為只有文件主有讀、寫和執(zhí)行權(quán)限  mkdir  -m 700  aaa  23.設某文件myfile的權(quán)限為-rw-r--r--,若要增加所有人可執(zhí)行的權(quán)限,應該怎么做  chmod +x  myfile   24.登錄到普通用戶user01,刪除/opt這個目錄,提示權(quán)限不足,如果要刪除/opt這個目錄,不使用root用戶刪除的話,你該怎么解決?  sudo提權(quán)   chmod  u+s   /usr/bin/rm     25.創(chuàng)建目錄/test,要求所有用戶對這個目錄有所有權(quán)限,現(xiàn)在只想讓每個用戶在這個目錄只能對屬于自己的文件進行操作,怎么實現(xiàn)?  mkdir  -m  1777  /test     26.創(chuàng)建一個文件/opt/file.txt,這個文件很重要,現(xiàn)要求所有人只能查看和追加內(nèi)容進去,不允許有其他的操作,怎么實現(xiàn)?  chattr  +a  /opt/file.txt   27.取消上題的設置,現(xiàn)要求所有人只能查看此文件,不允許有其他的操作,怎么實現(xiàn)?  chattr  -a  /opt/file.txt  chattr  -i  /opt/file.txt    28.請問在當前系統(tǒng)中新創(chuàng)建的目錄和文件,權(quán)限各是多少,為什么?  目錄755  文件644  因umask  控制權(quán)限     29.如果我想要在系統(tǒng)中新創(chuàng)建的目錄權(quán)限為555,文件為444,怎么實現(xiàn)?  umask   222   30.umask怎么計算目錄及文件權(quán)限的?  目錄最大777  減去umask權(quán)限   文件最大666  減去umask    文件權(quán)限遇到奇數(shù)時,在奇數(shù)為加1      31.在你修改了test.txt文件之后,如何知道在你下次要修改這個文件時,這個文件有沒有被別人修改過?  [root@qls ~]# cp  /etc/hosts  ./ [root@qls ~]# ll total 4 drwxr-xr-x 2 root root   6 Jul 27 12:12 data -rw-r--r-- 1 root root 158 Jul 28 10:29 hosts [root@qls ~]# md5sum  hosts   > figer.txt [root@qls ~]# cat figer.txt 54fb6627dbaa37721048e4549db3224d  hosts [root@qls ~]# md5sum  -c  figer.txt  hosts: OK [root@qls ~]# echo "1" >> hosts  [root@qls ~]# md5sum  -c  figer.txt  hosts: FAILED md5sum: WARNING: 1 computed checksum did NOT match    32.輸入時間命令"date"將當前系統(tǒng)時間輸出到/data/1.txt   [root@qls ~]# date Tue Jul 28 10:31:47 CST 2020 [root@qls ~]# mkdir  -p /data [root@qls ~]# date > /data/1.txt [root@qls ~]# cat /data/1.txt Tue Jul 28 10:32:22 CST 2020    33.輸入時間命令"date"將當前系統(tǒng)時間追加到/data/1.txt   [root@qls ~]# date >> /data/1.txt    34.在當前系統(tǒng)能ping通百度的情況下,使用" ping -c3 baidu.com "將返回的信息輸出到/data/1.txt  [root@qls ~]# ping -c3 baidu.com  >/data/1.txt [root@qls ~]# cat /data/1.txt PING baidu.com (39.156.69.79) 56(84) bytes of data. 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=31.7 ms 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=33.6 ms 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=30.9 ms  --- baidu.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2004ms rtt min/avg/max/mdev = 30.939/32.112/33.623/1.140 ms   35.使用“l(fā)s /ta”將錯誤的信息輸出到/data/1.txt  [root@qls ~]# ls /ta  2>/data/1.txt [root@qls ~]# cat /data/1.txt ls: cannot access /ta: No such file or directory   36.將/data/1.txt的文件內(nèi)容,標準輸出到/data/2.txt  [root@qls ~]# cat /data/1.txt  >  /data/2.txt [root@qls ~]# cat /data/2.txt ls: cannot access /ta: No such file or directory   37.使用"seq 10 50"將以0結(jié)尾的行標準輸出到3.txt   [root@qls ~]# seq  10 50 | grep  '0$'  > 3.txt [root@qls ~]# cat 3.txt 10 20 30 40 50 [root@qls ~]# seq  10 50 | sed -n '/0$/p'   10 20 30 40 50 [root@qls ~]# seq  10 50 | awk  '/0$/' 10 20 30 40 50   38.把/etc/fstab文件內(nèi)容重定向到/tmp目錄下文件名為fstab.out  [root@qls ~]# cat  /etc/fstab   > /tmp/fstab.out [root@qls ~]# cat /tmp/fstab.out  # # /etc/fstab # Created by anaconda on Mon Jul  6 02:13:15 2020 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=4aa02e85-ffe1-4783-9886-a328f923bee8 /                       xfs     defaults        0 0 UUID=70df4a5b-091c-4aaa-9cc7-d745201ec394 /boot                   xfs     defaults        0 0 UUID=6aefd444-30dc-4c0e-9a8d-75a8b1d9856b swap                    swap    defaults        0 0    39.把字符"hello world"追加到/tmp/fstab.out文件尾部   [root@qls ~]# echo  "hello world" >> /tmp/fstab.out   40.輸入df -h,取出當前系統(tǒng)根分區(qū)已用磁盤空間的百分比,并將取出來的數(shù)值輸出到/data/1.txt  [root@qls ~]# df -h Filesystem      Size  Used Avail Use% Mounted on /dev/sda3        98G  1.4G   97G   2% / devtmpfs        980M     0  980M   0% /dev tmpfs           991M     0  991M   0% /dev/shm tmpfs           991M  9.6M  981M   1% /run tmpfs           991M     0  991M   0% /sys/fs/cgroup /dev/sda1       497M  120M  378M  25% /boot tmpfs           199M     0  199M   0% /run/user/0 [root@qls ~]# df -h | awk  '//$/' /dev/sda3        98G  1.4G   97G   2% / [root@qls ~]# df -h | awk  '//$/{print $5}'  2% [root@qls ~]# df -h | awk  '//$/{print $(NF-1)}'   >/data/1.txt    41.使用命令ping測試10.0.0.100是否通暢,把輸出的結(jié)果不管是正確的還是錯誤的都追加到/data/1.txt  [root@qls ~]# ping  -c4  10.0.0.100  &> /data/1.txt  [root@qls ~]# cat /data/1.txt PING 10.0.0.100 (10.0.0.100) 56(84) bytes of data. 64 bytes from 10.0.0.100: icmp_seq=1 ttl=64 time=0.134 ms 64 bytes from 10.0.0.100: icmp_seq=2 ttl=64 time=0.038 ms 64 bytes from 10.0.0.100: icmp_seq=3 ttl=64 time=0.038 ms 64 bytes from 10.0.0.100: icmp_seq=4 ttl=64 time=0.034 ms  --- 10.0.0.100 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3000ms rtt min/avg/max/mdev = 0.034/0.061/0.134/0.042 ms   42.使用重定向從文件/etc/passwd中讀取數(shù)據(jù)。   [root@qls ~]# cat < /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  43.用 << 舉個命令的使用例子。   [root@qls ~]# cat >123.txt<<EOF > 12 > EOF [root@qls ~]# cat 123.txt  12    44.改變文件所有者的命令是?  chown  45.新建用戶oldboy,oldgirl,屬同一用戶組edu  groupadd   edu  useradd  -g edu  oldboy   useradd  -g edu  oldgirl   46.修改文件1.txt的所有者為oldboy,屬組為edu  chown  oldboy.edu  1.txt   47.除所有者以外,屬組和其他用戶均沒有任何權(quán)限(要求普通用戶進行驗證)    48.復制/etc/fstab文件到/var/tmp下,設置文件所有者為wangcai讀寫權(quán)限,所屬組為sysadmins組有讀寫權(quán)限,其他人無權(quán)限  cp  /etc/fstab  /var/tmp  useradd  wangcai  groupadd   sysadmins     chmod  660  /var/tmp/fstab  49.創(chuàng)建下面的用戶、組和組成員關(guān)系,用戶natasha,使用admins作為附屬組,用戶harry,也使用admins作為附屬組,用戶sh,不可以登錄系統(tǒng),natasha,harry,sh密碼都是centos  groupadd   admins  useradd    -G  admins   natasha  useradd    -G  admins   harry    useradd  -M  -s  /sbin/nologin    echo "centos" | passwd   --stdin  natasha  echo "centos" | passwd   --stdin  harry  echo "centos" | passwd   --stdin  sh   50.新建一個用戶rose,uid為555,gid為500,注釋信息為:linux,家目錄在/rose  groupadd  -g  500  rose  useradd  -u 555  -g 500  -c "linux"  -d  /rose   rose     51.翻譯題 01).command not found         #沒有這個命令         02).No such file or directory     #沒有這個文件或者目錄         03).Permission denied         #權(quán)限不足         04).overwrite           #覆蓋        05).File exists           #文件已存在               06).Is a directory          #這是個目錄             07).Not a directory         #這不是個目錄                    08).Warning: Changing a readonly file   #警告:改變了一個只讀文件     09).Found a swap file by the name ".1.swp"  #找到一個交換文件,他的名字是.1.swp 10).unrecognized option '--oldboy'      #未知的選項  --oldboy 11).Operation not permitted         #操作不允許  12).invalid option              #無效的選項

11.用戶權(quán)限練習題

1.找出/tmp目錄下,屬主不是root,且文件名不以f開頭的文件   find  /tmp  -type f   !  -user  root  -name "[^f]*"   2.查找/etc/目錄下,所有.conf后綴的文件  find  /etc/   -type f  -name "*.conf"  3.查找/var目錄下屬主為root,且屬組為mail的所有文件  find  /var  -user  root  -a  -group  mail     [root@qls ~]# find  /var  -user  root  -a  -group  mail    -ls 201345345    4 drwxrwxr-x   2 root     mail         4096 Jul 24 09:08 /var/spool/mail 201696091    4 -rw-------   1 root     mail         1857 Jul 22 11:11 /var/spool/mail/root     4.查找/var目錄下7天以前,同時屬主不為root,也不是postfix的文件   find  /var  -type  f  -mtime +7 ! -user  root  -a  ! -user postfix  [root@qls ~]# find  /var  -type  f  -mtime +7  ! ( -user  root  -o -user postfix  ) -ls    5.查找/etc目錄下大于1M且類型為普通文件的所有文件  find  /etc   -type  f  -size +1M     6.查找/etc目錄下所有用戶都沒有寫權(quán)限的文件   [root@qls ~]# find  /etc/   !  -perm    /222  -ls    7.查找/目錄下最后創(chuàng)建時間是3天前,后綴是*.log的文件  find  /  -type  f   -mtime  +3   -name "*.log"  8.查找/目錄下文件名包含txt的文件   find  /  -type   -name  "*txt*"  9.查找/目錄下屬主是oldboy并且屬組是oldboy的文件  find   /    -user  oldboy  -group  oldboy     10.查找/目錄下屬主是oldboy但是屬組不是oldboy的文件  find  /  -user oldboy  ! -group  oldboy  11.查找/目錄下屬主是oldboy或者屬主是oldgirl的文件  find  /   (  -user  oldboy  -o  -user  oldgirl  )   12.查找/tmp目錄下屬主既不是oldboy,也不是oldgirl的文件  find  /tmp  -type  f   ! -user oldboy -a  ! -user oldgirl  [root@qls ~]# find  /tmp  -type  f    ! ( -user  oldboy  -o -user oldgirl  ) -ls    13.查找/var/log目錄下7天以前的文件  find  /var/log  -type  f    -mtime  +7     14.查找/tmp目錄下15天以前的文件刪除  find  /tmp   -type  f   -mtime  +15  -delete  find  /tmp   -type  f   -mtime  +15  | xargs  rm -f   find  /tmp   -type  f   -mtime  +15  -exec  rm -f  {}  ;  rm -f  $(find  /tmp   -type  f   -mtime  +15 )  rm -f `find  /tmp   -type  f   -mtime  +15 `  15.查找/home目錄下,類型是目錄的,并且屬主是oldboy的目錄  find  /home  -type  d  -user  oldboy   16.查找/var/log下大于100kb且以log結(jié)尾的所有文件  find  /var/log  -type f  -size +100k  -name "*log"   17.查找tmp目錄下所屬組group1,所屬主user1的目錄  find  /tmp -type  d  -user user1  -a  -group  group1    18.同時查找根目錄下名為1.txt,2.txt的文件和名字帶a的目錄  [root@qls ~]# find  /data  -type  f  -name "[12].txt"  -o -type  d  -name "*a*" /data /data/1.txt /data/2.txt  [root@qls ~]# find  /data ( -type  f  -name "[12].txt"  -o -type  d  -name "*a*" ) -ls 818813    0 drwxrws---   3 root     ops_group      147 Jul 28 10:35 /data 818804    4 -rw-r--r--   1 root     ops_group      432 Jul 28 10:44 /data/1.txt 818807    4 -rw-r--r--   1 root     ops_group       49 Jul 28 10:35 /data/2.txt   19.查找/tmp目錄下所有文件并刪除  find  /tmp  -type f  -delete    20.查找/etc目錄下至少有一類用戶沒有寫權(quán)限的文件  find  /etc -type  f   ! -perm   -222  [root@qls ~]# find  /etc  -type  f   ! -perm   -222  -ls  21.查找/var目錄下不屬于root、lp、gdm的所有文件  find   /var  -type  f  ! (  -user root -o -user lp  -o -user gdm  )  [root@qls ~]# find   /var  -type  f  ! (  -user root -o -user lp  -o -user gdm  )  -ls    22.將/etc/中的所有目錄(僅目錄)復制到/tmp下,目錄結(jié)構(gòu)不變  [root@qls ~]# find  /etc -type  d  |  xargs -I  {}  mkdir  -p  /tmp/{}   [root@qls ~]# find  /etc/ -type  d   -exec  mkdir  -p  /opt/{}  ;     23.將/etc目錄復制到/var/tmp/,/var/tmp/etc的所有目錄權(quán)限777  /var/tmp/etc目錄中所有文件權(quán)限666  cp  -a  /etc  /var/tmp/  find  /var/tmp/etc/   -type  d  -exec  chmod 777  {}  ;  find  /var/tmp/etc/   -type  f  -exec  chmod  666  {} ;   24.保留/var/log/下最近7天的日志文件,其他全部刪除  find  /var/log   ! -mtime  -7  -delete    25.創(chuàng)建touch file{1..10}10個文件, 保留file9,其他一次全部刪除  [root@qls opt]# find  ./  -type f   !  -name  "file9"  -delete    [root@qls opt]# find  ./  -type f  -name  "file[^9]*"  -delete  26.解釋如下每條命令含義   mkdir /root/dir1    #創(chuàng)建目錄   touch /root/dir1/file{1..10}  #創(chuàng)建文件   find /root/dir1 -type f -name "file5"  #查找文件名為file5的文件   find /root/dir1 ! -name "file5"    #查找文件名不是file5的文件   find /root/dir1 -name "file5" -o -name "file9"  #查找文件名是file5或者是file9的文件   #錯誤寫法 find /root/dir1 -name "file5" -o -name "file9" -ls  #查找文件名是file5或者是file9的文件并顯示詳細信息   find /root/dir1 ( -name "file5" -o -name "file9" ) -ls #查找文件名是file5或者是file9的文件并顯示詳細信息    find /root/dir1 ( -name "file5" -o -name "file9" ) -exec rm -rvf {} ;  #刪除file5或者file9  find /root/dir1 ! ( -name "file4" -o -name "file8" ) -exec rm -vf {} ;  #刪除文件名不叫file4或者file8的文件

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊11 分享