全新Linux+Python高端运维班第二次作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了全新Linux+Python高端运维班第二次作业相关的知识,希望对你有一定的参考价值。
1.列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可
[[email protected] ~]# who | cut -d ‘ ‘ -f1 | sort -u root
2.取出当前系统上被用户当作其默认shell的最多的那个shell
[[email protected] ~]# cat /etc/passwd | cut -d ‘:‘ -f7 | uniq -c | sort -nr | head -1 22 /sbin/nologin
3.将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中
[[email protected] ~]# cat /etc/passwd | sort -n -t ‘:‘ -k3 |tail | tr ‘a-z‘ ‘A-Z‘ >/tmp/maxusers.txt
[[email protected] ~]# cat /tmp/maxusers.txt AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN OPENERP:X:494:501::/OPT/OPENERP/:/BIN/BASH UNBOUND:X:495:491:UNBOUND DNS RESOLVER:/ETC/UNBOUND:/SBIN/NOLOGIN VBOXADD:X:496:1::/VAR/RUN/VBOXADD:/BIN/FALSE PULSE:X:497:495:PULSEAUDIO SYSTEM DAEMON:/VAR/RUN/PULSE:/SBIN/NOLOGIN SASLAUTH:X:498:76:"SASLAUTHD USER":/VAR/EMPTY/SASLAUTH:/SBIN/NOLOGIN RTKIT:X:499:496:REALTIMEKIT:/PROC:/SBIN/NOLOGIN SAM:X:500:500:SAMUEL:/HOME/SAM:/BIN/BASH NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
4.取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分
[[email protected] ~]# ifconfig|grep ‘inet[[:space:]].*‘ | cut -d ‘:‘ -f2 | cut -d ‘ ‘ -f1 172.18.11.121 127.0.0.1
5.显示/var目录下一级子目录或文件的总个数
[[email protected] ~]# tree -L 1 /var | tail -n 1 22 directories, 0 files
6.取出/etc/group文件中第三个字段数值最小的10个组的名字
[root[email protected] ~]# cat /etc/group | sort -n -t ‘:‘ -k3 | head | 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 Sun Nov 6 13:58:01 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 # UUID=4426c2ca-c804-4bd9-b071-9e7c83e12625 / ext4 defaults 1 1 UUID=77540bb8-0514-478c-8b63-ca165ab1d6a4 /boot ext4 defaults 1 2 UUID=300c1246-2d1f-4a8f-880e-b75556d52037 /home ext4 defaults 1 2 UUID=274f62f3-a54f-4d36-b9ec-25ebfce2af16 /opt ext4 defaults 1 2 UUID=82a0e328-a2f5-4e9a-8d4b-44b1f0b9e06c swap swap defaults 0 0 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 CentOS release 6.8 (Final) Kernel \r on an \m
8.请总结描述用户和组管理类命令的使用方法并完成以下练习:
创建用户命令:useradd 其用法:useradd [option] username
option:
-u:设定用户UID
-g:指定用户所属基本组
-c:添加用户详细信息
-d:设定用户主目录
-s:设定用户默认shell
删除用户命令:userdel 其用法:userdel [option] username
option:
-r:在删除用户时,删除用户主目录
用户添加密码:passwd 其用法:passwd [option] username
option:
-l:锁定用户
-u:解锁用户
用户属性修改:usermod 其用法:usermod [option] username
option:
-u:用户更换新UID
-g:用户更换新GID
-G:设定新的主目录,如果需要移动用户文件,需用同时使用-m选项
-l:设定新的用户名
-L:锁定用户
-U:解锁用户
创建组命令:groupadd 其用法:groupadd [option] groupname
option:
-g:指定组GID
-r:创建系统组
删除组命令:groupdel 其用法:groupdel groupname
组密码添加:gpasswd 其用法:gpasswd [option] group
option:
-a:添加指定用户至指定组
-d:从指定组中删除指定用户
-A:设定有管理员权限的用户
(1)、创建组distro,其GID为2016;
[[email protected] ~]# groupadd -g 2016 distro [[email protected] ~]# cat /etc/group | grep distro distro:x:2016:
(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] ~]# cat /etc/passwd | grep mageia mageia:x:1100:1100::/home/linux:/bin/bash
(4)、给用户mageia添加密码,密码为mageedu;
[[email protected] ~]# passwd mageia [[email protected] ~]# cat /etc/shadow | grep mageia mageia:$6$R82Ll/CQ$gzuzsUbmoTtFhzwYb3WLb8pJ84gYrrqgCMQ8XKDrLoo0oLgBUhYo9VycKZGpg7oJi2YKgopXB9ioKbuQuzMRU.:17160:0:99999:7:::
(5)、删除mandriva,但保留其家目录;
[[email protected] ~]# userdel mandriva [[email protected] ~]# id mandriva id: mandriva: No such user [[email protected] ~]# ls -l /home/ total 28 drwx------. 4 mageia mageia 4096 Dec 25 11:44 linux drwx------. 2 root root 16384 Nov 6 13:56 lost+found drwx------. 4 1005 distro 4096 Dec 25 11:41 mandriva drwx------. 29 sam sam 4096 Dec 16 16:35 sam
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[[email protected] ~]# useradd -u 2002 -g distro -Gpeguin 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 [[email protected] ~]# cat /etc/passwd | grep slackware slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins;
[[email protected] ~]# gpasswd -a slackware admins Adding user slackware to group admins [[email protected] ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[[email protected] ~]# passwd -n 3 -x 180 -w 3 slackware [[email protected] ~]# passwd slackware [[email protected] ~]# cat /etc/shadow | grep slackware slackware:$6$GQZ4Tacc$tgBjVYIKAQUmg1l2VIimrEwEVn5fTOEBlO3XLsBjk4H0PAcaLwG0andgr9FwWRMN9pJmOjJ559vYoRYDj3voo0:17160:3:180:3:::
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[[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 -rs /sbin/nologin mysql [[email protected] ~]# cat /etc/passwd | grep mysql mysql:x:493:490::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[[email protected] ~]# echo "mageedu" | passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully. [[email protected] ~]# cat /etc/shadow | grep openstack openstack:$6$cLZAIEmy$DgbAhNEK9PFNYFcEX5BAZlnSiH7dfrhBZQiXiLA7N/KGMdyWTEBbVqBoAf/uZu3xS//KnOXnqUF5ANlYwpGOf/:17160:0:99999:7:::
9.复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限
[[email protected] ~]# cp -ar /etc/skel /home/tuser1 [[email protected] ~]# chmod -R 600 /home/tuser1 [[email protected] ~]# ll -a /home/tuser1 total 28 drw-------. 4 root root 4096 Nov 6 16:06 . drwxr-xr-x. 9 root root 4096 Dec 25 12:58 .. -rw-------. 1 root root 18 May 11 2016 .bash_logout -rw-------. 1 root root 176 May 11 2016 .bash_profile -rw-------. 1 root root 124 May 11 2016 .bashrc drw-------. 2 root root 4096 Nov 12 2010 .gnome2 drw-------. 4 root root 4096 Nov 6 13:58 .mozilla
[[email protected] ~]# ll -a /home total 48 drwxr-xr-x. 9 root root 4096 Dec 25 12:58 . dr-xr-xr-x. 25 root root 4096 Dec 25 09:35 .. drwx------. 4 mageia mageia 4096 Dec 25 11:44 linux drwx------. 2 root root 16384 Nov 6 13:56 lost+found drwx------. 4 1005 distro 4096 Dec 25 11:41 mandriva drwx------. 4 openstack clouds 4096 Dec 25 12:06 openstack drwx------. 29 sam sam 4096 Dec 16 16:35 sam drwx------. 4 slackware distro 4096 Dec 25 11:52 slackware drw-------. 4 root root 4096 Nov 6 16:06 tuser1
10.显示/proc/meminfo文件中以大写或小写S开头的行
[[email protected] ~]# grep ‘^[sS].*‘ /proc/meminfo SwapCached: 0 kB SwapTotal: 4094972 kB SwapFree: 4094972 kB Shmem: 220 kB Slab: 110528 kB SReclaimable: 51020 kB SUnreclaim: 59508 kB
[[email protected] ~]# grep ‘\b[sS].*‘ /proc/meminfo SwapCached: 0 kB SwapTotal: 4094972 kB SwapFree: 4094972 kB Shmem: 220 kB Slab: 110552 kB SReclaimable: 51020 kB SUnreclaim: 59532 kB
11.显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户
[[email protected] ~]# cat /etc/passwd | grep ‘.*[^/sbin/nologin]$‘ root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync halt:x:7:0:halt:/sbin:/sbin/halt sam:x:500:500:samuel:/home/sam:/bin/bash vboxadd:x:496:1::/var/run/vboxadd:/bin/false openerp:x:494:501::/opt/openerp/:/bin/bash postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/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] ~]# cat /etc/passwd | grep ‘.*bash$‘ root:x:0:0:root:/root:/bin/bash sam:x:500:500:samuel:/home/sam:/bin/bash openerp:x:494:501::/opt/openerp/:/bin/bash postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash openstack:x:3003:2020::/home/openstack:/bin/bash
13.找出/etc/passwd文件中的一位数或两位数
[[email protected] ~]# grep ‘\b[0-9]\{1,2\}\b‘ /etc/passwd root:x:0:0:root:/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:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/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:"Saslauthd user":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin vboxadd:x:496:1::/var/run/vboxadd:/bin/false postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
14.显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行
[[email protected] ~]# grep ‘^#[[: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). # Check to see if a full relabel is needed # Update quotas if necessary # 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\b‘ tcp 0 0 0.0.0.0:36015 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 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 tcp 0 0 :::59501 :::* LISTEN tcp 0 0 :::111 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 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 nologin [[email protected] ~]# tail -n 4 /etc/passwd bash:x:3004:3004::/home/bash:/bin/bash testbash:x:3005:3005::/home/testbash:/bin/bash basher:x:3006:3006::/home/basher:/bin/bash nologin:x:3007:3007::/home/nologin:/sbin/nologin
[[email protected] ~]# grep "^\([[:alnum:]]\{1,\}\):.*\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晋级之路” 博客,转载请与作者联系!
以上是关于全新Linux+Python高端运维班第二次作业的主要内容,如果未能解决你的问题,请参考以下文章
马哥2016全新Linux+Python高端运维班第五周作业
[作业] 马哥2016全新Linux+Python高端运维班第八周作业