WORK2

发布时间 2023-12-07 21:23:21作者: 胖虎小夫

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

[root@srehost etc]#ls -d [^[:alpha:]]*
2test
[root@srehost etc]#mkdir 6t789
[root@srehost etc]#mkdir 678f9
[root@srehost etc]#touch 37890098t.txt
[root@srehost etc]#touch 37890098C
[root@srehost etc]#ls -d [^[:alpha:]]*[[:alpha:]]*
2test 37890098C 37890098t.txt 678f9 6t789

 

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

[root@srehost etc]#ls -d p*[^[:digit:]]
pam.d papersize passwd passwd- pbm2ppa.conf pinforc pipewire pki plymouth pm pnm2ppa.conf popt.d postfix prelink.conf.d printcap profile profile.d protocols pulse
[root@srehost etc]#cp -a p*[^[:digit:]] /tmp/mytest1/
[root@srehost etc]#ll /tmp/mytest1/
total 56
drwxr-xr-x. 2 root root 4096 Dec 4 11:31 pam.d
-rw-r--r--. 1 root root 68 May 14 2019 papersize
-rw-r--r--. 1 root root 2655 Dec 4 11:31 passwd
-rw-r--r--. 1 root root 2605 Dec 3 19:40 passwd-
-rw-r--r--. 1 root root 1362 May 14 2019 pbm2ppa.conf
-rw-r--r--. 1 root root 2872 May 14 2019 pinforc
drwxr-xr-x. 2 root root 27 Dec 3 19:07 pipewire
drwxr-xr-x. 10 root root 123 Dec 3 19:10 pki
drwxr-xr-x. 2 root root 28 Dec 3 19:07 plymouth
drwxr-xr-x. 5 root root 52 Dec 3 19:06 pm
-rw-r--r--. 1 root root 6300 May 14 2019 pnm2ppa.conf
drwxr-xr-x. 2 root root 6 Jan 19 2021 popt.d
drwxr-xr-x. 4 root root 4096 Dec 4 11:31 postfix
drwxr-xr-x. 2 root root 24 Dec 3 19:07 prelink.conf.d
-rw-r--r--. 1 root root 233 Sep 10 2018 printcap
-rw-r--r--. 1 root root 2123 May 15 2020 profile
drwxr-xr-x. 2 root root 4096 Dec 3 19:13 profile.d
-rw-r--r--. 1 root root 6568 Sep 10 2018 protocols
drwxr-xr-x. 2 root root 79 Dec 3 19:07 pulse

 

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

[root@srehost etc]#cat /etc/issue
\S
Kernel \r on an \m

[root@srehost etc]#cat /etc/issue| tr 'a-z' 'A-Z' > /tmp/issue.out
[root@srehost etc]#cat /tmp/issue.out
\S
KERNEL \R ON AN \M

 


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

[root@srehost ~]#groupadd -g 2019 distro

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

[root@srehost ~]#useradd -u 1005 -g distro mandriva

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

[root@srehost ~]#mkdir -p /home/linux
[root@srehost ~]#
[root@srehost ~]#
[root@srehost ~]#useradd -u 1100 -d /home/linux/ mageia
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@srehost ~]#su - mageia
[mageia@srehost ~]$ pwd
/home/linux/


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

[root@srehost ~]#echo 'magedu'| passwd -x 7 --stdin mageia
Adjusting aging data for user mageia.
passwd: Success


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

[root@srehost ~]#userdel mandriva
[root@srehost ~]#ls -l /home/
total 4
drwx------. 4 jin jin 4096 Dec 6 12:00 jin
drwxr-xr-x. 2 root root 6 Dec 7 20:43 linux
drwx------. 3 1005 distro 92 Dec 7 20:43 mandriva
drwxr-xr-x. 2 root root 101 Dec 5 22:40 software


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

[root@srehost ~]#groupadd peguin
[root@srehost ~]#useradd -u 2002 -g distro -G peguin slackware

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

[root@srehost ~]#usermod -s /bin/tcsh slackware

 


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

[root@srehost ~]#usermod -s /sbin/nologin -G peguin,admins slackware


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

[root@srehost ~]#useradd user1
[root@srehost ~]#echo "123456"| passwd --stdin user1
Changing password for user user1.
passwd: all authentication tokens updated successfully.
[root@srehost ~]#useradd user2
[root@srehost ~]#echo "123456"| passwd --stdin user2
Changing password for user user2.
passwd: all authentication tokens updated successfully.
[root@srehost ~]#useradd user3
[root@srehost ~]#echo "123456"| passwd --stdin user3
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@srehost ~]#mkdir -p /data/test

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

[root@srehost ~]#chown -R user1:user1 /data/test/
[root@srehost ~]#ll /data/
total 0
drwxr-xr-x. 2 user1 user1 6 Dec 7 20:56 test

 

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

