1.列出当前系统上所有已经登录的用户的用户名,注:同一个用户登录多次,则只显示一次即可。
who | cut -d ‘ ‘ -f1 | sort -u
[root@localhost ~]# who danry :0 2017-07-16 23:20 (:0) danry pts/0 2017-07-16 23:23 (192.168.1.2) root pts/1 2017-07-16 23:30 (192.168.1.2) [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# who | cut -d ' ' -f1 | sort -u danry root [root@localhost ~]#
2.列出最后登录到当前系统的用户的相关信息。
(1),last | head -1
[root@localhost ~]# last | head -1 root pts/3 192.168.1.2 Mon Jul 17 01:05 still logged in [root@localhost ~]# who danry :0 2017-07-16 23:20 (:0) danry pts/0 2017-07-16 23:23 (192.168.1.2) root pts/1 2017-07-16 23:30 (192.168.1.2) danry pts/2 2017-07-16 23:46 (192.168.1.2) root pts/3 2017-07-17 01:05 (192.168.1.2) [root@localhost ~]#
(2),id last | head -1 | cut -d ‘ ‘ -f1`
[root@localhost ~]# id `last | head -1 | cut -d ' ' -f1` uid=1000(danry) gid=1000(danry) groups=1000(danry),10(wheel) [root@localhost ~]# ^C [root@localhost ~]# who danry :0 2017-07-16 23:20 (:0) danry pts/0 2017-07-16 23:23 (192.168.1.2) root pts/1 2017-07-16 23:30 (192.168.1.2) danry pts/2 2017-07-16 23:46 (192.168.1.2) [root@localhost ~]#
3.列出当前系统上被用户当作其默认shell的最多的那个shell。
(1),echo $0
[root@localhost ~]# echo $0 -bash [root@localhost ~]#
(2),echo $SHELL
[root@localhost ~]# echo $SHELL /bin/bash [root@localhost ~]#
4.讲/etc/passwd中的第三个字段数值最大的10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
sort -t: -k3 -n -r /etc/passwd | head -10 | tr ‘a-z’ ‘A-Z’
[root@localhost ~]# sort -t: -k3 -n -r /etc/passwd | head -10 | tr 'a-z' 'A-Z' > /tmp/maxusers.txt [root@localhost ~]# [root@localhost ~]# cat /tmp/maxusers.txt NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN DANRY:X:1000:1000:DANRY:/HOME/DANRY:/BIN/BASH SYSTEMD-BUS-PROXY:X:999:998:SYSTEMD BUS PROXY:/:/SBIN/NOLOGIN SYSTEMD-NETWORK:X:998:997:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN POLKITD:X:997:996:USER FOR POLKITD:/:/SBIN/NOLOGIN UNBOUND:X:996:994:UNBOUND DNS RESOLVER:/ETC/UNBOUND:/SBIN/NOLOGIN COLORD:X:995:993:USER FOR COLORD:/VAR/LIB/COLORD:/SBIN/NOLOGIN GEOCLUE:X:994:991:USER FOR GEOCLUE:/VAR/LIB/GEOCLUE:/SBIN/NOLOGIN SASLAUTH:X:993:76:SASLAUTHD USER:/RUN/SASLAUTHD:/SBIN/NOLOGIN LIBSTORAGEMGMT:X:992:990:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/SBIN/NOLOGIN
5.取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
ifconfig | grep inet | grep -v 127.0.0.1 | head -1 | cut -d’ ‘ -f10
[root@localhost ~]# ifconfig | grep inet | grep -v 127.0.0.1 | head -1 | cut -d' ' -f10 192.168.1.1 [root@localhost ~]#
6.列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
ls /etc/*.conf | tr ‘a-z’ ‘A-Z’ > /tmp/etc.conf
[root@localhost ~]# ls /etc/*.conf | tr 'a-z' 'A-Z' > /tmp/etc.conf [root@localhost ~]# [root@localhost ~]# cat /tmp/etc.conf /ETC/ASOUND.CONF /ETC/AUTOFS.CONF /ETC/AUTOFS_LDAP_AUTH.CONF /ETC/BRLTTY.CONF /ETC/CGCONFIG.CONF /ETC/CGRULES.CONF /ETC/CGSNAPSHOT_BLACKLIST.CONF /ETC/CHRONY.CONF /ETC/DLEYNA-SERVER-SERVICE.CONF /ETC/DNSMASQ.CONF /ETC/DRACUT.CONF /ETC/E2FSCK.CONF /ETC/FPRINTD.CONF /ETC/FUSE.CONF /ETC/HBA.CONF /ETC/HOST.CONF /ETC/IDMAPD.CONF /ETC/IPSEC.CONF /ETC/KDUMP.CONF /ETC/KRB5.CONF /ETC/KSMTUNED.CONF /ETC/LD.SO.CONF /ETC/LIBAUDIT.CONF /ETC/LIBUSER.CONF /ETC/LOCALE.CONF /ETC/LOGROTATE.CONF /ETC/MAN_DB.CONF /ETC/MKE2FS.CONF /ETC/MTOOLS.CONF /ETC/NFSMOUNT.CONF /ETC/NSSWITCH.CONF /ETC/NTP.CONF /ETC/NUMAD.CONF /ETC/ODDJOBD.CONF /ETC/PBM2PPA.CONF /ETC/PNM2PPA.CONF /ETC/RADVD.CONF /ETC/REQUEST-KEY.CONF /ETC/RESOLV.CONF /ETC/RSYNCD.CONF /ETC/RSYSLOG.CONF /ETC/SESTATUS.CONF /ETC/SOS.CONF /ETC/SUDO.CONF /ETC/SUDO-LDAP.CONF /ETC/SYSCTL.CONF /ETC/TCSD.CONF /ETC/UPDATEDB.CONF /ETC/USB_MODESWITCH.CONF /ETC/VCONSOLE.CONF /ETC/WVDIAL.CONF /ETC/YUM.CONF [root@localhost ~]#
7.显示/var目录下一级子目录或文件的总个数。
ls /var/ | wc -l
[root@localhost ~]# ls /var/ | wc -l 22 [root@localhost ~]# [root@localhost ~]# ls /var/ account cache db games kerberos local log nis preserve spool tmp adm crash empty gopher lib lock mail opt run target yp [root@localhost ~]#
8.取出/etc/group 文件中的第三个字段最小的10个组的名字。
sort /etc/group -t ‘:’ -k3 -n | head -10
[root@localhost ~]# sort /etc/group -t ':' -k3 -n | head -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文件中。
(1),cat /etc/fstab > /tmp/etc.test , cat /etc/issue >> /tmp/etc.test
[root@localhost ~]# cat /etc/fstab > /tmp/etc.test [root@localhost ~]# cat /etc/issue >> /tmp/etc.test [root@localhost ~]# cat /tmp/etc.test # # /etc/fstab # Created by anaconda on Tue Jul 11 07:26:54 2017 # # 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=d6b40b2e-f1d4-44d7-9f7d-603a0a6e13b5 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 \S Kernel \r on an \m [root@localhost ~]#
(2),cat /etc/{fstab,issue} > /tmp/etc.test
[root@localhost ~]# rm -rf /tmp/etc.test [root@localhost ~]# cat /etc/{fstab,issue} > /tmp/etc.test [root@localhost ~]# cat /tmp/etc.test # # /etc/fstab # Created by anaconda on Tue Jul 11 07:26:54 2017 # # 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=d6b40b2e-f1d4-44d7-9f7d-603a0a6e13b5 /boot 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;
groupadd -g 2016 distro
[root@localhost ~]# groupadd -g 2016 distro [root@localhost ~]# tail -1 /etc/group distro:x:2016: [root@localhost ~]#
(2),创建用户mandriva,其中ID号为1005;基本组为distro;
useradd -g distro -u 1005 mandriva
[root@localhost ~]# useradd -g distro -u 1005 mandriva [root@localhost ~]# tail -1 /etc/passwd mandriva:x:1005:2016::/home/mandriva:/bin/bash [root@localhost ~]#
(3),创建用户mageia,其中ID号为1100,家目录为/home/linux;
useradd -u 1100 -1 /home/linux mageia
[root@localhost ~]# useradd -u 1100 -d /home/linux mageia [root@localhost ~]# tail -1 /etc/passwd mageia:x:1100:1100::/home/linux:/bin/bash [root@localhost ~]#
(4),给用户mageia添加密码,密码为mageedu;
echo mageedu | passwd –stdin mageia
[root@localhost ~]# echo mageedu | passwd --stdin mageia Changing password for user mageia. passwd: all authentication tokens updated successfully. [root@localhost ~]#
(5),删除mandriva,但保留其家目录;
userdel mandriva
[root@localhost ~]# userdel mandriva [root@localhost ~]# ls /home/ danry linux mandriva [root@localhost ~]# grep mandriva /etc/passwd [root@localhost ~]#
(6),创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
useradd -u 2002 -G distro,peguin slackware
[root@localhost ~]# useradd -u 2002 -G distro,peguin slackware [root@localhost ~]# tail -1 /etc/passwd slackware:x:2002:2002::/home/slackware:/bin/bash [root@localhost ~]# id slackware uid=2002(slackware) gid=2002(slackware) groups=2002(slackware),2016(distro),2017(peguin) [root@localhost ~]#
(7),修改slackware的默认shell为/bin/tosh;
usermod -s /bin/tcsh slackware
[root@localhost ~]# usermod -s /bin/tcsh slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2002(slackware) groups=2002(slackware),2016(distro),2017(peguin) [root@localhost ~]# grep slackware /etc/passwd slackware:x:2002:2002::/home/slackware:/bin/tcsh [root@localhost ~]#
(8),为用户slackware新增附加组admins;
[root@localhost ~]# usermod -G adminis slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2002(slackware) groups=2002(slackware),2018(adminis) [root@localhost ~]#
原创文章,作者:N27_Danry,如若转载,请注明出处:http://www.178linux.com/81613
评论列表(1条)
思路清晰,易懂,是一篇不错的文章,继续加油~