1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@localhost ~]# who root pts/0 2016-07-29 07:51 (192.168.0.15) root pts/1 2016-07-29 07:54 (192.168.0.15) centos pts/2 2016-07-29 07:55 (192.168.0.15) [root@localhost ~]# who | cut -d' ' -f1 | sort -u centos root
2、取出最后登录到当前系统的用户的相关信息。
[root@localhost ~]# who | tail -1 centos pts/2 2016-07-29 07:55 (192.168.0.15)
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
[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 operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:997:996::/var/lib/chrony:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin centos:x:1000:1000:centos:/home/centos:/bin/bash [root@localhost ~]# cut -d: -f7 /etc/passwd |sort | uniq -cd | tail -1 20 /sbin/nologin
4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@localhost ~]# sort -t: -k3 -n /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 operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin chrony:x:997:996::/var/lib/chrony:/sbin/nologin libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin centos:x:1000:1000:centos:/home/centos:/bin/bash [root@localhost ~]# sort -t: -k3 -n /etc/passwd | tail -n 10 | tr 'a-z' 'A-z' >/tmp/etc.conf [root@localhost ~]# cat /tmp/etc.conf SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGIN DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGIN NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN CHRONY:X:997:996::/VAR/LIB/CHRONY:/SBIN/NOLOGIN LIBSTORAGEMGMT:X:998:997:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/SBIN/NOLOGIN POLKITD:X:999:998:USER FOR POLKITD:/:/SBIN/NOLOGIN CENTOS:X:1000:1000:CENTOS:/HOME/CENTOS:/BIN/BASH [root@localhost ~]#
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[root@localhost ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.78 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::20c:29ff:fe7a:f237 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:7a:f2:37 txqueuelen 1000 (Ethernet) RX packets 25750 bytes 1924827 (1.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2063 bytes 238959 (233.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 139 bytes 12224 (11.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 139 bytes 12224 (11.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# ifconfig | head -n2 |tail -n1 | cut -d' ' -f10 192.168.0.78
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@localhost ~]# ls /etc/*.conf /etc/asound.conf /etc/krb5.conf /etc/nsswitch.conf /etc/sysctl.conf /etc/chrony.conf /etc/ld.so.conf /etc/resolv.conf /etc/tcsd.conf /etc/dnsmasq.conf /etc/libaudit.conf /etc/rsyncd.conf /etc/updatedb.conf /etc/dracut.conf /etc/libuser.conf /etc/rsyslog.conf /etc/usb_modeswitch.conf /etc/e2fsck.conf /etc/locale.conf /etc/sestatus.conf /etc/vconsole.conf /etc/fprintd.conf /etc/logrotate.conf /etc/sos.conf /etc/yum.conf /etc/host.conf /etc/man_db.conf /etc/sudo.conf /etc/kdump.conf /etc/mke2fs.conf /etc/sudo-ldap.conf [root@localhost ~]# ls /etc/*.conf | cut -d'/' -f3 | tr 'a-z' 'A-Z' >> /tmp/etc.conf [root@localhost ~]# cat /tmp/etc.conf SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGIN DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGIN NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN CHRONY:X:997:996::/VAR/LIB/CHRONY:/SBIN/NOLOGIN LIBSTORAGEMGMT:X:998:997:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/SBIN/NOLOGIN POLKITD:X:999:998:USER FOR POLKITD:/:/SBIN/NOLOGIN CENTOS:X:1000:1000:CENTOS:/HOME/CENTOS:/BIN/BASH ASOUND.CONF CHRONY.CONF DNSMASQ.CONF DRACUT.CONF E2FSCK.CONF FPRINTD.CONF HOST.CONF KDUMP.CONF KRB5.CONF LD.SO.CONF LIBAUDIT.CONF LIBUSER.CONF LOCALE.CONF LOGROTATE.CONF MAN_DB.CONF MKE2FS.CONF NSSWITCH.CONF RESOLV.CONF RSYNCD.CONF RSYSLOG.CONF SESTATUS.CONF SOS.CONF SUDO.CONF SUDO-LDAP.CONF SYSCTL.CONF TCSD.CONF UPDATEDB.CONF USB_MODESWITCH.CONF VCONSOLE.CONF YUM.CONF [root@localhost ~]#
7、显示/var目录下一级子目录或文件的总个数。
[root@localhost ~]# ls /var 123s account crash games lib log nis run tmp 12bn adm db gopher local mail opt sdf34ds.d var 4321331.d cache empty kerberos lock mwrf34sd preserve spool yp [root@localhost ~]# ls /var | wc -w 27
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@localhost ~]# sort -t':' -k3 -n /etc/group root:x:0: bin:x:1: daemon:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: lp:x:7: mem:x:8: kmem:x:9: wheel:x:10: cdrom:x:11: mail:x:12:postfix man:x:15: dialout:x:18: floppy:x:19: games:x:20: slocate:x:21: utmp:x:22: tape:x:30: utempter:x:35: ntp:x:38: video:x:39: dip:x:40: ftp:x:50: lock:x:54: tss:x:59: audio:x:63: tcpdump:x:72: sshd:x:74: dbus:x:81: postfix:x:89: postdrop:x:90: nobody:x:99: users:x:100: stapusr:x:156: stapsys:x:157: stapdev:x:158: avahi-autoipd:x:170: abrt:x:173: systemd-journal:x:190: chrony:x:996: libstoragemgmt:x:997: polkitd:x:998: ssh_keys:x:999: centos:x:1000:centos [root@localhost ~]# sort -t':' -k3 -n /etc/group | head -n 10 root:x:0: bin:x:1: daemon:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: lp:x:7: mem:x:8: kmem:x:9: [root@localhost ~]#
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@localhost ~]# cat /etc/{fstab,issue} # # /etc/fstab # Created by anaconda on Tue Jul 19 09:04:53 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/centos-root / xfs defaults 0 0 UUID=ea625619-a0c9-4173-9636-7edc34495127 /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 \S Kernel \r on an \m [root@localhost ~]# cat /etc/{fstab,issue} > /tmp/etc.test [root@localhost ~]# cat /tmp/etc.test # # /etc/fstab # Created by anaconda on Tue Jul 19 09:04:53 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/centos-root / xfs defaults 0 0 UUID=ea625619-a0c9-4173-9636-7edc34495127 /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 \S Kernel \r on an \m [root@localhost ~]#
10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
[root@localhost ~]# groupadd -g 2016 distro [root@localhost ~]# tail -1 /etc/group distro:x:2016: [root@localhost ~]#
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@localhost ~]# useradd -u 1005 -g distro mandriva [root@localhost ~]# id mandriva uid=1005(mandriva) gid=2016(distro) 组=2016(distro) [root@localhost ~]#
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@localhost ~]# useradd -u 1100 -d /home/linux mageia [root@localhost ~]# tail -1 /etc/passwd mageia:x:1100:1100::/home/linux:/bin/bash
(4)、给用户mageia添加密码,密码为mageedu;
[root@localhost ~]# echo "mageedu" | passwd --stdin mageia 更改用户 mageia 的密码 。 passwd:所有的身份验证令牌已经成功更新。 [root@localhost ~]# tail -1 /etc/shadow mageia:$6$u0zShA1C$I3QmD4888nubVFZOiXn5.3YyuPlHo6uwJhrJ4SzdONP9ylR5gfjrRVSmWZ5ggTUkdg3FbJuSABqlcuSaN3I78/:17011:0:99999:7::: [root@localhost ~]#
(5)、删除mandriva,但保留其家目录;
[root@localhost ~]# userdel mandriva | ls /home centos linux mandriva user1
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@localhost ~]# groupadd peguin [root@localhost ~]# useradd -u 2002 -g distro -G peguin slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) 组=2016(distro),2017(peguin) [root@localhost ~]#
(7)、修改slackware的默认shell为/bin/tcsh;
[root@localhost ~]# usermod -s /bin/tcsh slackware [root@localhost ~]# tail -1 /etc/passwd slackware:x:2002:2016::/home/slackware:/bin/tcsh [root@localhost ~]#
(8)、为用户slackware新增附加组admins;
[root@localhost ~]# groupadd admins [root@localhost ~]# usermod -a -G admins slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) 组=2016(distro),2017(peguin),2018(admins) [root@localhost ~]#
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@localhost ~]# echo "slackware1" | passwd --stdin slackware 更改用户 slackware 的密码 。 passwd:所有的身份验证令牌已经成功更新。 [root@localhost ~]# passwd -n3 -x180 -w3 slackware 调整用户密码老化数据slackware。 passwd: 操作成功 [root@localhost ~]# tail -1 /etc/shadow slackware:$6$o513PfmL$0BCpZ7aDNiOkD/0Nmh1tiYtqTmpsrxLgfQBuH71OkUSr2347r7sUcmLOe4C1/RBnymiJpCbqKuXu6gpfwr.VC0:17011:3:180:3:::
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[root@localhost ~]# useradd -u 3003 -g clouds -G peguin,nova openstack [root@localhost ~]# id openstack uid=3003(openstack) gid=2019(clouds) 组=2019(clouds),2017(peguin),2020(nova) [root@localhost ~]#
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[root@localhost ~]# useradd -s /sbin/nologin mysql [root@localhost ~]# tail -1 /etc/passwd mysql:x:2003:2003::/home/mysql:/sbin/nologin [root@localhost ~]#
(12)、使用echo命令,非交互式为openstack添加密码。
[root@localhost ~]# echo "openstack" | passwd --stdin openstack 更改用户 openstack 的密码 。 passwd:所有的身份验证令牌已经成功更新。 [root@localhost ~]# tail -1 /etc/shadow openstack:$6$NvniyGpF$lAM7m7UiOv7k5uwpAkOqqJ.BHkCSO9FffWP3Vx5jHV2Z6afZzl7YrXii1Eq1UyTGsPgpfY/q/wj57QOCPQPnE0:17012:0:99999:7::: [root@localhost ~]#
原创文章,作者:N21-3分之1,如若转载,请注明出处:http://www.178linux.com/26446
评论列表(1条)
写的很好,排版也很棒,加油