1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@centos ~]# who | awk '{print $1 $NF}'| uniq -d root(192.168.40.1) derulo(192.168.40.1) [root@centos ~]# who root tty1 2016-07-16 20:27 (:0) root pts/0 2016-07-16 20:28 (:0.0) root pts/1 2016-07-16 20:29 (192.168.40.1) root pts/2 2016-07-16 21:54 (192.168.40.1) root pts/3 2016-07-16 22:59 (192.168.40.1) root pts/4 2016-07-16 23:07 (192.168.40.1) root pts/5 2016-07-16 23:45 (192.168.40.1) derulo pts/6 2016-07-17 00:06 (192.168.40.1) derulo pts/7 2016-07-17 00:07 (192.168.40.1)
2、取出最后登录到当前系统的用户的相关信息。
[root@centos ~]# lastlog | grep -v "Never" Username Port From Latest root pts/2 192.168.40.1 Sun Jul 17 02:09:12 +0800 2016 derulo pts/7 192.168.40.1 Sun Jul 17 00:07:18 +0800 2016
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
[root@centos ~]# cut -d: -f7 /etc/passwd | uniq -c | sort -n -u -r -k1 | head -1 | awk '{print $NF}' /sbin/nologin [root@centos ~]# cut -d: -f7 /etc/passwd | uniq -c | sort -n -u -r -k1 | head -1 25 /sbin/nologin
4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@centos ~]# sort -t: -k3 -n /etc/passwd | tail -10 > /tmp/maxusers.txt [root@centos ~]# cat /tmp/maxusers.txt postfix:x:89:89::/var/spool/postfix:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin derulo:x:500:500::/home/derulo:/bin/bash nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[root@centos ~]# ifconfig | grep 'inet addr' | cut -d: -f2 192.168.40.128 Bcast 127.0.0.1 Mask [root@centos ~]# ifconfig | grep 'inet addr' | cut -d: -f2 | cut -d " " -f1 192.168.40.128 127.0.0.1
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@centos ~]# ls /etc/*.conf | tr 'a-z' 'A-Z'> /tmp/etc.conf [root@centos ~]# cat /tmp/etc.conf /ETC/ASOUND.CONF /ETC/AUTOFS.CONF /ETC/AUTOFS_LDAP_AUTH.CONF /ETC/CAS.CONF /ETC/DNSMASQ.CONF /ETC/DRACUT.CONF /ETC/FPRINTD.CONF /ETC/GAI.CONF /ETC/GRUB.CONF /ETC/GSSAPI_MECH.CONF /ETC/HOST.CONF /ETC/IDMAPD.CONF
7、显示/var目录下一级子目录或文件的总个数。
[root@centos var]# ls -l | grep -v '^d'| wc -l 4 [root@centos var]# ls -l | grep '^d'| wc -l 22
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@centos var]# sort -t: -k3 -n /etc/group | head -10 | cut -d: -f1 root bin daemon sys adm tty disk lp mem kmem
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@centos var]# cat /etc/fstab /etc/issue | tee /tmp/etc.test # # /etc/fstab # Created by anaconda on Thu Jun 2 18:06:52 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/vg_centos-lv_root / ext4 defaults 1 1 UUID=19bb5e83-9587-4a9a-b2e9-e4a26fdb9e29 /boot ext4 defaults 1 2 /dev/mapper/vg_centos-lv_swap 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.6 (Final) Kernel \r on an \m
10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
命令总结:useradd, groupadd, su, id, usermod, userdel, groupmod, groupdel, passwd, newgrp, pwck, gpasswd, chage, chsh, chfn, finger
(1)、创建组distro,其GID为2016;
[root@centos ~]# groupadd -g 2016 distro [root@centos ~]# groups distro groups: distro: No such user
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@centos ~]# useradd -u 1005 -g distro mandriva [root@centos ~]# id mandriva uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@centos ~]# useradd -u 1100 -d /home/linux mageia [root@centos ~]# id mageia uid=1100(mageia) gid=1100(mageia) groups=1100(mageia) [root@centos ~]# cd /home/ [root@centos home]# ls derulo jason linux mandriva
(4)、给用户mageia添加密码,密码为mageedu;
[root@centos home]# passwd mageia Changing password for user mageia. New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully.
(5)、删除mandriva,但保留其家目录;
[root@centos home]# userdel mandriva [root@centos home]# ls derulo jason linux mandriva
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@centos ~]# groupadd peguin [root@centos ~]# useradd -u 2002 -g distro -G peguin slackware [root@centos ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
(7)、修改slackware的默认shell为/bin/tcsh;
[root@centos ~]# usermod -s /bin/tcsh slackware [root@centos ~]# cat /etc/passwd | grep "slackware" slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins;
[root@centos ~]# groupadd admins [root@centos ~]# usermod -a -G admins slackware [root@centos ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@centos ~]# passwd -n3 -x180 -w3 slackware Adjusting aging data for user slackware. passwd: Success
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[root@centos ~]# useradd -u 3003 -g clouds -G peguin,nova openstack [root@centos ~]# id openstack uid=3003(openstack) gid=2020(clouds) groups=2020(clouds),2017(peguin),2019(nova)
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[root@centos ~]# useradd -s /sbin/nologin mysql [root@centos ~]# id mysql uid=3004(mysql) gid=3004(mysql) groups=3004(mysql) [root@centos ~]# cat /etc/passwd | grep "mysql" mysql:x:3004:3004::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[root@centos ~]# echo "openstackpass" | passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully.
原创文章,作者:Snoo,如若转载,请注明出处:http://www.178linux.com/24874
评论列表(2条)
写的很好,排版也很棒,加油
[root@centos ~]# who | awk ‘{print $1 $NF}’| uniq -d 改为 [root@centos ~]# who | awk ‘{print $1 $NF}’| sort -u 来去重