Linux+Python高端运维班第二次作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux+Python高端运维班第二次作业相关的知识,希望对你有一定的参考价值。
Linux+Python高端运维班第二次作业
1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[[email protected] ~]# who |cut -d" " -f1|uniq (unknown) root test1 [[email protected] ~]# who |cut -d" " -f1|sort -u root (unknown) wangyanglin
2、取出当前系统上被用户当作其默认shell的最多的那个shell。
[[email protected] ~]# cut -d: -f7 /etc/passwd|uniq -c|sort -n|tail -1 34 /sbin/nologin
3、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[[email protected] ~]# sort -t: -k3 -n /etc/passwd|tail -n 10|tr ‘a-z‘ ‘A-Z‘ >/tmp/maxusers.txt [[email protected] ~]# 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命令的结果进行切分。
[[email protected] ~]# ifconfig|grep ‘\<inet\>‘|cut -d" " -f10 192.168.233.128 127.0.0.1 192.168.122.1
5、显示/var目录下一级子目录或文件的总个数。
[[email protected] etc]# tree /var|tail -n 1 1606 directories, 11471 files
6、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[[email protected] ~]# 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文件中。
[[email protected] ~]# cat /etc/fstab /etc/issue >/tmp/etc.test [[email protected] ~]# 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;
[[email protected] ~]# groupadd -g 2016 distro
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[[email protected] ~]# useradd -u 1005 -g distro mandriva [[email protected] ~]# id mandriva uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[[email protected] ~]# useradd -u 1100 -d /home/linux mageia [[email protected] ~]# id mageia uid=1100(mageia) gid=1100(mageia) groups=1100(mageia)
(4)、给用户mageia添加密码,密码为mageedu;
[[email protected] ~]# 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,但保留其家目录;
[[email protected] ~]# userdel mandriva [[email protected] ~]# ls /home gentoo linux mandriva user1 user2 wangyanglin
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[[email protected] ~]# groupadd peguin [[email protected] ~]# useradd -u 2002 -g distro -G peguin slackware [[email protected] ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
(7)、修改slackware的默认shell为/bin/tcsh;
[[email protected] ~]# usermod -s /bin/tcsh slackware
(8)、为用户slackware新增附加组admins;
[[email protected] ~]# groupadd admins [[email protected] ~]# usermod -G admins slackware
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[[email protected] ~]# 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. [[email protected] ~]# passwd -n 3 -x 180 -w 3 slackware Adjusting aging data for user slackware. passwd: Success
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[[email protected] ~]# groupadd nova [[email protected] ~]# groupadd clouds [[email protected] ~]# useradd -u 3003 -g clouds -G peguin,nova openstack [[email protected] ~]# id openstack uid=3003(openstack) gid=2020(clouds) groups=2020(clouds),2017(peguin),2019(nova)
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[[email protected] ~]# useradd -s /sbin/nologin -r mysql [[email protected] ~]# id mysql uid=988(mysql) gid=983(mysql) groups=983(mysql)
(12)、使用echo命令,非交互式为openstack添加密码。
[[email protected] ~]# echo "123456"|passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully.
9、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
[[email protected] ~]# cp -r /etc/skel /home [[email protected] ~]# mv /home/skel /home/tuser1 [[email protected] ~]# chomd g-r,o-r -R /home/tuser1 [[email protected] ~]# 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开头的行;用两种方式;
第一种:
[[email protected] ~]# 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
第二种
[[email protected] ~]# 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的用户;
[[email protected] ~]# 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的用户;
[[email protected] ~]# grep ‘/bin/bash$‘ /etc/passwd |cut -d: -f1 root wangyanglin user1 user2 mageia openstack
13、找出/etc/passwd文件中的一位数或两位数;
[[email protected] ~]# 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文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
[[email protected] ~]# grep ‘^#[[:space:]]\+[^[:space:]]‘ /etc/rc.d/rc.sysinit grep: /etc/rc.d/rc.sysinit: No such file or directory
15、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
[[email protected] 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相同的用户的信息;
[[email protected] ~]# useradd bash [[email protected] ~]# useradd testbash [[email protected] ~]# useradd basher [[email protected] ~]# useradd -s /sbin/nologin nologin [[email protected] ~]# 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
本文出自 “学海无涯” 博客,谢绝转载!
以上是关于Linux+Python高端运维班第二次作业的主要内容,如果未能解决你的问题,请参考以下文章