1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[jizo@jizo ~]$ who | cut -d" " -f1 | uniq
jizo
2、取出最后登录到当前系统的用户的相关信息。
[root@jizo ~]# who | tail -l
jizo :0 2016-12-18 15:00 (:0)
jizo tty2 2016-12-18 15:01
jizo pts/0 2016-12-18 15:14 (192.168.1.5)
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
[root@jizo ~]# cut -d: -f7 /etc/passwd | uniq -c | sort -n | tail -1
21 /sbin/nologin
4、将/etc/passwd中的第三个字段数值最大的后10个用户信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@jizo ~]# sort -t: -k3 -n /etc/passwd | tail -10 | tr 'a-z' 'A-Z' > /tmp/maxusers.txt
[root@jizo ~]# cat /tmp/maxusers.txt
COLORD:X:992:990:USER FOR COLORD:/VAR/LIB/COLORD:/SBIN/NOLOGIN
SASLAUTH:X:993:76:SASLAUTHD USER:/RUN/SASLAUTHD:/SBIN/NOLOGIN
UNBOUND:X:994:991:UNBOUND DNS RESOLVER:/ETC/UNBOUND:/SBIN/NOLOGIN
SSSD:X:995:993:USER FOR SSSD:/:/SBIN/NOLOGIN
POLKITD:X:996:994:USER FOR POLKITD:/:/SBIN/NOLOGIN
SYSTEMD-NETWORK:X:997:995:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN
SYSTEMD-BUS-PROXY:X:998:996:SYSTEMD BUS PROXY:/:/SBIN/NOLOGIN
ODS:X:999:998:SOFTHSM PRIVATE KEYS OWNER://VAR/LIB/SOFTHSM:/SBIN/NOLOGIN
JIZO:X:1000:1000:JIZO:/HOME/JIZO:/BIN/BASH
NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
5、取出当前主机的ip地址,提示:对ifconfig命令的结果进行切分。
[root@jizo ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp12s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:26:18:af:fd:31 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.3/24 brd 192.168.1.255 scope global dynamic enp12s0
valid_lft 11847sec preferred_lft 11847sec
inet6 fe80::226:18ff:feaf:fd31/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 52:54:00:6c:f9:31 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500
link/ether 52:54:00:6c:f9:31 brd ff:ff:ff:ff:ff:ff
[root@jizo ~]# ifconfig enp12s0 | grep -o "inet .*" | cut -d" " -f2
192.168.1.3
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@jizo ~]# ls /etc/*.conf | tr 'a-z' 'A-Z' > /tmp/etc.conf
[root@jizo ~]# cat /tmp/etc.conf
/ETC/ANT.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/EXTLINUX.CONF
/ETC/FPRINTD.CONF
/ETC/FUSE.CONF
/ETC/GEOIP.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/NAMED.CONF
/ETC/NFSMOUNT.CONF
/ETC/NSSWITCH.CONF
/ETC/NTP.CONF
/ETC/NUMAD.CONF
/ETC/ODDJOBD.CONF
/ETC/PBM2PPA.CONF
/ETC/PCP.CONF
/ETC/PEAR.CONF
/ETC/PNM2PPA.CONF
/ETC/RADVD.CONF
/ETC/REQUEST-KEY.CONF
/ETC/RESOLV.CONF
/ETC/RSYNCD.CONF
/ETC/RSYSLOG.CONF
/ETC/SESTATUS.CONF
/ETC/SLP.CONF
/ETC/SOFTHSM2.CONF
/ETC/SOS.CONF
/ETC/SUDO.CONF
/ETC/SUDO-LDAP.CONF
/ETC/SYSCTL.CONF
/ETC/TCSD.CONF
/ETC/TROLLTECH.CONF
/ETC/UPDATEDB.CONF
/ETC/USB_MODESWITCH.CONF
/ETC/VCONSOLE.CONF
/ETC/WVDIAL.CONF
/ETC/YUM.CONF
7、显示出/var目录下一级子目录或文件总个数。
[root@jizo ~]# ll -a /var/ | wc -l
29
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@jizo ~]# cat /etc/group | sort -t : -k3 -n -r | tail -10
kmem:x:9:
mem:x:8:
lp:x:7:
disk:x:6:
tty:x:5:
adm:x:4:
sys:x:3:
daemon:x:2:
bin:x:1:
root:x:0:
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@jizo ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
[root@jizo ~]# cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Sun Dec 18 13:24:07 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=e60127bf-5bec-4b52-82dd-fc0048ac2f67 /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
10、请总结描述用户和组管理类命令的使用方法并完成一下练习:
1)创建组distro,其GID为2016
[root@jizo ~]# groupadd -g 2016 distro
2)创建用户mandriva,其ID号为1005;基本组为distro
[root@jizo ~]# useradd -u 1005 -g distro mandreva
3)创建用户mageia,其ID号为1100,家目录为/home/linux
[root@jizo ~]# useradd -u 1100 -d /home/liunx mageia
4)给用户mageia添加密码,密码为mageedu
[root@jizo ~]# echo "mageedu" | passwd –stdin mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
5)删除mandriva,但保留其家目录
[root@jizo ~]# userdel mandriva
6)创建用户slackware,其ID号为2002,基本组为distro,附加组peguin
[root@jizo ~]# groupadd peguin
[root@jizo ~]# useradd -u 2002 -g distro -G peguin slackware
7)修改slackware的默认shell为/bin/tcsh
[root@jizo ~]# usermod -s /bin/tcsh slackware
[root@jizo ~]# cat /etc/passwd | grep slackware
slackware:x:2002:2016::/home/slackware:/bin/tcsh
8)为用户slackware新增附加组admins
[root@jizo ~]# groupadd admins
[root@jizo ~]# usermod -a -G admins slackware
9)为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天
[root@jizo ~]# passwd -n 3 -x 180 -w 3 slackware
Adjusting aging data for user slackware.
passwd: Success
[root@jizo ~]# cat /etc/shadow | grep slackware
slackware:!!:17153:3:180:3:::
10)添加用户openstack,其ID为3003,基本组为clouds,附加组为peguin和nova
[root@jizo ~]# useradd -u 3003 -g clouds -G peguin,nova openstack
useradd: group 'clouds' does not exist
[root@jizo ~]# groupadd clouds
[root@jizo ~]# useradd -u 3003 -g clouds -G peguin,nova openstack
useradd: group 'nova' does not exist
[root@jizo ~]# groupadd nova
[root@jizo ~]# useradd -u 3003 -g clouds -G peguin,nova openstack
11)添加系统用户mysql,要求其shell为/sbin/nologin
[root@jizo ~]# useradd -r -s /sbin/nologin mysql
12)使用echo命令,非交互式为openstack添加密码
[root@jizo ~]# echo "openstack" | passwd –stdin openstack
Changing password for user openstack.
passwd: all authentication tokens updated successfully.
原创文章,作者:jindizang,如若转载,请注明出处:http://www.178linux.com/64042