1. 列出当前系统上所有已经登陆的用户的用户名,注意:同一个用户登陆多次,则只显示一次即可。
`~]# who | grep -v “(unknown)” | cut -d’ ‘ -f1 | sort -u`
扩展:显示所有登陆用户,并显示每个用户的登陆次数
`~]# who | grep -v “(unknown)” | cut -d’ ‘ -f1 |sort -n | uniq -c`
2. 列出最后登录到当前系统的用户的相关信息
`~]# who | tail -1 | cut -d’ ‘ -f1 | id`
3. 取出当前系统上被用户当作其默认shell的最多的那个shell
`~]# cat /etc/passwd | cut -d: -f7 | sort -n | uniq -c | sort -t/ -k1 -nr | head -1 | grep -o “/.*”`
4. 将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
“`
~]# cat /etc/passwd | sort -t: -k3 -nr | head | tr [a-z] [A-Z] > /tmp/maxusers.txt
~]# cat /tmp/maxusers.txt
NOLOGIN:X:505:505::/HOME/NOLOGIN:/SBIN/NOLOGIN
BASHER:X:504:504::/HOME/BASHER:/BIN/BASH
TESTBASH:X:503:503::/HOME/TESTBASH:/BIN/BASH
BASH:X:502:502::/HOME/BASH:/BIN/BASH
USER1:X:501:501::/HOME/USER1:/BIN/BASH
CENTOS:X:500:500::/HOME/CENTOS:/BIN/BASH
RTKIT:X:499:497:REALTIMEKIT:/PROC:/SBIN/NOLOGIN
SASLAUTH:X:498:76:”SASLAUTHD USER”:/VAR/EMPTY/SASLAUTH:/SBIN/NOLOGIN
PULSE:X:497:496:PULSEAUDIO SYSTEM DAEMON:/VAR/RUN/PULSE:/SBIN/NOLOGIN
NGINX:X:496:493::/HOME/NGINX:/SBIN/NOLOGIN
“`
5. 取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
`~]# ifconfig eth0 | grep “inet addr” | cut -d: -f2 | cut -d’ ‘ -f1`
6. 列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
“`
~]# ls /etc/ | grep “\.conf$” | tr [a-z] [A-Z] > /tmp/etc.conf
~]# cat /tmp/etc.conf
ASOUND.CONF
DNSMASQ.CONF
DRACUT.CONF
FPRINTD.CONF
GAI.CONF
GRUB.CONF
HOST.CONF
KDUMP.CONF
KRB5.CONF
LD.SO.CONF
LIBAUDIT.CONF
LIBUSER.CONF
LOGROTATE.CONF
MKE2FS.CONF
MTOOLS.CONF
NSSWITCH.CONF
NTP.CONF
PM-UTILS-HD-APM-RESTORE.CONF
PRELINK.CONF
READAHEAD.CONF
RESOLV.CONF
RSYSLOG.CONF
SESTATUS.CONF
SMARTD.CONF
SOS.CONF
SUDO.CONF
SUDO-LDAP.CONF
SYSCTL.CONF
TROLLTECH.CONF
UPDATEDB.CONF
WARNQUOTA.CONF
YUM.CONF
“`
7. 显示/var目录下一级子目录或文件的总个数。
`~]# ls /var | wc -l`
8. 取出/etc/group文件中第三个字段数值最小的10个组的名字。
`~]# cat /etc/group | sort -t: -k3 -n | head | cut -d: -f1`
9. 将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
“`
~]# cat /etc/fstab /etc/issue > /tmp/etc.test
~]# cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Wed Jul 26 04:34:38 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/VolGroup-lv_root / ext4 defaults 1 1
UUID=1140ecc6-5420-4976-8fa2-09e18fc33fca /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.5 (Final)
Kernel \r on an \m
“`
10. 请总结描述用户和组管理命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
“`
~]# groupadd -g 2016 distro
~]# tail -1 /etc/group
distro:x:2016:
“`
(2)、创建用户mandriva,其ID号为1005,基本组为distro;
“`
~]# useradd -u 1005 -g distro mandriva
~]# id mandriva
uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
“`
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
“`
~]# useradd -u 1100 -d /home/linux mageia
~]# tail -1 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash
“`
(4)、给用户mageia添加密码,密码为mageedu;
“`
~]# echo “mageedu” | passwd –stdin mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
“`
(5)、删除mandriva,但保留其家目录;
“`
~]# userdel mandriva
~]# ls /home
bash basher centos linux lost+found mandriva nologin testbash user1
“`
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组为peguin;
“`
~]# useradd -u 2002 -g distro -G peguin slackware
~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
“`
(7)、修改slackware的默认shell为/bin/tcsh;
“`
~]# usermod -s /bin/tcsh slackware
~]# grep “^slackware” /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/tcsh
“`
(8)、为用户slackware新增附加组admins;
“`
~]# groupadd admins
~]# usermod -aG admins slackware
~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
“`
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/87466