s43.Linux用户组和权限管理

1,424 阅读7分钟

“ 本文正在参加「金石计划 . 瓜分6万现金大奖」 ”

一.第一部分 用户和组

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 权限恢复