1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。

[root@lanin ~]# who    #显示当前用户登录信息
root     tty1         2016-08-15 15:58
root     pts/0        2016-08-15 16:07 (192.168.1.118)
[root@lanin ~]# who | cut -d' ' -f1    #取出当前用户登录信息的用户名
root
root
[root@lanin ~]# who | cut -d' ' -f1|uniq    #对当前用户登录信息的用户名去重
root


 

2、取出最后登录到当前系统的用户的相关信息。

[root@lanin ~]# who |tail -n 1    #取当前用户最后登录的信息的一行数据
root     pts/0        2016-08-15 16:07 (192.168.1.118)
[root@lanin ~]# who |tail -n 1|cut -d' ' -f1    #取出用户名
root
[root@lanin ~]# who |tail -n 1|cut -d' ' -f1|id    #查询用户相关信息
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023


 

3、取出当前系统上被用户当作其默认shell的最多的那个shell。

[root@lanin ~]# cut -d: -f7 /etc/passwd|uniq -c|sort -n
##取出用户默认shell相同的用户数量及其相应shell,并顺序排序
[root@lanin ~]# cut -d: -f7 /etc/passwd|uniq -c|sort -n|tail -n 1
##取出最后一行即为默认shell用户最多的shell


 

4、将/etc/passwd 中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。

[root@lanin~]# cat /etc/passwd|sort -n -k 3 -t:|tail -10|tr 'a-z’‘A-Z’ >/tmp/maxusers.txt

 

5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。

[root@lanin ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:BF:26:20            inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0          TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:596390239 (568.7 MiB)  TX bytes:2886956 (2.7 MiB)lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          UP LOOPBACK RUNNING  MTU:16436  Metric:1          RX packets:68 errors:0 dropped:0 overruns:0 frame:0          TX packets:68 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:2856 (2.7 KiB)  TX bytes:2856 (2.7 KiB)
[root@lanin ~]# ifconfig|grep ‘inet addr’ |head -1|cut -d’ ‘ -f2|cut -d’:’ -f2
192.168.120.204


 

6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。

[root@lanin ~]# mkdir /tmp/etc
[root@lanin ~]# find /etc/*.conf|tr 'a-z' 'A-Z'|cut -d/ -f3 >> /tmp/etc/conf
[root@lanin ~]# more /tmp/etc/conf


 

7、显示/var目录下一级子目录或文件的总个数。

[root@lanin /]# ls /var |wc -l


8、取出/etc/group文件中第三个字段数值最小的10个组的名字。

[root@lanin /]# cat /etc/group|sort -n  -t: -k3|head -n 10
root:x:0:bin:x:1:daemon:x:2:sys:x:3:adm:x:4:tty:x:5:disk:x:6:lp:x:7:mem:x:8:kmem:x:9:
[root@lanin /]# cat /etc/group|sort -n  -t: -k3|head -n 10|cut -d: -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem


 

9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。

[root@lanin /]# cat /etc/fstab /etc/issue >>/tmp/etc/test


 

10、请总结描述用户和组管理类命令的使用方法并完成以下练习:

(1)、创建组distro,其GID为2016;

[root@lanin /]# groupadd distro -g 2016


 

(2)、创建用户mandriva, 其ID号为1005;基本组为distro;

[root@lanin /]# useradd mandriva -u 1005 -g distro


 

(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;

[root@lanin /]# useradd -mageia -u 1100  -d /home/linux


(4)、给用户mageia添加密码,密码为mageedu;

[root@lanin /]# echo ‘mageedu’|passwd --stdin mageia

(5)、删除mandriva,但保留其家目录;

[root@lanin /]# userdel mandriva


 

(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;

[root@lanin /]#groupadd peguin
[root@lanin /]# useradd slackware -u 2002 -g distro -G peguin


 

(7)、修改slackware的默认shell为/bin/tcsh;

[root@lanin /]#
usermod -s slackware /bin/tcsh


(8)、为用户slackware新增附加组admins;

[root@lanin /]#groupadd admins
[root@lanin /]#usermod slackware -a -G admins


(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;

[root@lanin /]#echo "654321"|passwd --stdin slackware && chage -W 3 -E 180 -I 3 slackware


 

(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;

[root@lanin /]# useradd openstack -u 3003 -g clouds -G peguin,nova


 

(11)、添加系统用户mysql,要求其shell为/sbin/nologin;

[root@lanin /]# useradd mysql -u 499 -s /sbin/nologin


 

(12)、使用echo命令,非交互式为openstack添加密码。

[root@lanin/]# echo "openstack"|passwd --stdin openstack