5. 第四章 Linux用戶組和權限管理


一.第一部分 用戶和組

1.創建用戶gentoo,附加組為bin和root,默認shell為/bin/csh,注釋信息為“Gentoo Distribution”

[root@centos8 ~]# useradd -G bin,root -s /bin/csh -c "“Gentoo Distribution”" geetoo
[root@centos8 ~]# getent passwd geetoo
geetoo:x:1001:1001:“Gentoo Distribution”:/home/geetoo:/bin/csh
[root@centos8 ~]# id geetoo
uid=1001(geetoo) gid=1001(geetoo) groups=1001(geetoo),1(bin),0(root)

2.創建下面的用戶、組和組成員

​ 名字為webs的組

​ 用戶nginx,使用webs作為附加組

​ 用戶varnish,使用webs作為附加組

​ 用戶mysql,不可交互登錄系統,且不是webs的成員,nginx,varnish,mysql密碼都是magede

[root@centos8 ~]# groupadd webs
[root@centos8 ~]# getent group webs
webs:x:1002:

[root@centos8 ~]# useradd -G webs nginx
[root@centos8 ~]# getent passwd nginx
nginx:x:1002:1003::/home/nginx:/bin/bash
[root@centos8 ~]# id nginx
uid=1002(nginx) gid=1003(nginx) groups=1003(nginx),1002(webs)

[root@centos8 ~]# useradd -G webs varnish
[root@centos8 ~]# getent passwd varnish
varnish:x:1003:1004::/home/varnish:/bin/bash
[root@centos8 ~]# id varnish
uid=1003(varnish) gid=1004(varnish) groups=1004(varnish),1002(webs)

[root@centos8 ~]# useradd -s /sbin/nologin mysql
[root@centos8 ~]# getent passwd mysql
mysql:x:1004:1005::/home/mysql:/sbin/nologin
[root@centos8 ~]# id mysql
uid=1004(mysql) gid=1005(mysql) groups=1005(mysql)
[root@centos8 ~]# echo magedu | passwd --stdin nginx
Changing password for user nginx.
passwd: all authentication tokens updated successfully.
[root@centos8 ~]# echo magedu | passwd --stdin varnish
Changing password for user varnish.
passwd: all authentication tokens updated successfully.
[root@centos8 ~]# echo magedu | passwd --stdin mysql
Changing password for user mysql.
passwd: all authentication tokens updated successfully.
[root@centos8 ~]# getent shadow nginx
nginx:$6$7r6YkegEx2W26SMM$f3YETfxOYLPZGes2fjZezD4vn2ITOYVOMG3pRlkGh1gACjUh5QZmzgQUdJwdEYywg8qnTRLf9NkjRJ04YpH3U0:18586:0:99999:7:::
[root@centos8 ~]# getent shadow varnish
varnish:$6$hYCSWUVx7egt6s72$PlpwJAVY61feV19uInUesGVm1iG9eTLjDuLRben9A44uSMlPe9qKDG2y5mQV0gW92jcYRB/zuUHNUuT6rk8V01:18586:0:99999:7:::
[root@centos8 ~]# getent shadow mysql
mysql:$6$BfZ8lKoLQE4VzSyf$UpAgw63Hv.wd7SYMZIG7WBYp98d0KuZFG7XJn1xdFk1Of3n3lkTfUsEfvtUdW1VMnLq5/06Wji85GD/ksXSO4.:18586:0:99999:7:::

二.第二部分 文件權限

1.當用戶docker對/testdir目錄無執行權限時,意味着無法做那些操作?

