【Linux】文件及用户组合权限管理

第二周
1、显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录

 

ls -a /etc/[^[:alpha:]][:alpha:]*

2、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。

[root@temp-ubuntu-1804-serrver:~]# cp -r /etc/p*[^0-9] /tmp/mytest/
[root@temp-ubuntu-1804-serrver:~]# ll /tmp/mytest/
total 52
drwxr-xr-x  7 root root 4096 Jul 14 23:28 ./
drwxrwxrwt 11 root root 4096 Jul 14 23:29 ../
-rw-r--r--  1 root root  552 Jul 14 23:28 pam.conf
drwxr-xr-x  2 root root 4096 Jul 14 23:28 pam.d/
-rw-r--r--  1 root root 1572 Jul 14 23:28 passwd
-rw-r--r--  1 root root 1512 Jul 14 23:28 passwd-
drwxr-xr-x  4 root root 4096 Jul 14 23:28 perl/
drwxr-xr-x  3 root root 4096 Jul 14 23:28 pm/
drwxr-xr-x  2 root root 4096 Jul 14 23:28 pollinate/
-rw-r--r--  1 root root  350 Jul 14 23:28 popularity-contest.conf
-rw-r--r--  1 root root  581 Jul 14 23:28 profile
drwxr-xr-x  2 root root 4096 Jul 14 23:28 profile.d/
-rw-r--r--  1 root root 2932 Jul 14 23:28 protocols

 

3、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

[root@temp-ubuntu-1804-serrver:~]# cat /etc/issue | tr [a-z] [A-Z] >> /tmp/issue.out
[root@temp-ubuntu-1804-serrver:~]# cat /etc/issue
Ubuntu 18.04.5 LTS \n \l

[root@temp-ubuntu-1804-serrver:~]# cat /tmp/issue.out 
UBUNTU 18.04.5 LTS \N \L

 

4、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组,其GID为2019;

[root@temp-ubuntu-1804-serrver:~]# groupadd -g 2019 distro
[root@temp-ubuntu-1804-serrver:~]# cat /etc/group | grep distro
distro:x:2019:

 

(2)、创建用户mandriva, 其ID号为1005;基本组为distro;

[root@temp-ubuntu-1804-serrver:~]# useradd -u 1005 -g distro mandriva
[root@temp-ubuntu-1804-serrver:~]# cat /etc/passwd | grep mandriva
mandriva:x:1005:2019::/home/mandriva:/bin/sh
[root@temp-ubuntu-1804-serrver:~]# 

 

