1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
用who命令显示出所有的登录用户名
[root@localhost ~]# whocpl tty1 2016-08-22 09:28 (:0)root pts/0 2016-08-22 09:28 (192.168.0.108)
再用以下命令去重
[root@localhost ~]# who | cut -d' ' -f1|uniqcplroot
[root@localhost ~]# who | cut -d" " -f1 | uniqcplroot
[root@localhost ~]# who|cut -d' ' -f1|sort -ucplroot
2、取出最后登录到当前系统的用户的相关信息。
[root@localhost ~]# 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
[root@localhost ~]# id $(who |tail -n 1|cut -d' ' -f1)uid=0(root) gid=0(root) 组=0(root)
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
取出当前所有使用的shell
[root@localhost ~]# cut -d":" -f7 /etc/passwd | uniq -c | sort -n 1 /bin/bash 1 /bin/bash 1 /bin/sync 1 /sbin/halt 1 /sbin/shutdown 4 /sbin/nologin 25 /sbin/nologin#可取消 :左右的双引号或者替换为单引号
在取出默认
[root@localhost ~]# cut -d: -f7 /etc/passwd | uniq -c | sort -n | tail -1 25 /sbin/nologin #或者[root@localhost ~]# cut -d: -f7 /etc/passwd | uniq -c | sort -rn | head -n 1 25 /sbin/nologin
4、将/etc/passw d 中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@localhost ~]# cat /etc/passwd|sort -n -k 3 -t:|tail -10|tr ‘a-z’ ‘A-Z’ >/tmp/maxusers.txt[root@localhost ~]# cat maxusers.txt POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGINNOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGINUSBMUXD:X:113:113:USBMUXD USER:/:/SBIN/NOLOGINAVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGINABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGINPULSE:X:497:496:PULSEAUDIO SYSTEM DAEMON:/VAR/RUN/PULSE:/SBIN/NOLOGINSASLAUTH:X:498:76:SASLAUTHD USER:/VAR/EMPTY/SASLAUTH:/SBIN/NOLOGINRTKIT:X:499:499:REALTIMEKIT:/PROC:/SBIN/NOLOGINCPL:X:500:500:CPL:/HOME/CPL:/BIN/BASHNFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[root@localhost ~]# ifconfigeth0 Link encap:Ethernet HWaddr 00:0C:29:3D:9A:46 inet addr:192.168.0.102 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe3d:9a46/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:15603 errors:0 dropped:0 overruns:0 frame:0 TX packets:4427 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:21733117 (20.7 MiB) TX bytes:338923 (330.9 KiB)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:28 errors:0 dropped:0 overruns:0 frame:0 TX packets:28 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2048 (2.0 KiB) TX bytes:2048 (2.0 KiB)[root@localhost ~]# ifconfig | grep "inet addr" | cut -d: -f2| cut -d" " -f1192.168.0.102127.0.0.1
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@localhost ~]# ls /etc/*.conf | tr a-z A-Z >/tmp/etc.conf[root@localhost ~]# 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/ETC/INIT.CONF/ETC/KDUMP.CONF
7、显示/var目录下一级子目录或文件的总个数。
[root@localhost ~]# ls /var | wc -w21#或者[root@localhost ~]# ls /var | wc -l21
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@localhost ~]# sort -t: -k3 -n /etc/group | head -10 | cut -d: -f1rootbindaemonsysadmttydisklpmemkmem
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@localhost tmp]# cat /etc/fstab /etc/issue > /tmp/etc.test[root@localhost tmp]# lsetc.conf maxusers.txt pulse-U9em3Y4q4RUQ virtual-cpl.zEJpRG vmware-rootetc.test orbit-cpl vgauthsvclog.txt.0 vmware-config0 yum.logkeyring-X6CoM8 orbit-gdm virtual-cpl.MThlGH vmware-cplkeyring-xPxmHy pulse-HwnH1KzJuelY virtual-cpl.oh8ean VMwareDnD[root@localhost tmp]# cat etc.test ## /etc/fstab# Created by anaconda on Tue Aug 16 05:26:36 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/VolGroup-lv_root / ext4 defaults 1 1UUID=5d6bc760-8aa8-4209-a5ab-2d8fe3da2e27 /boot ext4 defaults 1 2/dev/mapper/VolGroup-lv_home /home ext4 defaults 1 2/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0tmpfs /dev/shm tmpfs defaults 0 0devpts /dev/pts devpts gid=5,mode=620 0 0sysfs /sys sysfs defaults 0 0proc /proc proc defaults 0 0CentOS release 6.8 (Final)Kernel \r on an \m
10、请总结描述用户和组管理类命令的使用方法并完成以下练习: (1)、创建组distro,其GID为2016;
[root@localhost ~]# groupadd -g 2016 distro[root@localhost ~]# cat /etc/group#前面省stapusr:x:156:stapsys:x:157:stapdev:x:158:tcpdump:x:72:cpl:x:500:distro:x:2016:
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@localhost ~]# useradd -u 1005 -g distro mandiriva[root@localhost ~]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologinrtkit:x:499:499:RealtimeKit:/proc:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinhaldaemon:x:68:68:HAL daemon:/:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologinapache:x:48:48:Apache:/var/www:/sbin/nologinsaslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologingdm:x:42:42::/var/lib/gdm:/sbin/nologinpulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologincpl:x:500:500:cpl:/home/cpl:/bin/bashmandriva:x:1005:2016::/home/mandriva:/bin/bash
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@localhost ~]# useradd -u 1100 -m -d /home/linux mageia[root@localhost ~]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologinrtkit:x:499:499:RealtimeKit:/proc:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinhaldaemon:x:68:68:HAL daemon:/:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologinapache:x:48:48:Apache:/var/www:/sbin/nologinsaslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologingdm:x:42:42::/var/lib/gdm:/sbin/nologinpulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologincpl:x:500:500:cpl:/home/cpl:/bin/bashmandriva:x:1005:2016::/home/mandriva:/bin/bashmageia:x:1100:1100::/home/linux:/bin/bash
(4)、给用户mageia添加密码,密码为mageedu;
[root@localhost ~]# echo mageedu | passwd --stdin mageia更改用户 mageia 的密码 。passwd: 所有的身份验证令牌已经成功更新。
(5)、删除mandriva,但保留其家目录;
[root@localhost ~]# userdel mandriva
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@localhost ~]# groupadd peguin[root@localhost ~]# useradd -u 2002 -g 2016 -G peguin slackware [root@localhost ~]# cat /etc/grouproot:x:0:bin:x:1:bin,daemondaemon:x:2:bin,daemonsys:x:3:bin,admadm:x:4:adm,daemontty:x:5:disk:x:6:lp:x:7:daemonmem:x:8:kmem:x:9:wheel:x:10:mail:x:12:mail,postfixuucp:x:14:man:x:15:games:x:20:gopher:x:30:video:x:39:dip:x:40:ftp:x:50:lock:x:54:audio:x:63:nobody:x:99:users:x:100:dbus:x:81:usbmuxd:x:113:rpc:x:32:utmp:x:22:utempter:x:35:rtkit:x:499:avahi-autoipd:x:170:desktop_admin_r:x:498:desktop_user_r:x:497:floppy:x:19:vcsa:x:69:abrt:x:173:cdrom:x:11:tape:x:33:dialout:x:18:wbpriv:x:88:rpcuser:x:29:nfsnobody:x:65534:haldaemon:x:68:haldaemonntp:x:38:apache:x:48:saslauth:x:76:postdrop:x:90:postfix:x:89:gdm:x:42:pulse:x:496:pulse-access:x:495:fuse:x:494:sshd:x:74:slocate:x:21:stapusr:x:156:stapsys:x:157:stapdev:x:158:tcpdump:x:72:cpl:x:500:distro:x:2016:mageia:x:1100:peguin:x:2017:slackware
(7)、修改slackware的默认shell为/bin/tcsh;
[root@localhost ~]# usermod -s /bin/tcsh slackware[root@localhost ~]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologinrtkit:x:499:499:RealtimeKit:/proc:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinhaldaemon:x:68:68:HAL daemon:/:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologinapache:x:48:48:Apache:/var/www:/sbin/nologinsaslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologingdm:x:42:42::/var/lib/gdm:/sbin/nologinpulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologincpl:x:500:500:cpl:/home/cpl:/bin/bashmageia:x:1100:1100::/home/linux:/bin/bashslackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins;
[root@localhost ~]# groupadd admins[root@localhost ~]# usermod -a -G admins slackware[root@localhost ~]# cat /etc/grouproot:x:0:bin:x:1:bin,daemondaemon:x:2:bin,daemonsys:x:3:bin,admadm:x:4:adm,daemontty:x:5:disk:x:6:lp:x:7:daemonmem:x:8:kmem:x:9:wheel:x:10:mail:x:12:mail,postfixuucp:x:14:man:x:15:games:x:20:gopher:x:30:video:x:39:dip:x:40:ftp:x:50:lock:x:54:audio:x:63:nobody:x:99:users:x:100:dbus:x:81:usbmuxd:x:113:rpc:x:32:utmp:x:22:utempter:x:35:rtkit:x:499:avahi-autoipd:x:170:desktop_admin_r:x:498:desktop_user_r:x:497:floppy:x:19:vcsa:x:69:abrt:x:173:cdrom:x:11:tape:x:33:dialout:x:18:wbpriv:x:88:rpcuser:x:29:nfsnobody:x:65534:haldaemon:x:68:haldaemonntp:x:38:apache:x:48:saslauth:x:76:postdrop:x:90:postfix:x:89:gdm:x:42:pulse:x:496:pulse-access:x:495:fuse:x:494:sshd:x:74:slocate:x:21:stapusr:x:156:stapsys:x:157:stapdev:x:158:tcpdump:x:72:cpl:x:500:distro:x:2016:mageia:x:1100:peguin:x:2017:slackwareadmins:x:2018:slackware
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@localhost ~]# passwd -n 3 -x 180 -w 3 slackware调整用户密码老化数据slackware。passwd: 操作成功[root@localhost ~]# echo 123456 | passwd --stdin slackware更改用户 slackware 的密码 。passwd: 所有的身份验证令牌已经成功更新。
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[root@localhost ~]# groupadd clouds[root@localhost ~]# groupadd nova[root@localhost ~]# useradd -u 3003 -g clouds -G peguin,nova openstack[root@localhost ~]# cat /etc/grouproot:x:0:bin:x:1:bin,daemondaemon:x:2:bin,daemonsys:x:3:bin,admadm:x:4:adm,daemontty:x:5:disk:x:6:lp:x:7:daemonmem:x:8:kmem:x:9:wheel:x:10:mail:x:12:mail,postfixuucp:x:14:man:x:15:games:x:20:gopher:x:30:video:x:39:dip:x:40:ftp:x:50:lock:x:54:audio:x:63:nobody:x:99:users:x:100:dbus:x:81:usbmuxd:x:113:rpc:x:32:utmp:x:22:utempter:x:35:rtkit:x:499:avahi-autoipd:x:170:desktop_admin_r:x:498:desktop_user_r:x:497:floppy:x:19:vcsa:x:69:abrt:x:173:cdrom:x:11:tape:x:33:dialout:x:18:wbpriv:x:88:rpcuser:x:29:nfsnobody:x:65534:haldaemon:x:68:haldaemonntp:x:38:apache:x:48:saslauth:x:76:postdrop:x:90:postfix:x:89:gdm:x:42:pulse:x:496:pulse-access:x:495:fuse:x:494:sshd:x:74:slocate:x:21:stapusr:x:156:stapsys:x:157:stapdev:x:158:tcpdump:x:72:cpl:x:500:distro:x:2016:mageia:x:1100:peguin:x:2017:slackware,openstackadmins:x:2018:slackwareclouds:x:2019:nova:x:2020:openstack
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[root@localhost ~]# useradd -s /sbin/nologin mysql[root@localhost ~]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologinrtkit:x:499:499:RealtimeKit:/proc:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinhaldaemon:x:68:68:HAL daemon:/:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologinapache:x:48:48:Apache:/var/www:/sbin/nologinsaslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologingdm:x:42:42::/var/lib/gdm:/sbin/nologinpulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologincpl:x:500:500:cpl:/home/cpl:/bin/bashmageia:x:1100:1100::/home/linux:/bin/bashslackware:x:2002:2016::/home/slackware:/bin/tcshopenstack:x:3003:2019::/home/openstack:/bin/bashmysql:x:496:493::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[root@localhost ~]# echo '12345678' | passwd --stdin openstack更改用户 openstack 的密码 。passwd: 所有的身份验证令牌已经成功更新。