[root@centos8 data]# useradd docker
[root@centos8 data]# echo 123456 |passwd --stdin docker
Changing password for user docker.
passwd: all authentication tokens updated successfully.
[root@centos8 data]# chown docker testdir/
[root@centos8 data]# ll -d testdir/
drwxr-xr-x. 2 docker root 6 Nov 20 21:14 testdir/
[root@centos8 data]# chmod u-x testdir/
[root@centos8 data]# ll -d testdir/
drw-r-xr-x. 2 docker root 6 Nov 20 21:14 testdir/
[root@centos8 data]# touch testdir/a.txt
[root@centos8 data]# su docker
[docker@centos8 data]$ cd testdir/
bash: cd: testdir/: Permission denied
[docker@centos8 data]$ touch testdir/a.txt
touch: cannot touch 'testdir/a.txt': Permission denied
[docker@centos8 data]$ rm -rf testdir/
rm: cannot remove 'testdir/': Permission denied
[docker@centos8 data]$ cat testdir/a.txt
cat: testdir/a.txt: Permission denied
[docker@centos8 data]$ echo docker data >> testdir/a.txt
bash: testdir/a.txt: Permission denied
[docker@centos8 data]$ echo docker data > testdir/a.txt
bash: testdir/a.txt: Permission denied

#結論:沒有讀權限,沒有寫權限,當然也就沒有執行權限。

2.當用戶mongodb對/testdir目錄無讀權限時,意味着無法做那些操作?

[root@centos8 data]# useradd mongodb 
[root@centos8 data]# ls
a.txt  testdir
[root@centos8 data]# ll -d
drwxr-xr-x. 3 root root 34 Nov 20 21:14 .
[root@centos8 data]# ll -d testdir/
drw-r-xr-x. 2 docker root 19 Nov 20 21:21 testdir/
[root@centos8 data]# chown mongodb testdir/
[root@centos8 data]# ll -d testdir/
drw-r-xr-x. 2 mongodb root 19 Nov 20 21:21 testdir/
[root@centos8 data]# chmod u=wx testdir/
[root@centos8 data]# ll -d testdir/
d-wxr-xr-x. 2 mongodb root 19 Nov 20 21:21 testdir/
[root@centos8 data]# su mongodb
[mongodb@centos8 data]$ cd testdir/
[mongodb@centos8 testdir]$ touch b.txt
[mongodb@centos8 testdir]$ echo mongodb data  >> b.txt 
[mongodb@centos8 testdir]$ cat b.txt
mongodb data
[mongodb@centos8 testdir]$ cp b.txt ~/
[mongodb@centos8 testdir]$ rm -f b.txt

#結論:什么操作都能做,因為other有r權限

3.當redis對/testdir目錄無寫權限時,該目錄下的只讀文件file1是否可修改和刪除?

[root@centos8 data]# ll -d testdir/
d-wxr-xr-x. 2 mongodb root 19 Nov 20 21:28 testdir/
[root@centos8 data]# useradd redis
[root@centos8 data]# chown redis testdir/
[root@centos8 data]# ll -d testdir/
d-wxr-xr-x. 2 redis root 19 Nov 20 21:28 testdir/
[root@centos8 data]# chmod u=rx testdir/
[root@centos8 data]# ll -d testdir/
dr-xr-xr-x. 2 redis root 19 Nov 20 21:28 testdir/
[root@centos8 data]# touch testdir/file1
[root@centos8 data]# chmod 400 testdir/file1 
[root@centos8 data]# ll testdir/file1 
-r--------. 1 root root 0 Nov 20 21:34 testdir/file1
[redis@centos8 data]$ echo xx >>testdir/file1
bash: testdir/file1: Permission denied
[redis@centos8 data]$ rm -f testdir/file1 
rm: cannot remove 'testdir/file1': Permission denied

#結論不能修改,也不能刪除

4.當用戶zabbix對/testdir目錄有寫和執行權限時,該目錄下的只讀文件file1是否可修改和刪除?

[root@centos8 data]# useradd zabbix
[root@centos8 data]# chown zabbix testdir
[root@centos8 data]# ll -d testdir/
dr-xr-xr-x. 2 zabbix root 32 Nov 20 21:34 testdir/
[root@centos8 data]# chmod u=wx testdir/
[root@centos8 data]# ll -d testdir/
d-wxr-xr-x. 2 zabbix root 32 Nov 20 21:34 testdir/
[root@centos8 data]# ll -d testdir/file1 
-r--------. 1 root root 0 Nov 20 21:34 testdir/file1

[root@centos8 data]# su zabbix
[zabbix@centos8 data]$ echo xx >>testdir/file1
bash: testdir/file1: Permission denied
[zabbix@centos8 data]$ rm -f testdir/file1

#結論:不能修改,可以刪除

