Linux+Python运维培训班第4期--马哥——第2次作业(20161225)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux+Python运维培训班第4期--马哥——第2次作业(20161225)相关的知识,希望对你有一定的参考价值。
1、列出当前系统上所有已经登录的用户的用户名(注:同一个用户登录多次则只显示一次)。
[[email protected]~]# who ; echo -e ‘\n‘ ; who | grep -o ‘^\<[[:alpha:]]\+\>‘ | sort -u root pts/0 2016-12-23 22:00 (192.168.20.1) root pts/1 2016-12-23 22:00 (192.168.20.1) root pts/2 2016-12-23 22:00 (192.168.20.1) bash pts/3 2016-12-23 22:02 (192.168.20.1) bash pts/4 2016-12-23 22:02 (192.168.20.1) bash root
2、取出当前系统上被用户当作其默认shell的最多的那个shell。
[[email protected]~]# cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n -r | head -1 29 /sbin/nologin
3、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[[email protected]~]# sort -t: -k3 -n /etc/passwd | tail -10 | tr ‘a-z‘ ‘A-Z‘ | tee/tmp/maxuser.txt ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN PULSE:X:497:495:PULSEAUDIOSYSTEM DAEMON:/VAR/RUN/PULSE:/SBIN/NOLOGIN SASLAUTH:X:498:76:SASLAUTHDUSER:/VAR/EMPTY/SASLAUTH:/SBIN/NOLOGIN RTKIT:X:499:496:REALTIMEKIT:/PROC:/SBIN/NOLOGIN GENTOO:X:500:500:GENTOODISTRIBUTION:/HOME/GENTOO:/BIN/CSH BASH:X:501:501::/HOME/BASH:/BIN/BASH TESTBASH:X:502:502::/HOME/TESTBASH:/BIN/BASH BASHER:X:503:503::/HOME/BASHER:/BIN/BASH NOLOGIN:X:504:714::/HOME/NOLOGIN:/SBIN/NOLOGIN NFSNOBODY:X:65534:65534:ANONYMOUSNFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
4、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[[email protected]~]# ifconfig eth0 | egrep --color -o ‘\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-3])\>(\.\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\>){2}\.\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\>‘ 192.168.20.4
5、显示/var目录下一级子目录或文件的总个数。
[[email protected]~]# echo ‘/var目录中共有子目录:‘ $(ll /var | grep --color=auto -o ‘^d‘ | uniq -c | grep--color=auto -o ‘\<[[:digit:]]\+\>‘) /var目录中共有子目录: 20 [[email protected]~]# echo ‘/var目录中共有文件:‘ $(ll /var | grep --color=auto -o ‘^[a-z]‘ | grep --color=auto -o‘^[^d]‘ | uniq -c | grep --color=auto -o ‘\<[[:digit:]]\+\>‘) /var目录中共有文件: 1
6、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[[email protected]~]# sort -t: -k3 -n /etc/group | head -10 | grep -o ‘^[[:alpha:]]\+\>‘ 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 | tee /tmp/etc.test # #/etc/fstab #Created by anaconda on Wed Aug 3 20:31:152016 # #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 # UUID=ab07b266-6827-42f8-84cc-e697705dc1af/ ext4 defaults 1 1 UUID=baf8e327-f6f5-472f-a1d1-be289dc89ac5/boot ext4 defaults 1 2 UUID=6f0d8fb5-8889-418b-8ad9-f895a287b8af/home ext4 defaults 1 2 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 CentOSrelease 6.6 (Final) Kernel\r on an \m
8、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
[[email protected]~]# groupadd -g 2016 distro && grep --color=auto ‘^\<distro\>‘/etc/group distro:x:2016:
(2)、创建用户mandriva,其ID号为1005;基本组为distro;
[[email protected]~]# useradd -u 1005 -g distro mandriva && grep --color=auto‘^\<mandriva\>‘ /etc/passwd && echo -e ‘\n‘ && grep--color=auto ‘^\<distro\>‘ /etc/group mandriva:x:1005:2016::/home/mandriva:/bin/bash distro:x:2016:
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[[email protected]~]# useradd -u 1100 -d /home/linux mageia && grep --color=auto‘^\<mageia\>‘ /etc/passwd mageia:x:1100:1100::/home/linux:/bin/bash
(4)、给用户mageia添加密码,密码为mageedu;
[[email protected]~]# echo ‘mageedu‘ | passwd --stdin mageia 更改用户 mageia 的密码 。 passwd: 所有的身份验证令牌已经成功更新。
(5)、删除mandriva,但保留其家目录;
[[email protected]~]# userdel mandriva && ll -a /home/mandriva/ 总用量 14 drwx------.4 1005 distro 1024 12月 25 01:22 . drwxr-xr-x.9 root root 1024 12月 25 01:25 .. -rw-r--r--.1 1005 distro 18 10月 16 2014 .bash_logout -rw-r--r--.1 1005 distro 176 10月 16 2014.bash_profile -rw-r--r--.1 1005 distro 124 10月 16 2014 .bashrc drwxr-xr-x.2 1005 distro 1024 11月 12 2010 .gnome2 drwxr-xr-x.4 1005 distro 1024 8月 3 20:31 .mozilla
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[[email protected]~]# groupadd peguin && useradd -u 2002 -g distro -G peguin slackware&& grep --color=auto ‘^\<slackware\>‘ /etc/passwd && echo-e ‘\n‘ && egrep --color=auto ‘^\<distro\>|^\<peguin\>‘ /etc/group slackware:x:2002:2016::/home/slackware:/bin/bash distro:x:2016: peguin:x:2017:slackware
(7)、修改slackware的默认shell为/bin/tcsh;
[[email protected]~]# usermod -s /bin/tcsh slackware && grep --color=auto‘^\<slackware\>‘ /etc/passwd slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins;
[[email protected]~]# groupadd admins && usermod -G admins slackware && grep--color=auto ‘^\<slackware\>‘ /etc/passwd && grep --color=auto‘^\<admins\>‘ /etc/group slackware:x:2002:2016::/home/slackware:/bin/tcsh admins:x:2018:slackware
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[[email protected]~]# echo ‘slackware‘ | passwd --stdin slackware && passwd -n 3 -x 180-w 3 slackware && grep --color=auto ‘^\<slackware\>‘ /etc/shadow 更改用户 slackware 的密码 。 passwd: 所有的身份验证令牌已经成功更新。 调整用户密码老化数据slackware。 passwd:操作成功 slackware:$6$XGgPzQOZ$Wv1myOfzM8Kwh4vIUExB8km4KfVNx3981Yv9HFV1VYifM1zeFO1Qi3p1nJv2xp.8N8NreKuQ7QZZl2e6jkSeG1:17159:3:180:3:::
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[[email protected]~]# groupadd clouds && groupadd nova && useradd -u 3003 -gclouds -G peguin,nova openstack && grep --color=auto‘^\<openstack\>‘ /etc/passwd && egrep --color=auto‘^\<clouds\>|^\<peguin\>|^\<nova\>‘ /etc/group openstack:x:3003:2019::/home/openstack:/bin/bash peguin:x:2017:openstack clouds:x:2019: nova:x:2020:openstack
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[[email protected]~]# userdel -r mysql && useradd -r -s /sbin/nologin mysql &&grep --color=auto ‘^\<mysql\>‘ /etc/passwd mysql:x:496:492::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[[email protected]~]# echo ‘openstack‘ | passwd --stdin openstack 更改用户 openstack 的密码 。 passwd: 所有的身份验证令牌已经成功更新。
9、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
[[email protected]]# cp -r /etc/skel /home/tuser1 && chmod -R g-rwx,o-rwx/home/tuser1 && ll -a /home/tuser1 总用量 14 drwx------.4 root root 1024 12月 23 22:57 . drwxr-xr-x.8 root root 1024 12月 23 22:57 .. -rw-------.1 root root 18 12月 23 22:57.bash_logout -rw-------.1 root root 176 12月 23 22:57.bash_profile -rw-------.1 root root 124 12月 23 22:57 .bashrc drwx------.2 root root 1024 12月 23 22:57 .gnome2 drwx------.4 root root 1024 12月 23 22:57 .mozilla
10、使用两种方式显示/proc/meminfo文件中以大写或小写S开头的行;
[[email protected]~]# grep -i ‘^s‘ /proc/meminfo SwapCached: 0 kB SwapTotal: 0 kB SwapFree: 0 kB Shmem: 232 kB Slab: 70240 kB SReclaimable: 12892 kB SUnreclaim: 57348 kB [[email protected]~]# grep ‘^[Ss]‘ /proc/meminfo SwapCached: 0 kB SwapTotal: 0 kB SwapFree: 0 kB Shmem: 232 kB Slab: 70244 kB SReclaimable: 12892 kB SUnreclaim: 57352 kB
11、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
[[email protected]~]# grep --color=auto -v ‘/sbin/nologin$‘ /etc/passwd root:x:0:0:root:/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 mysql:x:27:27:MySQLServer:/var/lib/mysql:/bin/bash
12、显示/etc/passwd文件中其默认shell为/bin/bash的用户;
[[email protected]~]# grep --color=auto ‘/bin/bash$‘ /etc/passwd root:x:0:0:root:/root:/bin/bash mysql:x:27:27:MySQLServer:/var/lib/mysql:/bin/bash
13、找出/etc/passwd文件中的一位数或两位数;
[[email protected]]# grep --color=auto ‘\<[[:digit:]]\{1,2\}\>‘ /etc/passwd root:x:0:0:Administrator,China,123456,654321:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:Systemmessage bus:/:/sbin/nologin vcsa:x:69:69:virtualconsole memory owner:/dev:/sbin/nologin rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin rpcuser:x:29:29:RPCService User:/var/lib/nfs:/sbin/nologin haldaemon:x:68:68:HALdaemon:/:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin saslauth:x:498:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin mysql:x:27:27:MySQLServer:/var/lib/mysql:/bin/bash sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin
14、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
[[email protected]~]# grep --color=auto ‘^#[[:space:]]\+[^[:space:]]\+‘ /etc/rc.d/rc.sysinit #/etc/rc.d/rc.sysinit - run once at boot time #Taken in part from Miquel van Smoorenburg‘s bcheckrc. #Check SELinux status #Print a text banner. #Only read this once. #Initialize hardware #Set default affinity #Load other user-defined modules #Load modules (for backward compatibility with VARs) #Configure kernel parameters #Set the hostname. #Sync waiting for storage. #Device mapper & related initialization #Start any MD RAID arrays that haven‘t been started yet #Remount the root filesystem read-write. #Clean up SELinux labels #If relabeling, relabel mount points. #Mount all other filesystems (except for NFS and /proc, which is already #mounted). Contrary to standard usage, #filesystems are NOT unmounted in single user mode. #The ‘no‘ applies to all listed filesystem types. See mount(8). #Update quotas if necessary #Check to see if a full relabel is needed #Initialize pseudo-random number generator #Configure machine if necessary. #Clean out /. #Do we need (w|u)tmpx files? We don‘t set them up, but the sysadmin might... #Clean up /var. #Clean up utmp/wtmp #Clean up various /tmp bits #Make ICE directory #Start up swapping. #Set up binfmt_misc #Boot time profiles. Yes, this should be somewhere else. #Now that we have all of our basic modules loaded and the kernel going, #let‘s dump the syslog ring somewhere so we can find it later #create the crash indicator flag to warn on crashes, offer fsck with timeout #Let rhgb know that we‘re leaving rc.sysinit
15、打出netstat-tan命令执行结果中以‘LISTEN’后或跟空白字符结尾的行;
[[email protected]~]# netstat -tan | grep ‘LISTEN[[:space:]]*$‘ 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 tcp 0 0 0.0.0.0:39104 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN tcp 0 0 :::41289 :::* LISTEN tcp 0 0 :::111 :::* LISTEN
16、添加用户bash,testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
[[email protected]~]# useradd bash && useradd testbash && useradd basher&& useradd -s /sbin/nologin nologin && grep --color=auto‘\(^\<[[:alpha:]]\+\>\).*\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:500:500::/home/bash:/bin/bash nologin:x:503:503::/home/nologin:/sbin/nologin
以上是关于Linux+Python运维培训班第4期--马哥——第2次作业(20161225)的主要内容,如果未能解决你的问题,请参考以下文章
Linux+Python运维培训班第4期--马哥——第3次作业(20170212)
Linux+Python运维培训班第4期--马哥——第2次作业(20161225)