N26_第三周作业

1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。

演示:

[root@joylin test]# who|cut -d" " -f1|uniq 
root
gentoo
[root@joylin test]# who|cut -d" " -f1|uniq -c
      5 root
      1 gentoo
或者
[root@joylin test]# who|cut -d" " -f1|sort -u
gentoo
root
或者
[root@joylin test]# w|sed '1,2d'|cut -d" " -f1|uniq
root
gentoo

2、取出最后登录到当前系统的用户的相关信息。

[root@joylin test]# last|head -1
gentoo   pts/4        10.0.0.52        Tue Feb 21 22:02   still logged in 

3、取出当前系统上被用户当作其默认shell的最多的那个shell。

[root@joylin ~]# cat /etc/passwd|cut -d: -f7|uniq -c|sort -nr
     34 /sbin/nologin
      7 /bin/bash
      4 /sbin/nologin
      1 /sbin/shutdown
      1 /sbin/halt
      1 /bin/sync
      1 /bin/bash
[root@joylin ~]# cat /etc/passwd|cut -d: -f7|uniq -c|sort -nr|head -1 
     34 /sbin/nologin

4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。

[root@joylin test]# cat /etc/passwd|sort -nrk 3 -t:|head -10 >/tmp/maxusers.txt ##方法1
[root@joylin test]# cat /tmp/maxusers.txt 
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gentoo:x:2005:2005::/home/gentoo:/bin/bash
user2:x:2004:2004::/home/user2:/bin/bash
user1:x:2003:2003::/home/user1:/bin/bash
test:x:2002:2002::/home/test:/bin/bash
bb:x:2001:2001::/home/bb:/bin/bash
aa:x:2000:1000:test user:/test:/bin/bash
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
[root@joylin test]# cat /etc/passwd|sort -nk 3 -t:|tail >/tmp/maxusers.txt  ##方法2 [root@joylin test]# cat /tmp/maxusers.txt  unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin aa:x:2000:1000:test user:/test:/bin/bash bb:x:2001:2001::/home/bb:/bin/bash test:x:2002:2002::/home/test:/bin/bash user1:x:2003:2003::/home/user1:/bin/bash user2:x:2004:2004::/home/user2:/bin/bash gentoo:x:2005:2005::/home/gentoo:/bin/bash nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。

[root@joylin ~]# ifconfig ens33|grep inet|cut -d" " -f10 ##方法1
10.0.0.2
[root@joylin ~]# ifconfig ens33|head -2|tail -1|cut -d" " -f10 ##方法2
10.0.0.2

6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。

[root@joylin ~]# find /etc/ -name "*.conf"|tr 'a-z' 'A-Z' >/tmp/etc.conf
[root@joylin ~]# head -3 /tmp/etc.conf 
/ETC/RESOLV.CONF
/ETC/FONTS/CONF.D/65-0-SMC-MEERA.CONF
/ETC/FONTS/CONF.D/59-LIBERATION-SANS.CONF

7、显示/var目录下一级子目录或文件的总个数。

[root@joylin test]# tree -L 1 /var/|wc -l
27

8、取出/etc/group文件中第三个字段数值最小的10个组的名字。

[root@joylin test]# cat /etc/group|sort -nrk 3 -t:|tail
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:
[root@joylin test]# cat /etc/group|sort -nrk 3 -t:|tail|cut -d: -f1 ##方法1
kmem
mem
lp
disk
tty
adm
sys
daemon
bin
root
[root@joylin test]# cat /etc/group|sort -nk 3 -t:|head 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@joylin test]# cat /etc/group|sort -nk 3 -t:|head|cut -d: -f1 ##方法2 root bin daemon sys adm tty disk lp mem kmem

9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。

[root@joylin test]# sort -c /etc/fstab /etc/issue  ##-c检查文件是否已经排序
sort: 不允许额外的操作数"/etc/issue" 与-c 一起使用
[root@joylin test]# sort -c /etc/fstab
sort:/etc/fstab:4:无序: # Created by anaconda on Tue Feb  7 00:35:57 2017
[root@joylin test]# sort -c /etc/issue
sort:/etc/issue:2:无序: Kernel \r on an \m
[root@joylin test]# sort /etc/fstab >/test/fstab
[root@joylin test]# sort /etc/issue >/test/issue
[root@joylin test]# sort -m /test/fstab /test/issue >/tmp/etc.test #-m合并文件
[root@joylin test]# wc -l /etc/fstab 
12 /etc/fstab
[root@joylin test]# wc -l /etc/issue
3 /etc/issue
[root@joylin test]# wc -l /tmp/etc.test 
15 /tmp/etc.test

10、请总结描述用户和组管理类命令的使用方法并完成以下练习:

   (1)、创建组distro,其GID为2016;

[root@joylin test]# groupadd distro -g 2016
[root@joylin test]# tail -1 /etc/group
distro:x:2016:

   (2)、创建用户mandriva, 其ID号为1005;基本组为distro;

[root@joylin test]# useradd mandriva -u 1005 -g distro
[root@joylin test]# grep mandriva /etc/passwd
mandriva:x:1005:2016::/home/mandriva:/bin/bash

   (3)、创建用户mageia,其ID号为1100,家目录为/home/linux;

[root@joylin test]# useradd mageia -u 1100 -d /home/linux
[root@joylin test]# grep mageia /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash

   (4)、给用户mageia添加密码,密码为mageedu;

[root@joylin test]# echo "mageedu"|passwd --stdin mageia
更改用户 mageia 的密码 。
passwd:所有的身份验证令牌已经成功更新。

   (5)、删除mandriva,但保留其家目录;

