1、 列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [ddw@localhost ~]$ who ddw tty1 2016-06-27 19:15 (:0) ddw pts/0 2016-06-27 19:15 (:0.0) ddw pts/1 2016-06-27 19:18 (:0.0) [ddw@localhost ~]$ who | cut -d ' ' -f 1 | sort -u ddw [ddw@localhost ~]$ 2、 取出最后登录到当前系统的用户的相关信息。 [root@localhost ~]# id $(who | tail -n 1 | cut -d ' ' -f 1 ) uid=500(ddw) gid=500(ddw) groups=500(ddw) [root@localhost ~]# 3、 取出当前系统上被用户当作其默认shell的最多的那个shell。 root@ddwhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c | sort -n | tail -1 22 /sbin/nologin [root@ddwhost ~]# 4、 将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。 [root@ddwhost tmp]# cat /etc/passwd | sort -t: -k 3 -n | tail -n 10 | tr 'a-z' 'A-Z' PULSE:X:498:496:PULSEAUDIO SYSTEM DAEMON:/VAR/RUN/PULSE:/SBIN/NOLOGIN RTKIT:X:499:497:REALTIMEKIT:/PROC:/SBIN/NOLOGIN DDW:X:500:500:REDHAT:/HOME/DDW:/BIN/BASH DD:X:501:501::/HOME/USER1:/BIN/BASH EE:X:502:503::/HOME/EE:/BIN/BASH USER:X:503:504::/HOME/NEWUSER:/BIN/BASH LVS:X:504:506::/HOME/LVS:/BIN/BASH LINUXSO:X:505:509::/HOME/LINUXSO:/BIN/BASH PYTHON:X:506:510::/HOME/PYTHON:/BIN/BASH DDDDD:X:507:511::/HOME/DDDDD:/BIN/BASH [root@ddwhost tmp]# cat /etc/passwd | sort -t: -k 3 -n | tail -n 10 | tr 'a-z' 'A-Z' > /tmp/maxusers.tx [root@ddwhost tmp]# cat /tmp/maxusers.tx PULSE:X:498:496:PULSEAUDIO SYSTEM DAEMON:/VAR/RUN/PULSE:/SBIN/NOLOGIN RTKIT:X:499:497:REALTIMEKIT:/PROC:/SBIN/NOLOGIN DDW:X:500:500:REDHAT:/HOME/DDW:/BIN/BASH DD:X:501:501::/HOME/USER1:/BIN/BASH EE:X:502:503::/HOME/EE:/BIN/BASH USER:X:503:504::/HOME/NEWUSER:/BIN/BASH LVS:X:504:506::/HOME/LVS:/BIN/BASH LINUXSO:X:505:509::/HOME/LINUXSO:/BIN/BASH PYTHON:X:506:510::/HOME/PYTHON:/BIN/BASH DDDDD:X:507:511::/HOME/DDDDD:/BIN/BASH [root@ddwhost tmp]# 5、 取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。 [root@ddwhost ~]# ifconfig | grep 'inet addr' | cut -d : -f2 | cut -d " " -f1 192.168.10.135 127.0.0.1 [root@ddwhost ~]# 6、 列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。 [root@ddwhost etc]# find /etc/*.conf | tr 'a-z' 'A-Z' > /tmp/etc.conf [root@ddwhost etc]# cat /tmp/etc.conf /ETC/ASOUND.CONF /ETC/DNSMASQ.CONF /ETC/DRACUT.CONF /ETC/FPRINTD.CONF /ETC/GAI.CONF /ETC/GRUB.CONF /ETC/HOST.CONF /ETC/KDUMP.CONF /ETC/KRB5.CONF /ETC/LD.SO.CONF /ETC/LIBAUDIT.CONF /ETC/LIBUSER.CONF /ETC/LOGROTATE.CONF /ETC/MKE2FS.CONF /ETC/MTOOLS.CONF /ETC/NSSWITCH.CONF /ETC/NTP.CONF /ETC/PBM2PPA.CONF /ETC/PM-UTILS-HD-APM-RESTORE.CONF /ETC/PNM2PPA.CONF /ETC/PRELINK.CONF /ETC/READAHEAD.CONF /ETC/RESOLV.CONF /ETC/RSYSLOG.CONF /ETC/SESTATUS.CONF /ETC/SMARTD.CONF /ETC/SOS.CONF /ETC/SUDO.CONF /ETC/SUDO-LDAP.CONF /ETC/SYSCTL.CONF /ETC/TPVMLP.CONF /ETC/UPDATEDB.CONF /ETC/WARNQUOTA.CONF /ETC/YUM.CONF [root@ddwhost etc]# 7、 显示/var目录下一级子目录或文件的总个数。 [root@ddwhost ~]# ls /var account cache crash db empty games gdm lib local lock log mail nis opt preserve run spool tmp www yp [root@ddwhost ~]# ls /var | wc -l 20 [root@ddwhost ~]# 8、 取出/etc/group文件中第三个字段数值最小的10个组的名字。 [root@ddwhost ~]# cat /etc/group | sort -t: -k3 -n | head -n 10 | cut -d: -f1 root bin daemon sys adm tty disk lp mem kmem [root@ddwhost ~]# 9、 将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。 [root@ddwhost ~]# [root@ddwhost ~]# cd /tmp/ [root@ddwhost tmp]# touch etc.test [root@ddwhost tmp]# cat /etc/issue CentOS release 6.6 (Final) Kernel \r on an \m [root@ddwhost ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Mon May 2 07:55:28 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 # UUID=e6891007-5edb-4ec9-b9af-2e0a319bcde5 / ext4 defaults 1 1 UUID=9a0659a2-531b-4758-a519-84aca48c1c33 /boot ext4 defaults 1 2 UUID=d7b150f4-946a-4703-a1ea-fb3cae927d0b 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 [root@ddwhost tmp]# cat /etc/fstab /etc/issue > /tmp/etc.test [root@ddwhost tmp]# cat /tmp/etc.test # # /etc/fstab # Created by anaconda on Mon May 2 07:55:28 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 # UUID=e6891007-5edb-4ec9-b9af-2e0a319bcde5 / ext4 defaults 1 1 UUID=9a0659a2-531b-4758-a519-84aca48c1c33 /boot ext4 defaults 1 2 UUID=d7b150f4-946a-4703-a1ea-fb3cae927d0b 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 [root@ddwhost tmp]# 10、 请总结描述用户和组管理类命令的使用方法并完成以下练习: (1)、创建组distro,其GID为2016; [root@ddwhost ~]# groupadd distro -g 2016 [root@ddwhost ~]# [root@ddwhost ~]# cat /etc/group | tail -1 distro:x:2016: [root@ddwhost ~]# (2)、创建用户mandriva, 其ID号为1005;基本组为distro; [root@ddwhost ~]# useradd -u 1005 -g 2016 mandriva [root@ddwhost ~]# id mandriva uid=1100(mandriva) gid=2016(distro) groups=2016(distro) [root@ddwhost ~]# (3)、创建用户mageia,其ID号为1100,家目录为/home/linux; [root@ddwhost ~]# useradd -u 1100 -d /home/linux mageia useradd: UID 1100 is not unique [root@ddwhost ~]# tail -n 1 /etc/passwd mageia:x:1100:1100::/home/linux:/bin/bash (4)、给用户mageia添加密码,密码为mageedu; [root@ddwhost ~]# 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. [root@ddwhost ~]# (5)、删除mandriva,但保留其家目录; [root@ddwhost ~]# userdel mageia (6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin; [root@ddwhost ~]# groupadd peguin [root@ddwhost ~]# useradd -u 2002 -g distro -G peguin slackware [root@ddwhost ~]# (7)、修改slackware的默认shell为/bin/tcsh; [root@ddwhost ~]# usermod -s /bin/tcsh slackware (8)、为用户slackware新增附加组admins; [root@ddwhost ~]# groupadd admins [root@ddwhost ~]# usermod -G admins slackware [root@ddwhost ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2018(admins) (9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天; [root@ddwhost ~]# passwd -n 3 -x 180 -w 3 slackware Adjusting aging data for user slackware. passwd: Success [root@ddwhost ~]# [root@ddwhost ~]# tail -1 /etc/shadow slackware:!!:16982:3:180:3::: [root@ddwhost ~]# (10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova; [root@ddwhost ~]# useradd -g clouds -G peguin,nova openstack [root@ddwhost ~]# id openstack uid=2003(openstack) gid=2019(clouds) groups=2019(clouds),2017(peguin),2020(nova) [root@ddwhost ~]# (11)、添加系统用户mysql,要求其shell为/sbin/nologin; [root@ddwhost ~]# useradd -r -s /sbin/nologin mysql [root@ddwhost ~]# tail -1 /etc/passwd mysql:x:496:332::/home/mysql:/sbin/nologin [root@ddwhost ~]# (12)、使用echo命令,非交互式为openstack添加密码。 [root@ddwhost ~]# echo 'openstackpwd' | passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully. [root@ddwhost ~]#
原创文章,作者:dengdw0917,如若转载,请注明出处:http://www.178linux.com/21211
评论列表(1条)
写的很好,排版可以在漂亮一点,加油