5.復制/etc/fstab文件到/var/tmp下,設置文件所有者為tomcat讀寫權限,所屬組為apps 組有讀寫權限,其他人無權限

[root@centos8 data]# cp /etc/fstab /var/tmp
[root@centos8 data]# ll /var/tmp/fstab 
-rw-r--r--. 1 root root 709 Nov 20 21:40 /var/tmp/fstab
[root@centos8 data]# useradd tomcat
[root@centos8 data]# groupadd apps
[root@centos8 data]# chown tomcat.apps /var/tmp/fstab
[root@centos8 data]# ll /var/tmp/fstab 
-rw-r--r--. 1 tomcat apps 709 Nov 20 21:45 /var/tmp/fstab
[root@centos8 data]# chmod 660 /var/tmp/fstab
[root@centos8 data]# ll /var/tmp/fstab 
-rw-rw----. 1 tomcat apps 709 Nov 20 21:45 /var/tmp/fstab

6.誤刪除了用戶git的家目錄,請重建並恢復該用戶家目錄及相應的權限屬性

[root@centos8 data]# useradd git
[root@centos8 data]# ll -d /home/git
drwx------. 3 git git 78 Nov 20 21:51 /home/git
[root@centos8 data]# ll -a /home/git
total 12
drwx------.  3 git  git   78 Nov 20 21:51 .
drwxr-xr-x. 10 root root 113 Nov 20 21:51 ..
-rw-r--r--.  1 git  git   18 Nov  9  2019 .bash_logout
-rw-r--r--.  1 git  git  141 Nov  9  2019 .bash_profile
-rw-r--r--.  1 git  git  312 Nov  9  2019 .bashrc
drwxr-xr-x.  4 git  git   39 Nov 20 13:27 .mozilla
[root@centos8 data]# rm -rf /home/git
[root@centos8 data]# ll /home
total 0
drwx------. 3 docker   docker    99 Nov 20 21:14 docker
drwx------. 3 hf       hf       120 Nov 20 16:49 hf
drwx------. 3 mongodb  mongodb  112 Nov 20 21:31 mongodb
drwx------. 3 neteagle neteagle  99 Nov 20 15:59 neteagle
drwx------. 3 redis    redis     99 Nov 20 21:33 redis
drwx------. 3 tomcat   tomcat    78 Nov 20 21:41 tomcat
drwx------. 3 zabbix   zabbix    99 Nov 20 21:40 zabbix

[root@centos8 data]# ll -a /etc/skel/
total 24
drwxr-xr-x.   3 root root   78 Nov 20 13:28 .
drwxr-xr-x. 135 root root 8192 Nov 20 21:51 ..
-rw-r--r--.   1 root root   18 Nov  9  2019 .bash_logout
-rw-r--r--.   1 root root  141 Nov  9  2019 .bash_profile
-rw-r--r--.   1 root root  312 Nov  9  2019 .bashrc
drwxr-xr-x.   4 root root   39 Nov 20 13:27 .mozilla
[root@centos8 data]# cp -a /etc/skel/ /home/git
[root@centos8 data]# ll -a /home/git
total 12
drwxr-xr-x.  3 root root  78 Nov 20 13:28 .
drwxr-xr-x. 10 root root 113 Nov 20 21:59 ..
-rw-r--r--.  1 root root  18 Nov  9  2019 .bash_logout
-rw-r--r--.  1 root root 141 Nov  9  2019 .bash_profile
-rw-r--r--.  1 root root 312 Nov  9  2019 .bashrc
drwxr-xr-x.  4 root root  39 Nov 20 13:27 .mozilla

[root@centos8 data]# ll -d /home/git
drwxr-xr-x. 3 root root 78 Nov 20 13:28 /home/git
[root@centos8 data]# chown git.git /home/git
[root@centos8 data]# ll -d /home/git
drwxr-xr-x. 3 git git 78 Nov 20 13:28 /home/git
[root@centos8 data]# chmod 700 /home/git
[root@centos8 data]# ll -d /home/git
drwx------. 3 git git 78 Nov 20 13:28 /home/git

三.第三部分 特殊權限

