1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[[email protected] ~]# who | cut -d ‘ ‘ -f 1 //切割字符串,-d指明字符为空格,-f,显示第1列 jun_shao jun_shao jun_shao jun_shao root root [[email protected] ~]# who | cut -d ‘ ‘ -f 1 | sort -u jun_shao root
2、取出最后登录到当前系统的用户的相关信息。
[[email protected] ~]$ who | sort -r -k 3 jun_shao pts/5 2016-08-20 01:09 (192.168.0.100) root pts/4 2016-08-20 00:34 (192.168.0.100) root pts/3 2016-08-20 00:34 (192.168.0.100) jun_shao pts/2 2016-08-20 00:33 (192.168.0.100) jun_shao pts/1 2016-08-20 00:33 (192.168.0.100) jun_shao pts/0 2016-08-20 00:32 (:0) jun_shao :0 2016-08-20 00:31 (:0) [[email protected] ~]$ who | sort -r -k 3 | head -n 1 jun_shao pts/5 2016-08-20 01:09 (192.168.0.100) [[email protected] ~]$ who | sort -r -k 3 | head -n 1 > /tmp/last.txt [[email protected] ~]$ cat /tmp/last.txt jun_shao pts/5 2016-08-20 01:09 (192.168.0.100)
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
[[email protected] ~]$ cut -d: -f 7 /etc/passwd | sort | uniq -c | sort -f | tail -n 1 > /tmp/shell.txt [[email protected] ~]$ cat /tmp/shell.txt 39 /sbin/nologin
4、将/etc/passwd 中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[[email protected] ~]$ sort -t : -k 3 -n /etc/passwd | tail -n 10 | tr [a-z] [A-Z] > /tmp/maxusers.txt [[email protected] ~]$ cat /tmp/maxusers.txt GEOCLUE:X:992:989:USER FOR GEOCLUE:/VAR/LIB/GEOCLUE:/SBIN/NOLOGIN LIBSTORAGEMGMT:X:993:990:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/SBIN/NOLOGIN SASLAUTH:X:994:76:SASLAUTHD USER:/RUN/SASLAUTHD:/SBIN/NOLOGIN COLORD:X:995:992:USER FOR COLORD:/VAR/LIB/COLORD:/SBIN/NOLOGIN UNBOUND:X:996:993:UNBOUND DNS RESOLVER:/ETC/UNBOUND:/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 JUN_SHAO:X:1000:1000:JUN_SHAO:/HOME/JUN_SHAO:/BIN/BASH NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[[email protected] ~]$ ifconfig eno16777736 | grep -w "inet" | cut -d ‘ ‘ -f 10 192.168.0.101
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[[email protected] jun_shao]# ls /etc/ |grep ‘\.conf$‘ > /tmp/etc.conf [[email protected] jun_shao]# cat /tmp/etc.conf asound.conf autofs.conf autofs_ldap_auth.conf brltty.conf cgconfig.conf cgrules.conf cgsnapshot_blacklist.conf chrony.conf dleyna-server-service.conf dnsmasq.conf dracut.conf e2fsck.conf fprintd.conf fuse.conf hba.conf host.conf idmapd.conf ipsec.conf kdump.conf krb5.conf ksmtuned.conf ld.so.conf libaudit.conf libuser.conf locale.conf logrotate.conf man_db.conf mke2fs.conf mtools.conf nfsmount.conf nsswitch.conf ntp.conf numad.conf oddjobd.conf pbm2ppa.conf pnm2ppa.conf radvd.conf request-key.conf resolv.conf rsyncd.conf rsyslog.conf sestatus.conf sos.conf sudo.conf sudo-ldap.conf sysctl.conf tcsd.conf updatedb.conf usb_modeswitch.conf vconsole.conf wvdial.conf
7、显示/var目录下一级子目录或文件的总个数。
[[email protected] ~]$ ll -a /var/ | wc -l 26
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[[email protected] ~]$ cat /etc/group | sort -t: -k 3 -n -r | tail -n 10 kmem:x:9: mem:x:8: lp:x:7: disk:x:6: tty:x:5: adm:x:4: sys:x:3: daemon:x:2: bin:x:1: root:x:0:
9、将/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 Wed Aug 17 16:50:22 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=be50a901-277e-4252-b8be-717299df19f5 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 \S Kernel \r on an \m
10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
[[email protected] jun_shao]# groupadd -g 2016 distro
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[[email protected] jun_shao]# useradd -u 1005 -g distro mandriva
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[[email protected] jun_shao]# useradd -u 1100 -d /home/linux mageia
(4)、给用户mageia添加密码,密码为mageedu;
[[email protected] jun_shao]# passwd mageia Changing password for user mageia. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully.
(5)、删除mandriva,但保留其家目录;
[[email protected] home]# userdel mandriva [[email protected] home]# ls jun_shao linux mandriva
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[[email protected] home]# groupadd peguin [[email protected] home]# useradd -u 2002 -g distro -G peguin slackware [[email protected] home]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
(7)、修改slackware的默认shell为/bin/tcsh;
[[email protected] home]# usermod -s /bin/tcsh slackware
[[email protected] home]# tail -n 1 /etc/passwd slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins;
[[email protected] jun_shao]# usermod -a -G admins slackware [[email protected] jun_shao]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[[email protected] jun_shao]# passwd slackware | passwd slackware -n 3 -x 180 -w 3 Adjusting aging data for user slackware. passwd: Success New password: BAD PASSWORD: The password is shorter than 7 characters Retype new password: [[email protected] jun_shao]# tail -n 1 /etc/shadow slackware:$6$6SP2wfD/$9OyF6m0WBSnB/QvMeKcy/Qaa494ctEDVDWX2jktapof..6Ud.yCEnkPE3bYUWfr20wy6DWdIU6x1FIoum6Kd20:17033:3:180:3:::
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[[email protected] jun_shao]# useradd -u 3003 -g clouds -G peguin,nova openstack [[email protected] jun_shao]# id openstack uid=3003(openstack) gid=2019(clouds) groups=2019(clouds),2017(peguin),2020(nova)
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[[email protected] jun_shao]# useradd -r -s /sbin/nologin mysql [[email protected] jun_shao]# tail -n 1 /etc/passwd mysql:x:987:982::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[[email protected] jun_shao]# echo "admin" | passwd --stdin openstack