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.www58058.com/69685

(0)
jaylinjaylin
上一篇 2017-02-21
下一篇 2017-02-22

相關推薦

  • DNS實驗

      建立DNS服務器(正向解析) linux沒有緩存 windows有dns緩存 權威結果,非權威結果 無論是正向解析數據庫還是反向解析數據中  寫管理員郵件地址或完整域名(FQDN)都要再后面加上點。否則系統會自動不上區域名稱。 實驗環境先清理防火墻規則,關閉防火墻. centos7 systemctl disabl…

    Linux干貨 2016-11-01
  • Linux文件查找之find秘笈

    前言     Linux的基本特點之一是一切皆文件,在系統管理過程中難免會需要查找特定類型的文件,那么問題來了:如何進行有效且準確的查找呢?本文將對Linux系統中的文件查找工具及用法進行詳細講解。 常用工具對比     常用的文件查找工具主要有locate(非實時查找)和find(實時查找)。locate查找依賴于索…

    2015-03-23
  • 基礎命令

    1,對于Linux的實驗環境我們要用到虛擬機,往往每次上線都要登錄,這時候如果把用戶設為自動登錄就會方便許多,接下來就介紹下設置虛擬機的自動登錄 , /etc/gdm/custom.conf 這個就是設置自動登錄的一個配置文件,我們先來使用nano打開這個文件,如下 # GDM configuration storage 這是GDM的一個配置存儲 [daem…

    2017-11-19
  • 8月3號 用戶權限作業

     1,當用戶xiaoming 對/testdir  目錄無執行權限時,意味著無法做哪些操作?   先創建目錄/testdir,接著修改目錄的/testdir的權限,用xiaoming的身份去進入/testdir目錄 [root@localhost ~]# mkdir /testdir [root@l…

    Linux干貨 2016-08-08
  • Linux基礎知識之用戶和組的配置文件解析

    實驗環境:  Linux系統的版本為CentOS6.8_x86_64版本,以root用戶遠程用xshell連接,進行實驗。 1.創建用戶設置的配置文件:/etc/default/useradd        useradd 的配置文件如下圖所示:        &nbs…

    Linux干貨 2016-08-02
  • Linux文件權限管理及目錄文件的深入理解。

    文件權限及目錄 初學Linux,感覺這個東西該復雜,而且邏輯非常的強。難~! 自己根據學習到的理論和實踐,得出的對文件權限,進程,以及特殊權限的深入理解。希望能解決初學者對于權限的困惑。如有錯誤請指正。 文件的權限,指定的是什么? 是文件的權限位上的權限,針對三類用戶,任何用戶都必須是三類用戶中的一種,屬主屬組和其他人的權限rwx   &…

    Linux干貨 2016-08-10

評論列表(1條)

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

    解答問題的方法很棒

欧美性久久久久