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