第二周作业

2.第二周作业

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

  • 创建组distro,其GID为2019;
[05:57:06 root@centos8 ~]#groupadd -g 2019 distro
  • 创建用户mandriva, 其ID号为1005;基本组为distro;
[05:59:04 root@centos8 ~]#useradd -u 1005 -g distro mandriva
[05:59:44 root@centos8 ~]#id 1005
uid=1005(mandriva) gid=2019(distro) 组=2019(distro)
  • 创建用户mageia,其ID号为1100,家目录为/home/linux;
[06:02:14 root@centos8 ~]#useradd -u 1100 -d /home/linux mageia
  • 给用户mageia添加密码,密码为mageedu,并设置用户密码7天后过期
[06:05:17 root@centos8 ~]#echo "mageedu" | passwd --stdin -x 7 mageia
调整用户密码老化数据mageia。
passwd: 操作成功
  • 删除mandriva,但保留其家目录;
[06:09:09 root@centos8 ~]#userdel mandriva
[06:09:27 root@centos8 ~]#ls /home/
linux  mandriva  munki_repo  xiaobai  xiaohong  yuhao
  • 创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[06:12:52 root@centos8 ~]#groupadd peguin;useradd -u 2002 -g distro -G peguin slackware
[06:15:56 root@centos8 ~]#id 2002
uid=2002(slackware) gid=2019(distro) 组=2019(distro),2020(peguin)
  • 修改slackware的默认shell为/bin/tcsh;
[06:18:34 root@centos8 ~]#chsh -s /bin/tcsh slackware
正在更改 slackware 的 shell。
shell 已更改。
  • 为用户slackware新增附加组admins,并设置不可登陆。
[07:00:41 root@centos8 ~]#usermod -aG admins -s /bin/nologin slackware

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

  • 目录/data/test属主、属组为user1
[09:53:36 root@centos8 ~]#chown user1:user1 /data/test/
[09:54:40 root@centos8 ~]#ll -d /data/test/
drwxr-xr-x 2 user1 user1 6 4月  12 09:53 /data/test/
  • 在目录属主、属组不变的情况下,user2对文件有读写权限
[09:59:57 root@centos8 test]#pwd
/data/test
[09:59:44 root@centos8 test]#chmod o=rw *

[10:05:48 root@centos8 test]#setfacl -m u:user2:rw * 
  • user1在/data/test目录下创建文件a1.sh, a2.sh, a3.sh, a4.sh,设置所有用户都不可删除1.sh,2.sh文件、除了user1及root之外,所有用户都不可删除a3.sh, a4.sh
[10:10:35 root@centos8 test]#touch a{1..4}.sh
[10:11:26 root@centos8 test]#chattr +i a1.sh a2.sh
[10:15:30 root@centos8 test]#rm -f a1.sh a2.sh
rm: 无法删除'a1.sh': 不允许的操作
rm: 无法删除'a2.sh': 不允许的操作

[10:18:22 root@centos8 test]#chmod 700 /data/test/a{3..4}*
  • user3增加附加组user1,同时要求user1不能访问/data/test目录及其下所有文件
[10:25:02 root@centos8 test]#usermod -aG user1 user3
[10:25:33 root@centos8 test]#id user3
uid=2005(user3) gid=2005(user3) 组=2005(user3),2003(user1)

[10:33:57 root@centos8 test]#chmod 007 /data/test
  • 清理/data/test目录及其下所有文件的acl权限
[11:24:29 root@centos8 ~]#setfacl -bR /data/test/

3.统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

[04:11:16 root@centos8 ~]#grep -v "/sbin/nologin$" /etc/passwd | cut -d: -f1 | cat -n
     1	root
     2	sync
     3	shutdown
     4	halt
     5	munki_repo
     6	mysql
     7	yuhao
     8	xiaohong
     9	xiaobai
    10	mageia
    11	slackware
    12	user1
    13	user2
    14	user3
# -v 显示非正则匹配到的行, cut -d: -f1 (列出用户名) cat -n (对显示出的每一行进行编号)


[04:11:05 root@centos8 ~]#grep -v "/sbin/nologin$" /etc/passwd | wc -l
14
# wc -l 只计数行数

[04:37:51 root@centos8 ~]#grep -v "/sbin/nologin$" /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
munki_repo
mysql
yuhao
xiaohong
xiaobai
mageia
slackware
user1
user2
user3

4.查出用户UID最大值的用户名、UID及shell类型

[04:23:03 root@centos8 ~]#getent passwd | sort -t: -k3 -n | tail -1 | cut -d: -f1,3,7
nobody:65534:/sbin/nologin

# /etc/passwd 里记录着所有系统里的账户信息,
#通过'sort'排序,'-t':(以:分列) '-k3'(通过-k3选取第3列的UID信息) '-n'(按数字大小排序)。 UID最大的用户会被'sort'到最后一行,
#再通过'tail -1'把UID最大的一行输出。
#最后通过'cut'把用户名,UID,shell类型输出。(-d:  以:分列 , -f1,3,7  选区第1,3,7列)

5.统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[09:56:29 root@centos8 ~]#ss -nt|tail -n 2|tr -s ' ' :|cut -d: -f6|sort|uniq -c|sort -n
      1 100.100.30.26
      1 111.225.146.216

6.编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值

[04:57:32 root@centos8 ~]#cat > disk.sh <<-EOF
> #!/bin/bash
> df | tr -s " " % | cut -d% -f5 | egrep '[0-9]{1,3}' | sort -n | tail -1
> EOF
[04:57:55 root@centos8 ~]#bash disk.sh
12

[04:56:30 root@centos8 ~]#df
文件系统          1K-块    已用     可用 已用% 挂载点
devtmpfs         912940       0   912940    0% /dev
tmpfs            930232       0   930232    0% /dev/shm
tmpfs            930232     456   929776    1% /run
tmpfs            930232       0   930232    0% /sys/fs/cgroup
/dev/vda3      43925484 4951268 38974216   12% /
/dev/vda2        102182    7386    94796    8% /boot/efi
tmpfs            186044       0   186044    0% /run/user/0

7.编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

[09:56:08 root@centos8 ~]#bash systeminfo.sh
centos8
172.18.191.8
NAME="CentOS Linux"
VERSION="8"
4.18.0-348.2.1.el8_5.x86_64
型号:           79
型号名称:       Intel(R) Xeon(R) CPU E5-2682 v4 @ 2.50GHz
1.8Gi
42G
[09:56:14 root@centos8 ~]#cat systeminfo.sh
#!/bin/bash
hostname
ifconfig eth0 | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,33}" | head -1
cat /etc/os-release | head -2
uname -r
lscpu | egrep "型号"
free -h | tr -s " " | cut -d" " -f 2 | head -2 | tail -1
lsblk | tr -s " " | cut -d" " -f4 | head -2 | tail -1
[09:56:29 root@centos8 ~]#
posted @ 2022-05-15 22:03  阴东  阅读(44)  评论(0)    收藏  举报