Linux 用户文件属性等相关练习

Posted superkx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 用户文件属性等相关练习相关的知识,希望对你有一定的参考价值。

一切有为法,如梦幻泡影。

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

[root@centos7.9-68.71 etc]#cd /etc
[root@centos7.9-68.71 etc]#touch 1a.txt _abc.txt 1abcd.txt
[root@centos7.9-68.71 etc]#ll [^a-Z][[:alpha:]]*
-rw-r--r--. 1 root root 0 Jan 1 18:26 1abcd.txt
-rw-r--r--. 1 root root 0 Jan 1 18:26 1a.txt
-rw-r--r--. 1 root root 0 Jan 1 18:26 _abc.txt

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

[root@centos7.9-68.71 ~]#find /etc -name p*[^0-9]  -exec cp -ar  /tmp/mytest1/ \\;

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

[root@centos7.9-68.71 ~]#cat /etc/issue | tr -t "[a_z]" "[A_Z]" > /tmp/issue.out
[root@centos7.9-68.71 ~]#cat /tmp/issue.out
\\S
Kernel \\r on An \\m

4. 请总结描述用户和组管理类命令的使用方法并完成以下练习:

   (1) 创建组 distro,其 GID 为2019:

[root@centos7.9-68.71 ~]#groupadd -g 2019 distro

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

[root@centos7.9-68.71 ~]#useradd -u 1005 -g distro mandriva

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

[root@centos7.9-68.71 ~]#useradd -d /home/linux -u 1100 mageia

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

[root@centos7.9-68.71 ~]#echo mageedu | passwd --stdin mageia; passwd -x 7 mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
Adjusting aging data for user mageia.
passwd: Success

[root@centos7.9-68.71 ~]#getent shadow mageia
mageia:$6$2.gR4/Rd$VmWqjsK64flRxy/3ocEP.0SxbnL0nxV4B9zMpIRwYoO4Y02Ro1KnuX6ucGv4ZOrKQRqEbehdb8txgYjrI5Ibr.:18993:0:7:7:::

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

[root@centos7.9-68.71 ~]#userdel mandriva

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

[root@centos7.9-68.71 ~]#groupadd peguin;useradd -u 2002 -g distro -G peguin slackware

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

[root@centos7.9-68.71 ~]#usermod -s /bin/tcsh slackware

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

[root@centos7.9-68.71 ~]#groupadd admins;usermod -s /sbin/nologin -a -G admins slackware
[root@centos7.9-68.71 ~]#id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro),2020(peguin),2021(admins)


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

[root@centos7.9-68.71 ~]#useradd user1;useradd user2;useradd user3;mkdir /data/test

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

[root@centos7.9-68.71 ~]#chown user1.user1 /data/test
[root@centos7.9-68.71 ~]#ll /data
total 0
drwxr-xr-x. 2 user1 user1 6 Jan 1 16:55 test

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

[root@centos7.9-68.71 ~]#chmod g+w /data/test/*
[root@centos7.9-68.71 ~]#usermod -G user1 user2

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

# 1. 使用 su 命令指定 user1 用户在 /data/test 创建相关文件:
[root@centos7.9-68.71 test]#su user1 -c "touch /data/test/a1..4.sh"
[root@centos7.9-68.71 test]#ll
total 0
-rw-r--r--. 1 user1 user1 0 Jan 1 17:21 a1.sh
-rw-r--r--. 1 user1 user1 0 Jan 1 17:21 a2.sh
-rw-r--r--. 1 user1 user1 0 Jan 1 17:21 a3.sh
-rw-r--r--. 1 user1 user1 0 Jan 1 17:21 a4.sh

# 2. 设置 a1.sh 和 a2.sh 文件,所有用户都不可删除:
[root@centos7.9-68.71 test]#chattr +i a1.sh a2.sh
[root@centos7.9-68.71 test]#rm -f a1.sh
rm: cannot remove ‘a1.sh’: Operation not permitted
[root@centos7.9-68.71 test]#lsattr
----i----------- ./a2.sh
---------------- ./a3.sh
---------------- ./a4.sh
----i----------- ./a1.sh

# 3. 默认除user1及root之后,其他用户都不可删除a3.sh,a4.sh

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

[root@centos7.9-68.71 ~]#usermod -G user1 user3
[root@centos7.9-68.71 ~]#setfacl -m u:user1:0 /data/test/
[root@centos7.9-68.71 ~]#chown root.root -R /data/test

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

[root@centos7.9-68.71 test]#setfacl -R -b /data/test/*






以上是关于Linux 用户文件属性等相关练习的主要内容,如果未能解决你的问题,请参考以下文章

Linux用户和组的配置文件

用户和组——Linux基本命令

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

linux基本练习:用户和组管理的相关练习

linux命令练习:磁盘管理相关练习

linux命令练习:磁盘管理相关练习