1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
代码语言:javascript复制[root@Client ~]# who
root pts/0 2016-08-07 00:40 (192.168.137.2)
user2 pts/1 2016-08-07 03:22 (192.168.137.2)
user1 pts/2 2016-08-07 03:21 (192.168.137.2)
user2 pts/3 2016-08-07 03:23 (192.168.137.2)
[root@Client ~]# who | cut -d" " -f1
root
user2
user1
user2
[root@Client ~]# who | cut -d" " -f1 | sort -u
root
user1
user2
[root@Client ~]#
2、取出最后登录到当前系统的用户的相关信息。
代码语言:javascript复制[root@Client ~]# who
root pts/0 2016-08-07 00:40 (192.168.137.2)
user2 pts/1 2016-08-07 03:22 (192.168.137.2)
user1 pts/2 2016-08-07 03:21 (192.168.137.2)
user2 pts/3 2016-08-07 03:23 (192.168.137.2)
user3 pts/4 2016-08-07 04:56 (192.168.137.2)
[root@Client ~]# who | sort -r | head -n 1 | cut -d" " -f1 | id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@Client ~]#
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
代码语言:javascript复制cut -d: -f7 /etc/passwd | uniq -c | sort -n | tail -n 1 | grep -E -o "/.*"
4、将/etc/passwd 中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
代码语言:javascript复制[root@Client tmp]# sort -n -k 3 -t":" /etc/passwd | tail -n 10 | tr [a-z] [A-Z] > /tmp/maxusers.txt
[root@Client tmp]# cat /tmp/maxusers.txt
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:499:REALTIMEKIT:/PROC:/SBIN/NOLOGIN
MU:X:500:500:MU:/HOME/MU:/BIN/BASH
USER1:X:501:501::/HOME/USER1:/BIN/BASH
USER2:X:502:502::/HOME/USER2:/BIN/BASH
USER3:X:503:503::/HOME/USER3:/BIN/BASH
DFF_GRT:X:504:504::/HOME/DFF_GRT:/BIN/BASH
NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
[root@Client tmp]#
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
代码语言:javascript复制[root@Client tmp]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:C6:4F:C7
inet addr:192.168.137.3 Bcast:192.168.137.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fec6:4fc7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:79309 errors:0 dropped:0 overruns:0 frame:0
TX packets:25674 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10809561 (10.3 MiB) TX bytes:3665156 (3.4 MiB)
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:65536 Metric:1
RX packets:7436 errors:0 dropped:0 overruns:0 frame:0
TX packets:7436 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:602496 (588.3 KiB) TX bytes:602496 (588.3 KiB)
[root@Client tmp]# ifconfig | grep -E "<inet>" | cut -d":" -f2 | cut -d" " -f1
192.168.137.3
127.0.0.1
[root@Client tmp]#
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
代码语言:javascript复制[user2@Client ~]$ ls /etc/ | grep -E ".conf$" | tr [a-z] [A-Z] > /tmp/etc.conf
[user2@Client ~]$ cat /tmp/etc.conf
ANTHY-CONF
ASOUND.CONF
AUTOFS.CONF
AUTOFS_LDAP_AUTH.CONF
CAS.CONF
DNSMASQ.CONF
DRACUT.CONF
FPRINTD.CONF
GAI.CONF
GCONF
GRUB.CONF
GSSAPI_MECH.CONF
HOST.CONF
IDMAPD.CONF
INIT.CONF
KDUMP-ADV-CONF
KDUMP.CONF
KRB5.CONF
LATRACE.CONF
LD.SO.CONF
LIBAUDIT.CONF
LIBUSER.CONF
LOGROTATE.CONF
LTRACE.CONF
MKE2FS.CONF
MTOOLS.CONF
NFSMOUNT.CONF
NSSWITCH.CONF
NTP.CONF
ODDJOBD.CONF
PBM2PPA.CONF
PM-UTILS-HD-APM-RESTORE.CONF
PNM2PPA.CONF
PRELINK.CONF
READAHEAD.CONF
REQUEST-KEY.CONF
RESOLV.CONF
RSYSLOG.CONF
SESTATUS.CONF
SMARTD.CONF
SOS.CONF
SUDO.CONF
SUDO-LDAP.CONF
SYSCTL.CONF
TROLLTECH.CONF
UPDATEDB.CONF
WARNQUOTA.CONF
YP.CONF
YUM.CONF
[user2@Client ~]$
7、显示/var目录下一级子目录或文件的总个数。
代码语言:javascript复制[user3@Client var]$#/var一级子目录或文件总个数:
[user3@Client var]$ ls -l | grep "^[d|-]"
drwxr-xr-x. 2 root root 4096 Jul 31 19:12 account
drwxr-xr-x. 15 root root 4096 Jul 31 19:23 cache
drwxr-xr-x. 2 root root 4096 May 11 07:55 crash
drwxr-xr-x. 2 root root 4096 Nov 22 2013 cvs
drwxr-xr-x. 3 root root 4096 Jul 31 19:12 db
drwxr-xr-x. 3 root root 4096 Jul 31 19:11 empty
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxrwx--T. 2 root gdm 4096 Oct 16 2014 gdm
drwxr-xr-x. 38 root root 4096 Aug 7 03:35 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 local
drwxrwxr-x. 5 root lock 4096 Aug 7 03:35 lock
drwxr-xr-x. 13 root root 4096 Aug 7 03:35 log
drwxr-xr-x. 2 root root 4096 Sep 23 2011 nis
drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt
drwxr-xr-x. 2 root root 4096 Sep 23 2011 preserve
drwxr-xr-x. 28 root root 4096 Aug 5 04:25 run
drwxr-xr-x. 13 root root 4096 Jul 31 19:07 spool
drwxrwxrwt. 2 root root 4096 Jul 31 19:16 tmp
drwxr-xr-x. 6 root root 4096 Jul 31 19:06 www
drwxr-xr-x. 3 root root 4096 Jul 31 19:06 yp
[user3@Client var]$ ls -l | grep "^[d|-]" | wc -l
20
[user3@Client var]$
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
代码语言:javascript复制[user2@Client ~]$ sort -n -t ":" -k 3 /etc/group | head -n 10 | cut -d: -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
[user2@Client ~]$
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
代码语言:javascript复制[user2@Client ~]$ cat /etc/fstab /etc/issue >> /tmp/etc.test
[user2@Client ~]$ cat /tmp/etc.test | wc -l
18
[user2@Client ~]$ cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Sun Jul 31 18:58:06 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_client-lv_root / ext4 defaults 1 1
UUID=769bb69c-b8ce-4ce5-80e0-7f0a273536d6 /boot ext4 defaults 1 2
/dev/mapper/vg_client-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.8 (Final)
Kernel r on an m
[user2@Client ~]$
10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
代码语言:javascript复制[root@Client ~]# groupadd distro -g 2016
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
代码语言:javascript复制[root@Client ~]# useradd mandriva -u 1005 -g distro
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
代码语言:javascript复制[root@Client ~]# mkdir -p /home/linux
[root@Client ~]# useradd mageia -u 1100 -d /home/linux/
(4)、给用户mageia添加密码,密码为mageedu;
代码语言:javascript复制[root@Client ~]# echo "mageedu" | passwd --stdin mageia
(5)、删除mandriva,但保留其家目录;
代码语言:javascript复制[root@Client ~]# userdel mandriva
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
代码语言:javascript复制[root@Client ~]# useradd slackware -u 2002 -g distro
[root@Client ~]# groupadd peguin
[root@Client ~]# usermod slackware -aG peguin
[root@Client ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
(7)、修改slackware的默认shell为/bin/tcsh;
代码语言:javascript复制[root@Client ~]# usermod -s /bin/tcsh slackware
[root@Client ~]# cat /etc/passwd | tail -n 1
slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins;
代码语言:javascript复制[root@Client ~]# groupadd admins
[root@Client ~]# usermod slackware -aG admins
[root@Client ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
代码语言:javascript复制[root@Client ~]# echo "mageedu" | passwd --stdin slackware
Changing password for user slackware.
passwd: all authentication tokens updated successfully.
[root@Client ~]# chage -m 3 -M 180 -W 3 slackware
[root@Client ~]# cat /etc/shadow | tail -n 1
slackware:$6$bdbYcoWR$3UAiElPQWmATJsw.I7x6Lx9OdJwIlIadeKOY.BLRinErFRAxaJZqB.fhHaacPs7Oi.n6S/tBclVk9yq9L51cp.:17020:3:180:3:::
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
代码语言:javascript复制[root@Client ~]# groupadd clouds
[root@Client ~]# groupadd nova
[root@Client ~]# useradd openstack -u 3003 -g clouds
[root@Client ~]# usermod -aG peguin,nova openstack
[root@Client ~]# id openstack
uid=3003(openstack) gid=2019(clouds) groups=2019(clouds),2017(peguin),2020(nova)
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
代码语言:javascript复制[root@Client ~]# useradd mysql
[root@Client ~]# usermod -s /sbin/nologin mysql
[root@Client ~]# cat /etc/passwd | tail -n 1
mysql:x:3004:3004::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
代码语言:javascript复制[root@Client ~]# echo "newpasswd" | passwd --stdin openstack
Changing password for user openstack.
passwd: all authentication tokens updated successfully.