Linux+Python 高端运维班第二次作业
1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
- [ ~]# who |cut -d" " -f1|uniq
- (unknown)
- root
- test1
- [ ~]# who |cut -d" " -f1|sort -u
- root
- (unknown)
- wangyanglin
2、取出当前系统上被用户当作其默认 shell 的最多的那个 shell。
- [ ~]# cut -d: -f7 /etc/passwd|uniq -c|sort -n|tail -1
- 34 /sbin/nologin
3、将 / etc/passwd 中的第三个字段数值最大的后 10 个用户的信息全部改为大写后保存至 / tmp/maxusers.txt 文件中。
- [ ~]# sort -t: -k3 -n /etc/passwd|tail -n 10|tr 'a-z' 'A-Z' >/tmp/maxusers.txt
- [ ~]# cat /tmp/maxusers.txt
- COLORD:X:995:994:USER FOR COLORD:/VAR/LIB/COLORD:/SBIN/NOLOGIN
- UNBOUND:X:996:995:UNBOUND DNS RESOLVER:/ETC/UNBOUND:/SBIN/NOLOGIN
- POLKITD:X:997:996:USER FOR POLKITD:/:/SBIN/NOLOGIN
- SYSTEMD-NETWORK:X:998:997:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN
- SYSTEMD-BUS-PROXY:X:999:998:SYSTEMD BUS PROXY:/:/SBIN/NOLOGIN
- WANGYANGLIN:X:1000:1000:WANGYANGLIN:/HOME/WANGYANGLIN:/BIN/BASH
- GENTOO:X:1001:1001:GENTOO DISTRIBUTION:/HOME/GENTOO:/BIN/CSH
- USER1:X:1002:1004::/HOME/USER1:/BIN/BASH
- USER2:X:1003:1005::/HOME/USER2:/BIN/BASH
- NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
4、取出当前主机的 IP 地址,提示:对 ifconfig 命令的结果进行切分。
- [ ~]# ifconfig|grep '\'|cut -d" " -f10
- 192.168.233.128
- 127.0.0.1
- 192.168.122.1
5、显示 / var 目录下一级子目录或文件的总个数。
- [ etc]# tree /
- var | tail - n 1 1606 directories,
- 11471 files
6、取出 / etc/group 文件中第三个字段数值最小的 10 个组的名字。
- [ ~]# sort -t: -k3 -n /etc/group|head -n 10|cut -d: -f1
- root
- bin
- daemon
- sys
- adm
- tty
- disk
- lp
- mem
- kmem
7、将 / etc/fstab 和 / etc/issue 文件的内容合并为同一个内容后保存至 / tmp/etc.test 文件中。
- [ ~]# cat /etc/fstab /etc/issue >/tmp/etc.test
- [ ~]# cat /tmp/etc.test
- #
- # /etc/fstab
- # Created by anaconda on Mon Nov 28 04:42:49 2016
- #
- # Accessible filesystems, by reference, are maintained under '/dev/disk'
- # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
- #
- /dev/mapper/centos-root / xfs defaults 0 0
- UUID=011ed854-96ba-4384-a619-c7012204724c /boot xfs defaults 0 0
- /dev/mapper/centos-home /home xfs defaults 0 0
- /dev/mapper/centos-swap swap swap defaults 0 0
- \S
- Kernel \r on an \m
8、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组 distro,其 GID 为 2016;
- [ ~]# groupadd -g 2016 distro
(2)、创建用户 mandriva, 其 ID 号为 1005;基本组为 distro;
- [ ~]# useradd -u 1005 -g distro mandriva
- [ ~]# id mandriva
- uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
(3)、创建用户 mageia,其 ID 号为 1100,家目录为 / home/linux;
- [ ~]# useradd -u 1100 -d /home/linux mageia
- [ ~]# id mageia
- uid=1100(mageia) gid=1100(mageia) groups=1100(mageia)
(4)、给用户 mageia 添加密码,密码为 mageedu;
- [ ~]# passwd mageia
- Changing password for user mageia.
- New password:
- BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
- Retype new password:
- passwd: all authentication tokens updated successfully.
(5)、删除 mandriva,但保留其家目录;
- [ ~]# userdel mandriva
- [ ~]# ls /home
- gentoo linux mandriva user1 user2 wangyanglin
(6)、创建用户 slackware,其 ID 号为 2002,基本组为 distro,附加组 peguin;
- [ ~]# groupadd peguin
- [ ~]# useradd -u 2002 -g distro -G peguin slackware
- [ ~]# id slackware
- uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
(7)、修改 slackware 的默认 shell 为 / bin/tcsh;
- [ ~]# usermod -s /bin/tcsh slackware
(8)、为用户 slackware 新增附加组 admins;
- [ ~]# groupadd admins
- [ ~]# usermod -G admins slackware
(9)、为 slackware 添加密码,且要求密码最短使用期限为 3 天,最长为 180 天,警告为 3 天;
- [ ~]# passwd slackware
- Changing password for user slackware.
- New password:
- BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
- Retype new password:
- passwd: all authentication tokens updated successfully.
- [ ~]# passwd -n 3 -x 180 -w 3 slackware
- Adjusting aging data for user slackware.
- passwd: Success
(10)、添加用户 openstack,其 ID 号为 3003, 基本组为 clouds,附加组为 peguin 和 nova;
- [ ~]# groupadd nova
- [ ~]# groupadd clouds
- [ ~]# useradd -u 3003 -g clouds -G peguin,nova openstack
- [ ~]# id openstack
- uid=3003(openstack) gid=2020(clouds) groups=2020(clouds),2017(peguin),2019(nova)
(11)、添加系统用户 mysql,要求其 shell 为 / sbin/nologin;
- [ ~]# useradd -s /sbin/nologin -r mysql
- [ ~]# id mysql
- uid=988(mysql) gid=983(mysql) groups=983(mysql)
(12)、使用 echo 命令,非交互式为 openstack 添加密码。
- [ ~]# echo "123456"|passwd --stdin openstack
- Changing password for user openstack.
- passwd: all authentication tokens updated successfully.
9、复制 / etc/skel 目录为 / home/tuser1,要求 / home/tuser1 及其内部文件的属组和其它用户均没有任何访问权限。
- [ ~]# cp -r /etc/skel /home
- [ ~]# mv /home/skel /home/tuser1
- [ ~]# chomd g-r,o-r -R /home/tuser1
- [ ~]# ll -a /home/tuser1
- total 16
- drwx--x--x. 3 root root 74 Dec 18 21:55 .
- drwxr-xr-x. 11 root root 4096 Dec 18 21:55 ..
- -rw-------. 1 root root 18 Dec 18 21:55 .bash_logout
- -rw-------. 1 root root 193 Dec 18 21:55 .bash_profile
- -rw-------. 1 root root 231 Dec 18 21:55 .bashrc
- drwx--x--x. 4 root root 37 Dec 18 21:55 .mozilla
10、显示 / proc/meminfo 文件中以大写或小写 S 开头的行;用两种方式;
第一种:
- [ ~]# grep -i '^s' /proc/meminfo
- SwapCached: 0 kB
- SwapTotal: 2097148 kB
- SwapFree: 2097148 kB
- Shmem: 13580 kB
- Slab: 105364 kB
- SReclaimable: 53096 kB
- SUnreclaim: 52268 kB
第二种
- [ ~]# grep '^[sS]' /proc/meminfo
- SwapCached: 0 kB
- SwapTotal: 2097148 kB
- SwapFree: 2097148 kB
- Shmem: 13580 kB
- Slab: 105348 kB
- SReclaimable: 53096 kB
- SUnreclaim: 52252 kB
11、显示 / etc/passwd 文件中其默认 shell 为非 / sbin/nologin 的用户;
- [ ~]# grep -v '/sbin/nologin$' /etc/passwd
- root:x:0:0:man chfn:/root:/bin/bash
- sync:x:5:0:sync:/sbin:/bin/sync
- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
- halt:x:7:0:halt:/sbin:/sbin/halt
- wangyanglin:x:1000:1000:wangyanglin:/home/wangyanglin:/bin/bash
- gentoo:x:1001:1001:Gentoo Distribution:/home/gentoo:/bin/csh
- user1:x:1002:1004::/home/user1:/bin/bash
- user2:x:1003:1005::/home/user2:/bin/bash
- mageia:x:1100:1100::/home/linux:/bin/bash
- slackware:x:2002:2016::/home/slackware:/bin/tcsh
- openstack:x:3003:2020::/home/openstack:/bin/bash
12、显示 / etc/passwd 文件中其默认 shell 为 / bin/bash 的用户;
- [ ~]# grep '/bin/bash$' /etc/passwd |cut -d: -f1
- root
- wangyanglin
- user1
- user2
- mageia
- openstack
13、找出 / etc/passwd 文件中的一位数或两位数;
- [ ~]# grep -o ':[[:digit:]]\{1,2\}:' /etc/passwd
- :0:
- :1:
- :2:
- :3:
- :4:
- :5:
- :6:
- :7:
- :8:
- :11:
- :12:
- :14:
- :99:
- :81:
- :59:
- :32:
- :75:
- :29:
- :76:
- :42:
- :70:
- :74:
- :89:
- :38:
- :72:
14、显示 / etc/rc.d/rc.sysinit 文件中以 #开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
- [ ~]# grep '^#[[:space:]]\+[^[:space:]]' /etc/rc.d/rc.sysinit
- grep: /etc/rc.d/rc.sysinit: No such file or directory
15、打出 netstat -tan 命令执行结果中以'LISTEN',后或跟空白字符结尾的行;
- [ init.d]# netstat -tan|grep 'LISTEN[[:space:]]\+$'
- tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
- tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
- tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
- tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
- tcp6 0 0 :::22 :::* LISTEN
- tcp6 0 0 ::1:631 :::* LISTEN
- tcp6 0 0 ::1:25 :::* LISTEN
16、添加用户 bash, testbash,basher, nologin (此一个用户的 shell 为 / sbin/nologin),而后找出当前系统上其用户名和默认 shell 相同的用户的信息;
- [ ~]# useradd bash
- [ ~]# useradd testbash
- [ ~]# useradd basher
- [ ~]# useradd -s /sbin/nologin nologin
- [ ~]# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd
- sync:x:5:0:sync:/sbin:/bin/sync
- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
- halt:x:7:0:halt:/sbin:/sbin/halt
- bash:x:3004:3004::/home/bash:/bin/bash
- nologin:x:3007:3007::/home/nologin:/sbin/nologin
来源: