linux筛选数据的基本操作
-
列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@localhost script]# who chenjianhang tty1 2016-10-08 07:51 root tty2 2016-10-08 07:51 root pts/1 2016-10-08 02:58 (192.168.17.1) [root@localhost script]# who | grep -o "^[^[:space:]]*\>" | uniq chenjianhang root [root@localhost script]#
-
取出最后登录到当前系统的用户的相关信息。
[root@localhost ~]# last -1 root pts/1 192.168.17.1 Sat Oct 15 22:39 still logged in wtmp begins Thu Dec 3 15:53:02 2015 [root@localhost ~]#
-
取出当前系统上被用户当作其默认shell的最多的那个shell。
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | grep ".*sh" | uniq -c 1 /bin/bash 1 /sbin/shutdown 3 /bin/bash 1 /bin/tcsh 1 /bin/csh 7 /bin/bash [root@localhost ~]# cat /etc/passwd | cut -d: -f7 | grep ".*sh" | uniq -c | tail -1 7 /bin/bash [root@localhost ~]#
-
将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@localhost ~]# cat /etc/passwd | sort -nk 3 -t : | tail -10 | tr [a-z] [A-Z] >/tmp/maxusers.txt [root@localhost ~]# cat /tmp/maxusers.txt DOCKER:X:504:504::/HOME/DOCKER:/BIN/CSH OPENSTACK:X:3000:3000:COMMENT IMFOTMATION:/HOME/OPENSTACK:/BIN/BASH UBUNTE:X:3001:3001:COMMENT IMFOTMATION:/HOME/UBUNTE:/BIN/BASH MARIADB:X:3002:162::/HOME/MARIADB:/SBIN/NOLOGIN FEDORA:X:3003:3003::/USERS/FEDORA:/BIN/BASH WWW:X:3004:3004::/USERS/WWW:/BIN/BASH NEWUSERNAME:X:3005:3005::/HOME/NEWUSERNAME:/BIN/BASH ROOTKIT:X:3006:3006::/HOME/ROOTKIT:/BIN/BASH CHROOT:X:3007:3007::/BIN/CHROOT:/BIN/BASH NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN [root@localhost ~]#
-
取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:32:B0:C8 inet addr:192.168.17.128 Bcast:192.168.17.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe32:b0c8/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4079 errors:0 dropped:0 overruns:0 frame:0 TX packets:3539 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:449857 (439.3 KiB) TX bytes:520996 (508.7 KiB) Interrupt:67 Base address:0x2024 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:1565 errors:0 dropped:0 overruns:0 frame:0 TX packets:1565 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:3688919 (3.5 MiB) TX bytes:3688919 (3.5 MiB) [root@localhost ~]# ifconfig | grep "inet addr" | cut -d":" -f 2 | cut -d" " -f 1 192.168.17.128 127.0.0.1 [root@localhost ~]#
-
列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@localhost ~]# ls /etc/*.conf | cut -d"/" -f 3 | tr [a-z] [A-Z] > /tmp/etc.test [root@localhost ~]# cat /tmp/etc.test AUTOFS_LDAP_AUTH.CONF CDRECORD.CONF CONMAN.CONF DHCP6C.CONF DNSMASQ.CONF ESD.CONF GPM-ROOT.CONF GRUB.CONF GSSAPI_MECH.CONF HOST.CONF IDMAPD.CONF INITLOG.CONF JWHOIS.CONF KRB5.CONF LDAP.CONF LD.SO.CONF LFTP.CONF LIBAUDIT.CONF LIBUSER.CONF LOGROTATE.CONF LTRACE.CONF MKE2FS.CONF MODPROBE.CONF MTOOLS.CONF MULTIPATH.CONF NSCD.CONF NSSWITCH.CONF NTP.CONF ODDJOBD.CONF PAM_SMB.CONF PRELINK.CONF READER.CONF RESOLV.CONF SCROLLKEEPER.CONF SESTATUS.CONF SMARTD.CONF SYSCTL.CONF SYSLOG.CONF TPVMLP.CONF UPDATEDB.CONF WARNQUOTA.CONF YP.CONF YUM.CONF [root@localhost ~]#
-
显示/var目录下一级子目录或文件的总个数。
[root@localhost ~]# ll /var total 180 -rw-r--r-- 1 root root 0 Sep 25 18:38 1text2x -rw-r--r-- 1 root root 0 Sep 25 18:38 1text2x2 -rw-r--r-- 1 root root 0 Sep 25 18:38 1text2x-2 drwxr-xr-x 2 root root 4096 Dec 3 2015 account drwxr-xr-x 10 root root 4096 Dec 3 2015 cache drwxr-xr-x 2 root root 4096 Feb 23 2012 cvs drwxr-xr-x 3 root root 4096 Dec 3 2015 db drwxr-xr-x 3 root root 4096 Dec 3 2015 empty drwxr-xr-x 2 root root 4096 May 11 2011 games drwxrwx--T 2 root gdm 4096 Oct 15 22:24 gdm drwxr-xr-x 23 root root 4096 Dec 3 2015 lib drwxr-xr-x 2 root root 4096 May 11 2011 local drwxrwxr-x 6 root lock 4096 Oct 16 08:23 lock drwxr-xr-x 12 root root 4096 Oct 16 04:02 log lrwxrwxrwx 1 root root 10 Dec 3 2015 mail -> spool/mail drwxr-xr-x 2 root root 4096 May 11 2011 nis drwxr-xr-x 2 root root 4096 May 11 2011 opt -rw-r--r-- 1 root root 0 Sep 25 18:42 ppasswd drwxr-xr-x 2 root root 4096 May 11 2011 preserve drwxr-xr-x 2 root root 4096 Sep 11 2012 racoon drwxr-xr-x 18 root root 4096 Oct 16 07:25 run drwxr-xr-x 11 root root 4096 Dec 3 2015 spool drwxrwxrwt 3 root root 4096 Sep 22 08:03 tmp drwxr-xr-x 6 root root 4096 Dec 3 2015 www drwxr-xr-x 3 root root 4096 Dec 3 2015 yp [root@localhost ~]# ll /var | wc -l 26 [root@localhost ~]#
-
取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@localhost ~]# cat /etc/group | sort -n -t ":" -k 3 | head -10 | cut -d ":" -f 1 root bin daemon sys adm tty disk lp mem kmem [root@localhost ~]#
-
将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@localhost ~]# cat /etc/fstab /etc/issue > /tmp/etc.test [root@localhost ~]# cat /tmp/etc/test cat: /tmp/etc/test: No such file or directory [root@localhost ~]# cat /tmp/etc.test LABEL=/ / ext3 defaults 1 1 LABEL=/boot /boot ext3 defaults 1 2 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 LABEL=SWAP-sda3 swap swap defaults 0 0 CentOS release 5.11 (Final) Kernel \r on an \m [root@localhost ~]#
请总结描述用户和组管理类命令的使用方法并完成以下练习
-
创建组distro,其GID为2016;
[root@localhost ~]# groupadd -g 2016 distro
-
创建用户mandriva, 其ID号为1005;基本组为distro;
[root@localhost ~]# adduser -u 1005 -g distro mandriva
-
创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@localhost ~]# adduser -u 1100 -d /home/linux mageia
-
给用户mageia添加密码,密码为mageedu;
[root@localhost ~]# passwd mageia Changing password for user mageia. New UNIX password: BAD PASSWORD: it is based on a dictionary word Retype new UNIX password: passwd: all authentication tokens updated successfully. [root@localhost ~]#
-
删除mandriva,但保留其家目录;
[root@localhost ~]# userdel mandriva
-
创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@localhost ~]# useradd -u 2002 -g distro -G peguin
-
修改slackware的默认shell为/bin/tcsh;
[root@localhost ~]# usermod --shell /bash/tcsh slackware
-
为用户slackware新增附加组admins;
[root@localhost ~]# usermod -G admins slackware
-
为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@localhost ~]# passwd -n 3 -x 180 -w 3 slackware Adjusting aging data for user slackware. passwd: Success [root@localhost ~]#
-
添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[root@localhost ~]# useradd -u 3003 -g clouds -G peguin,nova openstack
-
添加系统用户mysql,要求其shell为/sbin/nologin;
[root@localhost ~]# adduser --shell /sbin/nologin mysql
-
使用echo命令,非交互式为openstack添加密码。
[root@localhost ~]# echo "openstack" | passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully. [root@localhost ~]#
原创文章,作者:Theo,如若转载,请注明出处:http://www.178linux.com/52310
评论列表(1条)
这套题基本涵盖了Linux系统中全部的用户管理情景,掌握的不错,继续加油。