[root@joylin test]# grep mandriva /etc/passwd
mandriva:x:1005:2016::/home/mandriva:/bin/bash
[root@joylin test]# userdel mandriva 
[root@joylin test]# grep mandriva /etc/passwd

   (6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;

[root@joylin test]# groupadd peguin
[root@joylin test]# useradd slackware -u 2002 -g distro -G peguin
[root@joylin test]# grep 2002 /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/bash

   (7)、修改slackware的默认shell为/bin/tcsh;

[root@joylin test]# grep slackware /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/bash
[root@joylin test]# usermod -s /bin/tcsh slackware 
[root@joylin test]# grep slackware /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/tcsh

   (8)、为用户slackware新增附加组admins;

[root@joylin test]# grep admins /etc/group
[root@joylin test]# groupadd admins
[root@joylin test]# usermod -G admins slackware
[root@joylin test]# groups slackware
slackware : distro admins
[root@joylin test]# id slackware
uid=2002(slackware) gid=2016(distro) 组=2016(distro),2018(admins

   (9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;

[root@joylin test]# chage -l slackware
最近一次密码修改时间                  :2月 21, 2017
密码过期时间                  :从不
密码失效时间                  :从不
帐户过期时间                      :从不
两次改变密码之间相距的最小天数     :0
两次改变密码之间相距的最大天数     :99999
在密码过期之前警告的天数    
[root@joylin test]# chage -m 3 -M 180 -W 3 slackware
[root@joylin test]# chage -l slackware
最近一次密码修改时间                  :2月 21, 2017
密码过期时间                  :8月 20, 2017
密码失效时间                  :从不
帐户过期时间                      :从不
两次改变密码之间相距的最小天数     :3
两次改变密码之间相距的最大天数     :180
在密码过期之前警告的天数    :3

   (10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;

[root@joylin test]# grep clouds /etc/group
[root@joylin test]# grep nova /etc/group
[root@joylin test]# groupadd clouds 
[root@joylin test]# groupadd nova
[root@joylin test]# useradd openstack -u 3003 -g clouds -G peguin,nova
[root@joylin test]# groups openstack
openstack : clouds peguin nova
[root@joylin test]# id openstack
uid=3003(openstack) gid=2019(clouds) 组=2019(clouds),2017(peguin),2020(nova)

   (11)、添加系统用户mysql,要求其shell为/sbin/nologin;

[root@joylin test]# grep mysql /etc/passwd
[root@joylin test]# useradd -r mysql -s /sbin/nologin 
[root@joylin test]# grep mysql /etc/passwd
mysql:x:600:600::/home/mysql:/sbin/nologin

   (12)、使用echo命令,非交互式为openstack添加密码。

[root@joylin test]# echo "openstack"|passwd --stdin openstack
更改用户 openstack 的密码 。
passwd:所有的身份验证令牌已经成功更新。

原创文章,作者:jaylin,如若转载,请注明出处:http://www.178linux.com/69685

(0)
jaylinjaylin
上一篇 2017-02-21 15:55
下一篇 2017-02-22 00:19

相关推荐

  • 自定义命令别名及引用结果

    自定义命令别名及引用结果 alias alias命令用来设置指令的别名。我们可以使用该命令可以将一些较长的命令进行简化。 语法 alias(选项)(参数) 选项 -p:打印已经设置的命令别名。 参数 命令别名设置:定义命令别名,格式为“命令别名=‘实际命令’” 实例 显示已设置的别名 [root@localhost ~]# alias alias cp=’c…

    Linux干货 2018-03-11
  • HAProxy初探及简单案例_Net21_第二周

    前言 随着互联网业务的迅猛发展,大型电商平台和门户网站对系统的可用性和可靠性的要求越来越高,高可用集群、负载均衡集群成为一种热门的系统架构解决方案。在众多的负载均衡解决方案中,有基于硬件的负载均衡设备,例如F5、Big-IP等,也有基于软件的负载均衡产品,如LVS、Nginx以及本文介绍的HAProxy等。在软件的负载均衡产品中,又分为两种实现方式,分别是基…

    Linux干货 2016-07-22
  • yum函数介绍以及自建yum仓库

    一、前言     在之前介绍了yum的配置(详细请移步 http://www.178linux.com/archives/6445)。但是有没有发现一个问题,虽然我们已将仓库指向一个可用的仓库服务器,但是随着Linux的不断升级和改版,我们是否还需要不断的去修改仓库的配置文件,如果只有一台还好,那如果我们有多…

    Linux干货 2015-07-24
  • raid 0磁盘阵列

    raid0 1  先给sdb磁盘和sdc磁盘分区 [root@localhost ~]# fdisk /dev/sdb    Command (m for help): n Command action   e   extended   p   primary part…

    Linux干货 2017-04-25
  • yum详解及源码包的编译安装

    yum的功能:     在我们使用rpm包安装程序时,各个程序包之间可能存在非常严重的依赖关系,这就导致我们在安装一个包的同时还需要安装其他存在依赖关系的包,因此我们可以使用rpm包的前端管理工具yum,可通过yum所支持的文件共享机制来解决各个rpm包之间的依赖关系。 yum仓库的配置文件:   …

    Linux干货 2016-08-25
  • Linux运维实战之6:挂载/卸载磁盘分区

    上次博文我们具体讨论了Linux系统中磁盘的分区及格式化( http://www.178linux.com/60811),本次博文我们具体来说说分区的挂载哈! 【问题引入】 已经格式化好的分区如何与系统现有的文件系统建立关联(如何使用新分区)? 新的磁盘分区创建完成并格式化之后需要挂载(或装入)到目录树上才能被使用。 何谓挂载/卸载? 【答】将某个…

    Linux干货 2016-12-04

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-06 19:55

    解答问题的方法很棒