1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[xw@localhost ~]$ who | cut -d' ' -f1 | uniq xw root
2、取出最后登录到当前系统的用户的相关信息。
[xw@localhost ~]$ last | head -1 xw pts/3 192.168.1.188 Wed Aug 31 11:02 still logged in
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
[xw@localhost ~]$ cat /etc/passwd | cut -d: -f7 | uniq -c | sort -n | tail -1 | cut -d' ' -f7 /sbin/nologin
4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[xw@localhost ~]$ cat /etc/passwd | sort -n -t: -k3 | tail -10 | tr [a-z] [A-Z] > /tmp/maxusers.txt [xw@localhost ~]$ cat /tmp/maxusers.txt SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGIN DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGIN NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN CHRONY:X:996:994::/VAR/LIB/CHRONY:/SBIN/NOLOGIN POLKITD:X:997:995:USER FOR POLKITD:/:/SBIN/NOLOGIN SYSTEMD-NETWORK:X:998:996:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN SYSTEMD-BUS-PROXY:X:999:997:SYSTEMD BUS PROXY:/:/SBIN/NOLOGIN XW:X:1000:1000:XW:/HOME/XW:/BIN/BASH
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[xw@localhost ~]$ ifconfig eno16777736 | head -2 | tail -1 | cut -d' ' -f10 192.168.1.189
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[xw@localhost ~]$ ls /etc/*.conf | tr [a-z] [A-Z] > /tmp/etc.conf [xw@localhost ~]$ cat /tmp/etc.conf /ETC/ASOUND.CONF /ETC/CHRONY.CONF /ETC/DNSMASQ.CONF /ETC/DRACUT.CONF /ETC/E2FSCK.CONF /ETC/HOST.CONF /ETC/KDUMP.CONF /ETC/KRB5.CONF /ETC/LD.SO.CONF /ETC/LIBAUDIT.CONF /ETC/LIBUSER.CONF /ETC/LOCALE.CONF /ETC/LOGROTATE.CONF /ETC/MAN_DB.CONF /ETC/MKE2FS.CONF /ETC/NSSWITCH.CONF /ETC/RESOLV.CONF /ETC/RSYSLOG.CONF /ETC/SESTATUS.CONF /ETC/SUDO.CONF /ETC/SUDO-LDAP.CONF /ETC/SYSCTL.CONF /ETC/TCSD.CONF /ETC/VCONSOLE.CONF /ETC/YUM.CONF
7、显示/var目录下一级子目录或文件的总个数。
[xw@localhost ~]$ ls /var/ | wc -l 28
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[xw@localhost ~]$ cat /etc/group | sort -n -t: -k3 | head -10 | cut -d: -f1 root bin daemon sys adm tty disk lp mem kmem
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[xw@localhost ~]$ cat /etc/fstab /etc/issue > /tmp/etc.test [xw@localhost ~]$ cat /tmp/etc.test # # /etc/fstab # Created by anaconda on Mon Jul 25 15:59:48 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=24e642e2-1a7e-4250-bd9a-334d16770b21 /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
10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)创建组distro,其GID为2016;
[root@localhost ~]# groupadd -g 2016 distro [root@localhost ~]# cat /etc/group | tail -1 distro:x:2016:
(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 ~]# cat /etc/passwd | tail -1 mageia:x:1100:1100::/home/linux:/bin/bash
(4)给用户mageia添加密码,密码为mageedu;
[root@localhost ~]# echo "mageedu" | passwd --stdin mageia Changing password for user mageia. passwd: all authentication tokens updated successfully.
(5)删除mandriva,但保留其家目录;
[root@localhost ~]# userdel mandriva [root@localhost ~]# ls -d /home/mandriva/ /home/mandriva/
(6)创建用户slackware,其ID号为2002,基本组为distro,附加组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 [root@localhost ~]# cat /etc/passwd | tail -1 slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)为用户slackware新增附加组admins;
[root@localhost ~]# usermod -a -G admins slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
(9)为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@localhost ~]# echo "slackware" | passwd --stdin slackware Changing password for user slackware. passwd: all authentication tokens updated successfully. [root@localhost ~]# passwd -n 3 -x 180 -w 3 slackware Adjusting aging data for user slackware. passwd: Success [root@localhost ~]# cat /etc/shadow | tail -1 slackware:$6$73CNb07U$Q2bGLwVIRgdZO9B9Fnmw/3f/yC5lrduyJafIeiPH45hflLPYRP3Iu1JgV8xk72y2S28KDUrVZW/7ZxUfuIrTi1:17044:3:180:3:::
(10)添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[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 mysql [root@localhost ~]# cat /etc/passwd | tail -1 mysql:x:3004:3004::/home/mysql:/sbin/nologin
(12)使用echo命令,非交互式为openstack添加密码。
[root@localhost ~]# echo "openstack" | passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully. [root@localhost ~]# cat /etc/shadow | tail -2 | head -1 openstack:$6$3QUK5pYT$uZQna5BiY1tacvQ9GshKW8t69Xb47GaVvhkkn0Le7.DDcDFaaYScsbBVYylzlDeV71naDUP7sXi.seZT8pvtd1:17044:0:99999:7:::
原创文章,作者:萝卜,如若转载,请注明出处:http://www.178linux.com/41798
评论列表(3条)
第5题是实现了,可以换用正则来匹配IP地址,建议尝试下。
@马哥教育:ifconfig | grep -E -o “inet[[:space:]]([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])” | head -1
老师这样可以吗