1、显示/etc目录下,以非字母开头,后面跟了一个字母以及其他任意长度字符的文件或目录。
[root@Centos7 etc]# ls /etc/ | grep ^[^[:alpha:]]
1a.txt
1dir
_a.log
2、复制/etc目录下所有以p开头,以非数字结尾的文件或目录,到/tmp/mytest1目录下。
[root@Centos7 etc]# cp -ar /etc/p*[^0-9] /tmp/mytest1
[root@Centos7 mytest1]# ls /tmp/mytest1/
pam.d passwd- plymouth popt.d ppp printcap profile.d python
passwd pki pm postfix prelink.conf.d profile protocols
3、将/etc/issue文件中的内容转化为大写后保存至/tmp/issue.out文件中。
[root@Centos7 tmp]# cat /etc/issue | tr 'a-z' 'A-Z' >/tmp/issue.out
[root@Centos7 tmp]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M
4、请总结描述用户和组管理类命令的使用方法并完成以下练习:
用户管理命令说明:
新建用户命令:useradd
用户属性修改:usermod
用户删除:userdel
修改用户密码策略:chage
组管理命令说明:
创建组:groupadd
组属性修改:groupmod
组删除:groupdel
(1) 创建组distro,其GID为2019;
[root@Centos7 tmp]# groupadd distro -g 2019
[root@Centos7 tmp]# getent group | grep 'distro*'
distro:x:2019:
(2) 创建用户mandriva,其ID号为1005,基本组为distro;
[root@Centos7 tmp]# useradd mandriva -u 1005 -g distro
[root@Centos7 tmp]# id mandriva
uid=1005(mandriva) gid=2019(distro) groups=2019(distro)
(3) 创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@Centos7 tmp]# useradd mageia -u 1100 -d /home/linux
[root@Centos7 tmp]# getent passwd | grep 'mageia'
mageia:x:1100:1100::/home/linux:/bin/bash
(4) 给用户mageia添加密码,密码为mageedu,并设置用户密码7天后过期;
[root@Centos7 tmp]# echo mageedu | passwd --stdin mageia
[root@Centos7 ~]# chage -M 7 mageia
(5) 删除mandriva,但保留其家目录;
[root@Centos7 ~]# userdel mandriva
[root@Centos7 ~]# ll /home
total 0
drwx------. 2 mageia mageia 62 Dec 12 12:54 linux
drwx------. 2 1005 distro 62 Dec 12 12:36 mandriva
drwx------. 2 wu wu 83 Nov 25 20:29 wu
(6) 创建用户slackware,其ID号为2002,基本组为distro,附属组peguin;
[root@Centos7 ~]# useradd slackware -u 2002 -g distro -G peguin
[root@Centos7 ~]# id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro),2020(peguin)
(7) 修改slackware的默认shell为/bin/tcsh;
[root@Centos7 ~]# getent passwd slackware
slackware:x:2002:2019::/home/slackware:/bin/bash
[root@Centos7 ~]# usermod -s /bin/tcsh slackware
[root@Centos7 ~]# getent passwd slackware
slackware:x:2002:2019::/home/slackware:/bin/tcsh
(8) 为用户slackware新增附属组admins,并设置不可登录;
[root@Centos7 ~]# usermod -G admins -s /sbin/nologin slackware
[root@Centos7 ~]# getent passwd slackware
slackware:x:2002:2019::/home/slackware:/sbin/nologin
[root@Centos7 ~]# id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro),2021(admins)
5、创建用户user1、user2、user3 。在/data/下创建目录test
(1) 目录/data/test属主、属组为user1;
[root@Centos7 data]# ll
total 8
drwxr-xr-x. 2 root root 62 Dec 8 16:25 binlog
-rw-r--r--. 1 root root 752 Dec 10 10:11 fstab
drwxr-xr-x. 3 root root 4096 Dec 8 15:12 mysql
drwxr-xr-x. 2 root root 6 Dec 12 13:55 test
[root@Centos7 data]# chown user1:user1 test
[root@Centos7 data]# ll
total 8
drwxr-xr-x. 2 root root 62 Dec 8 16:25 binlog
-rw-r--r--. 1 root root 752 Dec 10 10:11 fstab
drwxr-xr-x. 3 root root 4096 Dec 8 15:12 mysql
drwxr-xr-x. 2 user1 user1 6 Dec 12 13:55 test
(2) 在目录属主、属组不变的情况下,user2对文件有读写权限;
[root@Centos7 data]# getfacl test
# file: test
# owner: user1
# group: user1
user::rwx
group::r-x
other::r-x
[root@Centos7 data]# setfacl -m u:user2:rw test
[root@Centos7 data]# getfacl test
# file: test
# owner: user1
# group: user1
user::rwx
user:user2:rw-
group::r-x
mask::rwx
other::r-x
(3) user1在/data/test目录下创建文件a1.sh,a2.sh,a3.sh,a4.sh,设置所有用户都不可删除a1.sh,a2.sh文件。除了user1和root之外,其他用户都不可删除a3.sh,a4.sh文件;
[user1@Centos7 test]$ touch a{1..4}.sh
[user1@Centos7 test]$ ll
total 0
-rw-rw-r--. 1 user1 user1 0 Dec 12 14:55 a1.sh
-rw-rw-r--. 1 user1 user1 0 Dec 12 14:55 a2.sh
-rw-rw-r--. 1 user1 user1 0 Dec 12 14:55 a3.sh
-rw-rw-r--. 1 user1 user1 0 Dec 12 14:55 a4.sh
[user1@Centos7 test]$ chattr +i a1.sh a2.sh
chattr: Operation not permitted while setting flags on a1.sh
chattr: Operation not permitted while setting flags on a2.sh
[user1@Centos7 test]$ exit
logout
[root@Centos7 test]# chattr +i a1.sh a2.sh
[root@Centos7 test]# lsattr
---------------- ./1.txt
---------------- ./b.log
----i----------- ./a1.sh
----i----------- ./a2.sh
---------------- ./a3.sh
---------------- ./a4.sh
[root@Centos7 test]# rm -f a1.sh
rm: cannot remove ‘a1.sh’: Operation not permitted
#使用特殊权限sticky位,在目录设置Sticky位,只有文件的所有者或root可以删除该文件
[root@Centos7 test]# chmod o+t /data/test
[root@Centos7 test]# su - user3
Last login: Sat Dec 12 14:06:09 CST 2020 on pts/0
[user3@Centos7 ~]$ cd /data/test
[user3@Centos7 test]$ rm -f a3.sh
rm: cannot remove ‘a3.sh’: Permission denied
[user3@Centos7 test]$ exit
logout
[root@Centos7 test]# su - user1
Last login: Sat Dec 12 14:54:59 CST 2020 on pts/0
[user1@Centos7 ~]$ rm -f /data/test/a3.sh
[user1@Centos7 ~]$ ll /data/test/
total 0
-rw-rw-r--. 1 user1 user1 0 Dec 12 14:55 a1.sh
-rw-rw-r--. 1 user1 user1 0 Dec 12 14:55 a2.sh
-rw-rw-r--. 1 user1 user1 0 Dec 12 14:55 a4.sh
(4) user3增加附属组user1,同时要求user3不能访问/data/test目录及其下所有文件;
[root@Centos7 test]# usermod -G user1 user3
[root@Centos7 test]# id user3
uid=2005(user3) gid=2005(user3) groups=2005(user3),2003(user1)
[root@Centos7 ~]# setfacl -m u:user3:- /data/test
[root@Centos7 ~]# su - user3
Last login: Sat Dec 12 16:20:37 CST 2020 on pts/0
[user3@Centos7 ~]$ cd /data/test
-bash: cd: /data/test: Permission denied
(5) 清理/data/test目录下及其下所有文件的acl权限
[user3@Centos7 ~]$ getfacl /data/test
getfacl: Removing leading '/' from absolute path names
# file: data/test
# owner: user1
# group: user1
# flags: --t
user::rwx
user:user1:---
user:user2:rw-
user:user3:---
group::r-x
mask::rwx
other::r-x
[root@Centos7 ~]# setfacl -b /data/test
[root@Centos7 ~]# getfacl /data/test
getfacl: Removing leading '/' from absolute path names
# file: data/test
# owner: user1
# group: user1
# flags: --t
user::rwx
group::r-x
other::r-x