本周作业内容:
1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
#who | tr -s [[:space:]] | cut -d" " -f1 | sort -u
[root@hiallServer bin]# who | tr -s [[:space:]] | cut -d" " -f1 | sort -u
kzxjwb
root
2、取出最后登录到当前系统的用户的相关信息。
#last | head -1
[root@hiallServer bin]# last | head -1
root pts/4 223.72.80.48 Mon Oct 24 23:07 still logged in
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
#cat /etc/passwd | cut -d":" -f7 | uniq -c | sort -n | tail -1 | tr -s [[:space:]] | cut -d" " -f3
[root@hiallServer webapps]# cat /etc/passwd | cut -d":" -f7 | uniq -c | sort -n | tail -1 | tr -s [[:space:]] | cut -d" " -f3
/sbin/nologin
4、将/etc/passw d 中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@hiall2016 /]# cat /etc/passwd | sort -t":" -nk3 | tail -10 | tr [a-z] [A-Z] > /tmp/maxusers.txt
[root@hiall2016 /]# cat /tmp/maxusers.txt
USER2:X:1024:1026::/HOME/USER2:/BIN/BASH
USER3:X:1025:1027::/HOME/USER3:/BIN/BASH
USER4:X:1026:1028::/HOME/USER4:/BIN/BASH
USER5:X:1027:1029::/HOME/USER5:/BIN/BASH
USER6:X:1028:1030::/HOME/USER6:/BIN/BASH
USER7:X:1029:1031::/HOME/USER7:/BIN/BASH
USER8:X:1030:1032::/HOME/USER8:/BIN/BASH
USER9:X:1031:1033::/HOME/USER9:/BIN/BASH
USER10:X:1032:1034::/HOME/USER10:/BIN/BASH
USER11:X:1033:1035::/HOME/USER11:/BIN/BASH
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
#ifconfig | grep -E "inet addr" | cut -d":" -f2 | cut -d" " -f1
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@hiall2016 /]# ls /etc/*.conf | tr [a-z] [A-Z] > /tmp/etc.conf
[root@hiall2016 /]# cat /tmp/etc.conf
/ETC/ASOUND.CONF
/ETC/DNSMASQ.CONF
/ETC/DRACUT.CONF
/ETC/E2FSCK.CONF
/ETC/HOST.CONF
/ETC/KDUMP.CONF
/ETC/KRB5.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/NSSWITCH.CONF
/ETC/NTP.CONF
/ETC/RESOLV.CONF
/ETC/RSYSLOG.CONF
/ETC/SESTATUS.CONF
/ETC/SUDO.CONF
/ETC/SUDO-LDAP.CONF
/ETC/SYSCTL.CONF
/ETC/TCSD.CONF
/ETC/VCONSOLE.CONF
/ETC/YUM.CONF
7、显示/var目录下一级子目录或文件的总个数。
[root@hiall2016 /]# ll /var | wc -l
22
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@hiall2016 /]# cat /etc/group | sort -t":" -nk3 | head -10 | cut -d":" -f1
root
bin
daemon
sys
adm
tty
disk
lp
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@hiall2016 /]# cat /etc/fstab /etc/issue > /tmp/etc.test
[root@hiall2016 /]# cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Fri Mar 11 01:59:10 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
#
UUID=e7edf137-5d28-407f-98b9-804ead7732b2 / xfs defaults 0 0
UUID=6d3d5767-9a65-4f80-ae84-87d2c324edbd /boot xfs defaults 0 0
UUID=63a207f4-e7ae-4b27-87c5-f56054d49206 /data xfs defaults 0 0
UUID=d0e6dd42-8f9c-4fba-b450-d068475fbb14 /tmp xfs defaults 0 0
UUID=9015aa16-40bd-46db-8c6b-b019513a8111 /usr xfs defaults 0 0
UUID=dcbba68b-953b-4cd5-9b92-88801ef89c0a swap swap defaults 0 0
\S
Kernel \r on an \m
10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
[root@hiall2016 ~]# groupadd -g 2016 distro
[root@hiall2016 ~]# cat /etc/group |tail -1
distro:x:2016:
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@hiall2016 ~]# useradd -u 1005 -g distro mandriva
useradd: UID 1005 is not unique
[root@hiall2016 ~]# useradd -u 1105 -g distro mandriva
[root@hiall2016 ~]# id mandriva
uid=1105(mandriva) gid=2016(distro) groups=2016(distro)
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@hiall2016 ~]# useradd -u 1100 -d /home/linux mageia
[root@hiall2016 ~]# cat /etc/passwd | tail -1
mageia:x:1100:1100::/home/linux:/bin/bash
(4)、给用户mageia添加密码,密码为mageedu;
[root@hiall2016 ~]# echo "mageedu" | passwd �Cstdin mageia
passwd: Only one user name may be specified.
(5)、删除mandriva,但保留其家目录;
[root@hiall2016 ~]# cat /etc/passwd | grep mandriva
mandriva:x:1105:2016::/home/mandriva:/bin/bash
[root@hiall2016 ~]# userdel mandriva
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@hiall2016 ~]# groupadd peguin
[root@hiall2016 ~]# useradd -u 2002 -g distro -G peguin slackware
[root@hiall2016 ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
(7)、修改slackware的默认shell为/bin/tcsh;
[root@hiall2016 ~]# cat /etc//passwd | tail -1
slackware:x:2002:2016::/home/slackware:/bin/bash
[root@hiall2016 ~]# usermod -s /bin/tcsh slackware
[root@hiall2016 ~]# cat /etc/passwd | tail -1
slackware:x:2002:2016::/home/slackware:/bin/tcsh
[root@hiall2016 ~]#
(8)、为用户slackware新增附加组admins;
[root@hiall2016 ~]# groupadd admins
[root@hiall2016 ~]# usermod -aG admins slackware
[root@hiall2016 ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@hiall2016 ~]# echo slackware | passwd �Cstdin -n3 -x180 -w3 slackware
passwd: Only one user name may be specified.
[root@hiall2016 ~]# cat /etc/shadow | tail -1
slackware:!!:17098:0:99999:7:::
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
useradd -u 3003 -g clouds -G peguin openstack
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
#useradd -r -s /sbin/nologin mysql
(12)、使用echo命令,非交互式为openstack添加密码。
echo openstack | passwd �Cstdin openstack &> /dev/null
本文出自 “51geeks” 博客,请务必保留此出处http://geeks.blog.51cto.com/9099360/1865243