[root@srehost data]#su user1
[user1@srehost data]$ cd test/
[user1@srehost test]$ ll
total 0
[user1@srehost test]$ touch 11.txt
[user1@srehost test]$ touch 22.txt
[user1@srehost test]$ exit
exit
[root@srehost data]#cd test/
[root@srehost test]#ll
\total 0
-rw-rw-r--. 1 user1 user1 0 Dec 7 21:06 11.txt
-rw-rw-r--. 1 user1 user1 0 Dec 7 21:06 22.txt
[root@srehost test]#setfacl -m u:user2:rw 11.txt
[root@srehost test]#getfacl 11.txt
# file: 11.txt
# owner: user1
# group: user1
user::rw-
user:user2:rw-
group::rw-
mask::rw-
other::r--


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

[root@srehost test]#touch a{1..4}.sh
[root@srehost test]#ll
total 0
-rw-rw-r--+ 1 user1 user1 0 Dec 7 21:06 11.txt
-rw-rw-r--. 1 user1 user1 0 Dec 7 21:06 22.txt
-rw-r--r--. 1 root root 0 Dec 7 21:07 a1.sh
-rw-r--r--. 1 root root 0 Dec 7 21:07 a2.sh
-rw-r--r--. 1 root root 0 Dec 7 21:07 a3.sh
-rw-r--r--. 1 root root 0 Dec 7 21:07 a4.sh
[root@srehost test]#
[root@srehost test]#
[root@srehost test]#
[root@srehost test]#
[root@srehost test]#
[root@srehost test]#cd ..
[root@srehost data]#ll
total 0
drwxr-xr-x. 2 user1 user1 86 Dec 7 21:07 test
[root@srehost data]#chmod 1755 test/
[root@srehost data]#ll
total 0
drwxr-xr-t. 2 user1 user1 86 Dec 7 21:07 test
[root@srehost data]#cd test/
[root@srehost test]#ll
total 0
-rw-rw-r--+ 1 user1 user1 0 Dec 7 21:06 11.txt
-rw-rw-r--. 1 user1 user1 0 Dec 7 21:06 22.txt
-rw-r--r--. 1 root root 0 Dec 7 21:07 a1.sh
-rw-r--r--. 1 root root 0 Dec 7 21:07 a2.sh
-rw-r--r--. 1 root root 0 Dec 7 21:07 a3.sh
-rw-r--r--. 1 root root 0 Dec 7 21:07 a4.sh
[root@srehost test]#chattr +i a1.sh
[root@srehost test]#chattr +i a2.sh
[root@srehost test]#lsattr
-------------------- ./11.txt
-------------------- ./22.txt
----i--------------- ./a1.sh
----i--------------- ./a2.sh
-------------------- ./a3.sh
-------------------- ./a4.sh
[root@srehost test]#

 

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

[root@srehost test]#usermod -G user1 user3
[root@srehost test]#
[root@srehost test]#
[root@srehost test]#
[root@srehost test]#setfacl -m:user1:- *
setfacl: Option -m: Invalid argument near character 2
[root@srehost test]#setfacl -m u:user1:- *
setfacl: a1.sh: Operation not permitted
setfacl: a2.sh: Operation not permitted
[root@srehost test]#chattr -i a1.sh
[root@srehost test]#chattr -i a2.sh
[root@srehost test]#setfacl -m u:user1:- *
[root@srehost test]#getfacl *
# file: 11.txt
# owner: user1
# group: user1
user::rw-
user:user1:---
user:user2:rw-
group::rw-
mask::rw-
other::r--

# file: 22.txt
# owner: user1
# group: user1
user::rw-
user:user1:---
group::rw-
mask::rw-
other::r--

# file: a1.sh
# owner: root
# group: root
user::rw-
user:user1:---
group::r--
mask::r--
other::r--

# file: a2.sh
# owner: root
# group: root
user::rw-
user:user1:---
group::r--
mask::r--
other::r--

# file: a3.sh
# owner: root
# group: root
user::rw-
user:user1:---
group::r--
mask::r--
other::r--

# file: a4.sh
# owner: root
# group: root
user::rw-
user:user1:---
group::r--
mask::r--
other::r--

[root@srehost test]#


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

root@srehost test]#cd ..
[root@srehost data]#setfacl -R -b test/
[root@srehost data]#pwd
/data
[root@srehost data]#cd test/
[root@srehost test]#getfacl *
# file: 11.txt
# owner: user1
# group: user1
user::rw-
group::rw-
other::r--

# file: 22.txt
# owner: user1
# group: user1
user::rw-
group::rw-
other::r--

# file: a1.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--

# file: a2.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--

# file: a3.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--

# file: a4.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--

[root@srehost test]#