1.在/testdir/dir里創建的新文件自動屬於webs組,組apps的成員如下:tomcat能對這些新文件有讀寫權限,組dbs的成員如下:mysql只能對新文件有讀權限,其它用戶(不屬於webs,apps,dbs)不能訪問這個文件夾

[root@centos8 data]# mkdir -p testdir/dir
[root@centos8 data]# groupadd webs
[root@centos8 data]# chown :webs testdir/dir/
[root@centos8 data]# ll -d testdir/dir/
drwxr-xr-x. 2 root webs 6 Nov 20 22:05 testdir/dir/
[root@centos8 data]# chmod g+s testdir/dir/
[root@centos8 data]# ll -d testdir/dir/
drwxr-sr-x. 2 root webs 6 Nov 20 22:05 testdir/dir/
[root@centos8 data]# touch testdir/dir/a.txt
[root@centos8 data]# ll testdir/dir/a.txt
-rw-r--r--. 1 root webs 0 Nov 20 22:07 testdir/dir/a.txt

[root@centos8 data]# id tomcat
uid=1006(tomcat) gid=1007(tomcat) groups=1007(tomcat)
[root@centos8 data]# groupmems -l -g apps
[root@centos8 data]# groupmems -a tomcat -g apps
[root@centos8 data]# groupmems -l -g apps
tomcat 
[root@centos8 data]# setfacl -m u:tomcat:rw a.txt 
[root@centos8 data]# getfacl 
Usage: getfacl [-aceEsRLPtpndvh] file ...
Try `getfacl --help' for more information.
[root@centos8 data]# getfacl a.txt 
# file: a.txt
# owner: root
# group: root
user::rw-
user:tomcat:rw-
group::r--
mask::rw-


[root@centos8 data]# groupmems -l -g dbs
groupmems: group 'dbs' does not exist in /etc/group
[root@centos8 data]# groupadd dbs
[root@centos8 data]# groupmems -l -g dbs
[root@centos8 data]# groupmems -a mysql  -g dbs
groupmems: user 'mysql' does not exist
[root@centos8 data]# useradd mysql
[root@centos8 data]# groupmems -a mysql  -g dbs
[root@centos8 data]# groupmems -l -g dbs
mysql 
[root@centos8 data]# setfacl -m u:mysql:r a.txt
[root@centos8 data]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
user:tomcat:rw-
user:mysql:r--
group::r--
mask::rw-
other::r--

[root@centos8 data]# setfacl -m o:- testdir/dir/
[root@centos8 data]# getfal testdir/dir/
bash: getfal: command not found...
[root@centos8 data]# getfacl testdir/dir/
# file: testdir/dir/
# owner: root
# group: webs
# flags: -s-
user::rwx
group::r-x
other::---

2.誤將/bin/chmod文件的執行權限刪除,如何恢復?

[root@centos8 data]# ll /bin/chmod
-rwxr--r--. 1 root root 133952 Nov 20 22:26 /bin/chmod
[root@centos8 data]# chmod a-x /bin/chmod	#去掉/bin/chmod執行權限。
[root@centos8 data]# ll /bin/chmod
-rw-r--r--. 1 root root 133952 Apr 10  2020 /bin/chmod	#現在已經沒有執行權限
[root@centos8 data]# chmod u+x /bin/chmod
-bash: /usr/bin/chmod: Permission denied	#賦予執行權限,提示權限不足。

[root@centos8 data]# install /bin/chmod -m 744 ./chmod	#在用install指令拷貝時指定權限,趁此時假加入執行權限
[root@centos8 data]# ll
total 136
-rw-rw-r--+ 1 root root      3 Nov 20 20:56 a.txt
-rwxr--r--. 1 root root 133952 Nov 20 22:26 chmod
drwxr-xr-x. 3 root root     17 Nov 20 22:05 testdir
[root@centos8 data]# mv ./chmod /bin/chmod	#用mv將先前的chmod 移動到/bin/chmod,覆蓋。
mv: overwrite '/bin/chmod'? y
[root@centos8 data]# ll /bin/chmod
-rwxr--r--. 1 root root 133952 Nov 20 22:26 /bin/chmod
#現在/bin/chomd 權限恢復


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM