练习
1、显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录
[root@centos8 etc]# touch {1..3}.txt
[root@centos8 etc]# mkdir {1..3}dir
[root@centos8 ~]# ls -d /etc/[^[:alpha:]]*
/etc/1.txt /etc/1aa /etc/1dir /etc/2.txt /etc/2dir /etc/3.txt /etc/3dir
2、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。
[root@centos8 ~]# mkdir /tmp/mytest1
[root@centos8 mytest1]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1
[root@centos8 mytest1]# ll
total 52
drwxr-xr-x 2 root root 4096 Jan 9 11:08 pam.d
-rw-r--r-- 1 root root 68 Jan 9 11:08 papersize
-rw-r--r-- 1 root root 2896 Jan 9 11:08 passwd
-rw-r--r-- 1 root root 2843 Jan 9 11:08 passwd-
-rw-r--r-- 1 root root 1362 Jan 9 11:08 pbm2ppa.conf
-rw-r--r-- 1 root root 2872 Jan 9 11:08 pinforc
drwxr-xr-x 2 root root 27 Jan 9 11:08 pipewire
drwxr-xr-x 10 root root 123 Jan 9 11:08 pki
drwxr-xr-x 2 root root 28 Jan 9 11:08 plymouth
drwxr-xr-x 5 root root 52 Jan 9 11:08 pm
-rw-r--r-- 1 root root 6300 Jan 9 11:08 pnm2ppa.conf
drwxr-xr-x 2 root root 6 Jan 9 11:08 popt.d
drwxr-xr-x 4 root root 288 Jan 9 11:08 postfix
drwxr-xr-x 2 root root 24 Jan 9 11:08 prelink.conf.d
-rw-r--r-- 1 root root 233 Jan 9 11:08 printcap
-rw-r--r-- 1 root root 2123 Jan 9 11:08 profile
drwxr-xr-x 2 root root 4096 Jan 9 11:08 profile.d
-rw-r--r-- 1 root root 6568 Jan 9 11:08 protocols
drwxr-xr-x 2 root root 79 Jan 9 11:08 pulse
3、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@centos8 ~]# cat /etc/issue | tr [:lower:] [:upper:] > /tmp/issue.out
[root@centos8 ~]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M
4、请总结描述用户和组管理类命令的使用方法并完成以下练习:
# 创建用户命令
useradd [options] username
# 修改用户命令
usermod [options] username
# 删除用户命令
userdel [options] username
# 创建组命令
groupadd [options] groupname
# 修改组命令
groupmod [options] groupname
# 删除组命令
groupdel [options] groupname
(1)、创建组distro,其GID为2019;
[root@centos8 ~]# groupadd -g 2019 -r distro
[root@centos8 ~]# cat /etc/group | grep distro
distro:x:2019:
[root@centos8 ~]#
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@centos8 ~]# useradd -u 1005 -g distro mandriva
[root@centos8 ~]# id mandriva
uid=1005(mandriva) gid=2019(distro) groups=2019(distro)
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@centos8 ~]# useradd -u 1100 -d /home/linux mageia
[root@centos8 ~]# cat /etc/passwd | grep mageia
mageia:x:1100:1100::/home/linux:/bin/bash
(4)、给用户mageia添加密码,密码为mageedu,并设置用户密码7天后过期
[root@centos8 ~]# echo "mageedu" | passwd --stdin mageia | passwd -x 7
Adjusting aging data for user mageia.
passwd: Success
[root@centos8 ~]# cat /etc/shadow | grep mageia
mageia:$6$kmtDFO8LIssYiG6p$XaW2rL437Z38KjOhhqBNvRlojTZ6RpWUzml9wq.JfV/.wFuKh04w0WKW8sAVK.3FNQlamulGYuPt1kP7k3x0g1:19001:0:7:7:::
(5)、删除mandriva,但保留其家目录;
[root@centos8 ~]# userdel mandriva
[root@centos8 ~]# ll /home/
total 4
drwx------. 3 chenyinj chenyinj 99 Dec 24 21:18 chenyinj
drwx------ 6 mageia mageia 121 Jan 9 12:22 linux
drwx------ 3 1005 distro 78 Jan 9 11:36 mandriva
drwx------. 15 sakura sakura 4096 Jan 6 21:26 sakura
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@centos8 ~]# groupadd peguin
[root@centos8 ~]# useradd -u 2002 -g distro -G peguin slackware
[root@centos8 ~]# id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro)
(7)、修改slackware的默认shell为/bin/tcsh;
[root@centos8 ~]# usermod -s /bin/tcsh slackware
[root@centos8 ~]# cat /etc/passwd | grep slackware
slackware:x:2002:2019::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins,并设置不可登陆。
[root@centos8 ~]# usermod -G admins -s /sbin/nologin slackware
[root@centos8 ~]# id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro),2021(admins)
[root@centos8 ~]# cat /etc/passwd | grep slackware
slackware:x:2002:2019::/home/slackware:/sbin/nologin
5、创建用户user1、user2、user3。在/data/下创建目录test
[root@centos8 ~]# useradd user1;useradd user2;useradd user3
[root@centos8 ~]# mkdir /data/test
(1)、目录/data/test属主、属组为user1
[root@centos8 ~]# chown user1 /data/test ; chgrp user1 /data/test
[root@centos8 ~]# ll -d /data/test
drwxr-xr-x 2 user1 user1 6 Jan 9 12:50 /data/test
(2)、在目录属主、属组不变的情况下,user2对文件有读写权限
[root@centos8 ~]# ll -d /data/test
drwxr-xrw- 2 user1 user1 6 Jan 9 12:50 /data/test
(3)、user1在/data/test目录下创建文件a1.sh, a2.sh, a3.sh, a4.sh,设置所有用户都不可删除a1.sh,a2.sh文件、除了user1及root之外,所有用户都不可删除a3.sh, a4.sh
[user1@centos8 test]$ touch a{1..4}.sh
[user1@centos8 test]$ ll
total 0
-rw-rw-r-- 1 user1 user1 0 Jan 9 12:59 a1.sh
-rw-rw-r-- 1 user1 user1 0 Jan 9 12:59 a2.sh
-rw-rw-r-- 1 user1 user1 0 Jan 9 12:59 a3.sh
-rw-rw-r-- 1 user1 user1 0 Jan 9 12:59 a4.sh
[root@centos8 ~]# chattr +i /data/test/a{1,2}.sh
[root@centos8 ~]# rm -f /data/test/a{1,2}.sh
rm: cannot remove '/data/test/a1.sh': Operation not permitted
rm: cannot remove '/data/test/a2.sh': Operation not permitted
[root@centos8 ~]# chmod o+t /data/test/a{3,4}.sh
[root@centos8 ~]# ll /data/test/a{3,4}.sh
-rw-rw-r-T 1 user1 user1 0 Jan 9 12:59 /data/test/a3.sh
-rw-rw-r-T 1 user1 user1 0 Jan 9 12:59 /data/test/a4.sh
(4)、user3增加附加组user1,同时要求user1不能访问/data/test目录及其下所有文件
[root@centos8 ~]# chmod -R u-x /data/test/
chmod: changing permissions of '/data/test/a1.sh': Operation not permitted
chmod: changing permissions of '/data/test/a2.sh': Operation not permitted
[user1@centos8 ~]$ cd /data/test
-bash: cd: /data/test: Permission denied
[user1@centos8 ~]$ cat /data/test/a1.sh
cat: /data/test/a1.sh: Permission denied
(5)、清理/data/test目录及其下所有文件的acl权限
[root@centos8 ~]# setfacl -R -b /data/test