1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
用who命令显示出所有的登录用户名
[root@localhost ~]# who cpl 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|uniq cpl root
[root@localhost ~]# who | cut -d" " -f1 | uniq cpl root
[root@localhost ~]# who|cut -d‘ ‘ -f1|sort -u cpl root
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/NOLOGIN NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN USBMUXD:X:113:113:USBMUXD USER:/:/SBIN/NOLOGIN AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN 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 CPL:X:500:500:CPL:/HOME/CPL:/BIN/BASH NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[root@localhost ~]# ifconfig eth0 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" " -f1 192.168.0.102 127.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 -w 21 #或者 [root@localhost ~]# ls /var | wc -l 21
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@localhost ~]# sort -t: -k3 -n /etc/group | head -10 | cut -d: -f1 root bin daemon sys adm tty disk lp mem kmem
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@localhost tmp]# cat /etc/fstab /etc/issue > /tmp/etc.test [root@localhost tmp]# ls etc.conf maxusers.txt pulse-U9em3Y4q4RUQ virtual-cpl.zEJpRG vmware-root etc.test orbit-cpl vgauthsvclog.txt.0 vmware-config0 yum.log keyring-X6CoM8 orbit-gdm virtual-cpl.MThlGH vmware-cpl keyring-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 1 UUID=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 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
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/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin cpl:x:500:500:cpl:/home/cpl:/bin/bash mandriva: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/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin cpl:x:500:500:cpl:/home/cpl:/bin/bash mandriva:x:1005:2016::/home/mandriva:/bin/bash mageia: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/group root:x:0: bin:x:1:bin,daemon daemon:x:2:bin,daemon sys:x:3:bin,adm adm:x:4:adm,daemon tty:x:5: disk:x:6: lp:x:7:daemon mem:x:8: kmem:x:9: wheel:x:10: mail:x:12:mail,postfix uucp: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:haldaemon ntp: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/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin cpl:x:500:500:cpl:/home/cpl:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash slackware: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/group root:x:0: bin:x:1:bin,daemon daemon:x:2:bin,daemon sys:x:3:bin,adm adm:x:4:adm,daemon tty:x:5: disk:x:6: lp:x:7:daemon mem:x:8: kmem:x:9: wheel:x:10: mail:x:12:mail,postfix uucp: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:haldaemon ntp: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 admins: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/group root:x:0: bin:x:1:bin,daemon daemon:x:2:bin,daemon sys:x:3:bin,adm adm:x:4:adm,daemon tty:x:5: disk:x:6: lp:x:7:daemon mem:x:8: kmem:x:9: wheel:x:10: mail:x:12:mail,postfix uucp: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:haldaemon ntp: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,openstack admins:x:2018:slackware clouds:x:2019: nova:x:2020:openstack
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[root@localhost ~]# useradd -s /sbin/nologin mysql [root@localhost ~]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin cpl:x:500:500:cpl:/home/cpl:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/slackware:/bin/tcsh openstack:x:3003:2019::/home/openstack:/bin/bash mysql:x:496:493::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[root@localhost ~]# echo ‘12345678‘ | passwd --stdin openstack 更改用户 openstack 的密码 。 passwd: 所有的身份验证令牌已经成功更新。
原文地址:http://goudan.blog.51cto.com/10227079/1840763