您的位置:首页 > 其它

第三周作业

2016-08-20 17:17 218 查看
本周作业内容:
1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@localhost ~]# who | cut -d' ' -f1 | sort -u

2、取出最后登录到当前系统的用户的相关信息。
[root@localhost ~]# who | tail -n1
# 或
[root@localhost ~]# last | head -n1

3、取出当前系统上被用户当作其默认shell的最多的那个shell。
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -n | tail -n1
# 输出结果为:
20 /sbin/nologin

4、将/etc/passwd 中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
# 创建maxusers.txt文件:
[root@localhost ~]# touch /tmp/maxusers.txt
#将结果保存至maxusers.txt中:
[root@localhost ~]# sort -t: -k3 -n /etc/passwd | tail -n10 | tr 'a-z' 'A-Z' > /tmp/maxusers.txt
# 结果显示(验证):
[root@localhost ~]# cat /tmp/maxusers.txt

5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
对于CentOS6.x可以采用:
[root@localhost ~]# ifconfig | grep " inet " | cut -d: -f2 | cut -d' ' -f1 | head -1
192.168.178.129

但CentOS7.x上该命令并不可用,为何?
对比两个版本,会发觉同样的ifconfig命令,IP前面的标识名称,位置是不一样的,而且网卡名称也不相同。
有没有一种办法可以实现通用。经过各方面尝试,以及跟同学们的讨论后得出,使用多次grep加正则表达式可实现:
对CentOS6.x和CentOS7.x均适用的命令:
[root@localhost ~]# ifconfig |grep -A 7 "^e[tn]" |grep -o -i " inet .*$" | grep -o "[0-9].*$"| cut -d' ' -f1
192.168.178.129

6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
# 创建etc.conf文件:
[root@localhost ~]# touch /tmp/etc.conf
# 保存输出结果:
[root@localhost ~]# ls -a /etc/*.conf | tr 'a-z' 'A-Z' > /tmp/etc.conf
# 显示输出(验证结果正确性):
[root@localhost ~]# cat /tmp/etc.conf

7、显示/var目录下一级子目录或文件的总个数。
[root@localhost ~]# ls -a /var | wc -w
26

8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@localhost ~]# cat /etc/group | sort -t: -k3 -n | head -n 10
# 或:
[root@localhost ~]# sort -t: -k3 -n /etc/group | head -n 10

9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
# 创建etc.test文件:
[root@localhost ~]# touch /tmp/etc.test
#将两文件内容合并保存在etc.test文件中
[root@localhost ~]# cat /etc/fstab /etc/issue > /tmp/etc.test

# 结果显示:
[root@localhost ~]# cat /tmp/etc.test

10、请总结描述用户和组管理类命令的使用方法并完成以下练习: (1)、创建组distro,其GID为2016;
[root@localhost ~]# group -g 2016
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@localhost ~]# useradd mandriva -u 1005 -g distro

(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@localhost ~]# useradd mageia -u 1100 -d /home/linux

(4)、给用户mageia添加密码,密码为mageedu;
[root@localhost ~]# echo 'mageedu' | passwd mageia --stdin
(5)、删除mandriva,但保留其家目录;
# 删除用户mandriva:
[root@localhost ~]# userdel mandriva
结果验证:
#(1)查看账号是否删除:
[root@localhost ~]# cat /etc/passwd
#(2)查看删除后mandriva家目录是否存在:
[root@localhost ~]# ll /home

(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
# 创建组peguin:
[root@localhost ~]# groupadd peguin
# 创建用户slackware:
[root@localhost ~]# useradd slackware -u 2002 -g distro -G peguin
# 验证结果:
[root@localhost ~]# id slackware
(7)、修改slackware的默认shell为/bin/tcsh;
[root@localhost ~]# usermod -s /bin/tcsh slackware
(8)、为用户slackware新增附加组admins;
# 新增组admins:
[root@localhost ~]# groupadd admins
# 为用户slackware新增附加组admins:
[root@localhost ~]# usermod -G admins slackware
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@localhost ~]# passwd slackware
[root@localhost ~]# passwd -n3 -x180 -w3  slackware
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
# 创建clouds、nova组:
[root@localhost ~]# groupadd clouds
[root@localhost ~]# groupadd nova

# 创建用户openstack:
[root@localhost ~]# useradd openstack -u 3003 -g clouds -G peguin,nova
# 结果验证:
[root@localhost ~]# id openstack
uid=3003(openstack) gid=2020(clouds) groups=2020(clouds),2017(peguin),2021(nova)
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[root@localhost ~]# useradd mysql -r -s /sbin/nologin
# 查看:
[root@localhost ~]# cat /etc/passwd | tail -1
mysql:x:498:498::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[root@localhost ~]# echo "mylinux" | passwd --stdin openstack
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息