(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;

[root@temp-ubuntu-1804-serrver:~]# useradd -u 1100 -d /home/linux mageia
[root@temp-ubuntu-1804-serrver:~]# cat /etc/passwd | grep mageia
mageia:x:1100:1100::/home/linux:/bin/sh
[root@temp-ubuntu-1804-serrver:~]# 

 

(4)、给用户mageia添加密码,密码为mageedu,并设置用户密码7天后过期

[root@temp-ubuntu-1804-serrver:~]# passwd -x 7 mageia
passwd: password expiry information changed.
[root@temp-ubuntu-1804-serrver:~]# echo "mageia":"mageedu" | chpasswd
[root@temp-ubuntu-1804-serrver:~]# cat /etc/shadow | grep mageia
mageia:$6$cHRZpRSP$jDRziXS27By5mjPfjSMIF/4uJ9eUyPskeOKf4vH8W9awYDmH1axLvjX5WNsJdbJKPIatu6EVvmRtL9y1uQAmU/:18822:0:7:7:::
[root@temp-ubuntu-1804-serrver:~]#

 

(5)、删除mandriva,但保留其家目录;

[root@temp-ubuntu-1804-serrver:~]# userdel mandriva 
[root@temp-ubuntu-1804-serrver:~]# ll -d /home/mandriva
drwxr-xr-x 2 1005 distro 4096 Jul 15 00:19 /home/mandriva/
[root@temp-ubuntu-1804-serrver:~]# cat /etc/passwd | grep mandriva
[root@temp-ubuntu-1804-serrver:~]# 

 


(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;

[root@temp-ubuntu-1804-serrver:~]# cat /etc/group | grep peguin
[root@temp-ubuntu-1804-serrver:~]# groupadd peguin
[root@temp-ubuntu-1804-serrver:~]# cat /etc/group | grep peguin
peguin:x:2020:
[root@temp-ubuntu-1804-serrver:~]# useradd -u 2002 -g distro -G peguin slackware
[root@temp-ubuntu-1804-serrver:~]# cat /etc/passwd | grep slackware
slackware:x:2002:2019::/home/slackware:/bin/sh
[root@temp-ubuntu-1804-serrver:~]# id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro),2020(peguin)
[root@temp-ubuntu-1804-serrver:~]# 

(7)、修改slackware的默认shell为/bin/tcsh;

[root@temp-ubuntu-1804-serrver:~]# usermod -s /bin/tcsh slackware
[root@temp-ubuntu-1804-serrver:~]# cat /etc/passwd | grep slackware
slackware:x:2002:2019::/home/slackware:/bin/tcsh
[root@temp-ubuntu-1804-serrver:~]# 

(8)、为用户slackware新增附加组admins,并设置不可登陆。

[root@temp-ubuntu-1804-serrver:~]# groupadd admins
[root@temp-ubuntu-1804-serrver:~]# usermod -s /usr/sbin/nologin -G admins slackware 
[root@temp-ubuntu-1804-serrver:~]# id slackware 
uid=2002(slackware) gid=2019(distro) groups=2019(distro),2021(admins)
[root@temp-ubuntu-1804-serrver:~]# cat /etc/passwd | grep slackware
slackware:x:2002:2019::/home/slackware:/usr/sbin/nologin
[root@temp-ubuntu-1804-serrver:~]# 

 

5、创建用户user1、user2、user3。在/data/下创建目录test

[root@temp-ubuntu-1804-serrver:~]# useradd user1 user2 user3
[root@temp-ubuntu-1804-serrver:~]# mkdir /data/test -p
[root@temp-ubuntu-1804-serrver:~]# useradd user1
[root@temp-ubuntu-1804-serrver:~]# useradd user2
[root@temp-ubuntu-1804-serrver:~]# useradd user3
[root@temp-ubuntu-1804-serrver:~]# cat /etc/passwd | grep user
user1:x:2003:2003::/home/user1:/bin/sh
user2:x:2004:2004::/home/user2:/bin/sh
user3:x:2005:2005::/home/user3:/bin/sh
[root@temp-ubuntu-1804-serrver:~]# cd /data
[root@temp-ubuntu-1804-serrver:data]# ls
test
[root@temp-ubuntu-1804-serrver:data]# ll
total 12
drwxr-xr-x 3 root root 4096 Jul 15 00:37 ./
drwxr-xr-x 25 root root 4096 Jul 15 00:37 ../
drwxr-xr-x 2 root root 4096 Jul 15 00:37 test/

 

(1)、目录/data/test属主、属组为user1

[root@temp-ubuntu-1804-serrver:data]# chown user1:user1 test/
[root@temp-ubuntu-1804-serrver:data]# ll
total 12
drwxr-xr-x  3 root  root  4096 Jul 15 00:37 ./
drwxr-xr-x 25 root  root  4096 Jul 15 00:37 ../
drwxr-xr-x  2 user1 user1 4096 Jul 15 00:37 test/
[root@temp-ubuntu-1804-serrver:data]# 

 

(2)、在目录属主、属组不变的情况下,user2对文件有读写权限

[root@temp-ubuntu-1804-serrver:data]# chmod o+w test/
[root@temp-ubuntu-1804-serrver:data]# ll
total 12
drwxr-xr-x  3 root  root  4096 Jul 15 00:37 ./
drwxr-xr-x 25 root  root  4096 Jul 15 00:37 ../
drwxr-xrwx  2 user1 user1 4096 Jul 15 00:37 test/
[root@temp-ubuntu-1804-serrver:data]# 

(3)、user1在/data/test目录下创建文件a1.sh, a2.sh, a3.sh, a4.sh,设置所有用户都不可删除1.sh,2.sh文件、除了user1及root之外,所有用户都不可删除a3.sh, a4.sh

user1@temp-ubuntu-1804-serrver:/data/test$ touch a{1..4}.sh
user1@temp-ubuntu-1804-serrver:/data/test$ ll
ll: command not found
user1@temp-ubuntu-1804-serrver:/data/test$ ls
a1.sh  a2.sh  a3.sh  a4.sh
user1@temp-ubuntu-1804-serrver:/data/test$ ls -l
total 0
-rw-rw-r-- 1 user1 user1 0 Jul 15 22:54 a1.sh
-rw-rw-r-- 1 user1 user1 0 Jul 15 22:54 a2.sh
-rw-rw-r-- 1 user1 user1 0 Jul 15 22:54 a3.sh
-rw-rw-r-- 1 user1 user1 0 Jul 15 22:54 a4.sh
[root@temp-ubuntu-1804-serrver:test]# chattr +a a[12].sh
[root@temp-ubuntu-1804-serrver:test]# lsattr
--------------e--- ./a3.sh
-----a--------e--- ./a2.sh
--------------e--- ./a4.sh
-----a--------e--- ./a1.sh
[root@temp-ubuntu-1804-serrver:test]$ chmod 755 ../test
[root@temp-ubuntu-1804-serrver:test]$ ll ..
total 12
drwxr-xr-x 3 root root 4096 Jul 15 00:37 ./
drwxr-xr-x 25 root root 4096 Jul 15 00:37 ../
drwxr-xr-x 2 user1 user1 4096 Jul 15 23:25 test/

验证权限

[user2@temp-ubuntu-1804-serrver:test]$ ls
a1.sh  a2.sh  a3.sh  a4.sh
[user2@temp-ubuntu-1804-serrver:test]$ rm -f a*.sh
rm: cannot remove a1.sh: Permission denied
rm: cannot remove a2.sh: Permission denied
rm: cannot remove a3.sh: Permission denied
rm: cannot remove a4.sh: Permission denied
[user2@temp-ubuntu-1804-serrver:test]$ exit
[root@temp-ubuntu-1804-serrver:test]# su user3
[user3@temp-ubuntu-1804-serrver:test]$ rm -f a*.sh
rm: cannot remove a1.sh: Permission denied
rm: cannot remove a2.sh: Permission denied
rm: cannot remove a3.sh: Permission denied
rm: cannot remove a4.sh: Permission denied
[user3@temp-ubuntu-1804-serrver:test]$ exit
[root@temp-ubuntu-1804-serrver:test]# su user1
[user1@temp-ubuntu-1804-serrver:test]$ ls
a1.sh  a2.sh  a3.sh  a4.sh
[user1@temp-ubuntu-1804-serrver:test]$ rm -f a*.sh
rm: cannot remove a1.sh: Operation not permitted
rm: cannot remove a2.sh: Operation not permitted
[user1@temp-ubuntu-1804-serrver:test]$ ls
a1.sh  a2.sh
[user1@temp-ubuntu-1804-serrver:test]$ touch a{3,4}.sh
[user1@temp-ubuntu-1804-serrver:test]$ ls
a1.sh  a2.sh  a3.sh  a4.sh
[user1@temp-ubuntu-1804-serrver:test]$ exit
[root@temp-ubuntu-1804-serrver:test]# ls
a1.sh  a2.sh  a3.sh  a4.sh
[root@temp-ubuntu-1804-serrver:test]# rm -f a*.sh
rm: cannot remove a1.sh: Operation not permitted
rm: cannot remove a2.sh: Operation not permitted
[root@temp-ubuntu-1804-serrver:test]# ls
a1.sh  a2.sh

 

(4)、user3增加附加组user1,同时要求user1不能访问/data/test目录及其下所有文件

[root@temp-ubuntu-1804-serrver:test]# usermod -G user1 user3
[root@temp-ubuntu-1804-serrver:test]# id user3
uid=2005(user3) gid=2005(user3) groups=2005(user3),2003(user1)
[root@temp-ubuntu-1804-serrver:data]$ ll
total 12
drwxr-xr-x  3 root  root  4096 Jul 15 00:37 ./
drwxr-xr-x 25 root  root  4096 Jul 15 00:37 ../
drwxr-xr-x  2 user1 user1 4096 Jul 15 23:35 test/
[root@temp-ubuntu-1804-serrver:data]$ chmod 705 test
[root@temp-ubuntu-1804-serrver:data]$ ll
total 12
drwxr-xr-x  3 root  root  4096 Jul 15 00:37 ./
drwxr-xr-x 25 root  root  4096 Jul 15 00:37 ../
drwx---r-x  2 user1 user1 4096 Jul 15 23:35 test/
[root@temp-ubuntu-1804-serrver:data]$ su user3
[user3@temp-ubuntu-1804-serrver:data]$ ls -l
total 4
drwx---r-x 2 user1 user1 4096 Jul 15 23:35 test
[user3@temp-ubuntu-1804-serrver:data]$ ls test/
ls: cannot open directory test/: Permission denied
[user3@temp-ubuntu-1804-serrver:data]$ cd test/
bash: cd: test/: Permission denied
[user3@temp-ubuntu-1804-serrver:data]$ cat test/a1.sh
cat: test/a1.sh: Permission denied
[user3@temp-ubuntu-1804-serrver:data]$ 

 

(5)、清理/data/test目录及其下所有文件的acl权限 

[root@temp-ubuntu-1804-serrver:data]$ setfacl -b -R test
[root@temp-ubuntu-1804-serrver:data]$ 

 

【Linux】文件及用户组合权限管理

上一篇:Shell脚本之条件语句


下一篇:windows常用快捷键