馬哥教育網絡班22期+第3周課程練習

1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。

   [test2@localhost ~]$ who |cut -d " " -f1|sort -u
   root
   test1
   test2

   [test2@localhost ~]$  who |cut -d " " -f1|sort|uniq
   root
   test1
   test2

   [test2@localhost ~]$  who |cut -d " " -f1|uniq
   root
   test1
   test2

2、取出最后登錄到當前系統的用戶的相關信息。

   [root@localhost ~]#  w|sed -n '1,3p'
    22:33:44 up  1:11,  4 users,  load average: 0.00, 0.01, 0.05
   USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
   root     pts/0    192.168.1.110    21:43   50:01   0.02s  0.02s -bash

   [root@localhost ~]# w | head -3
    22:32:23 up  1:10,  4 users,  load average: 0.00, 0.01, 0.05
   USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
   root     pts/0    192.168.1.110    21:43   48:40   0.02s  0.02s -bash

   [root@localhost ~]# who |cut -d " " -f1 |sed -n '1p'|id 
   uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023


3、取出當前系統上被用戶當作其默認shell的最多的那個shell。

   [root@localhost ~]#  cut -d: -f7 /etc/passwd |sort|uniq -c|sort -n|tail -n 1
        23 /sbin/nologin


4、將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxusers.txt文件中。

   sort -t: -k3 -n /etc/passwd|tail -n 10|cut -d: -f1|tr "a-z" "A-Z">/tmp/maxusers.txt


5、取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分。

   [root@localhost ~]# ifconfig eth0|grep 'inet addr:'|cut -d: -f2|cut -d" " -f1 
   192.168.1.14

   [root@localhost ~]# ifconfig eth0|sed -nr 's/^.*addr:(.*) Bca.*$/\1/p' 
   192.168.1.14

   [root@localhost ~]# grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0 |cut -d"=" -f2
   192.168.1.14




6、列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf文件中。

   [root@localhost ~]# ls /etc/*.conf | grep  -E  -o  "[^/]+/?$"|tr "a-z" "A-Z" >/tmp/etc.conf

7、顯示/var目錄下一級子目錄或文件的總個數。

   [root@localhost ~]# ls /var |wc -l
   19

8、取出/etc/group文件中第三個字段數值最小的10個組的名字。

   [root@localhost ~]# sort -t: -k3 -n /etc/group|tail -n 10|cut -d: -f1
   pulse-access
   pulse
   desktop_user_r
   desktop_admin_r
   ctapiusers
   rtkit
   test
   test1
   test2
   test3

9、將/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中。

   [root@localhost ~]# cat /etc/fstab /etc/issue >/tmp/etc.test

10、請總結描述用戶和組管理類命令的使用方法并完成以下練習:

   useradd :創建用戶
      useradd [選項] 登錄名
         -u, --uid UID:指定UID
         -g,--gid GROUP:指定基本組ID,此組得事先存在
         -G,--groups GROUP1 [,GROUP2,...[,GROUPN]] 指明用戶所屬的附加組,多個組之間用逗號分隔
         -c, --comment COMMENT:指明注釋信息
         -d, --home HOME_DIR:以指定的路徑為用戶的家目錄,通過復制/etc/skel此目錄重命名實現,指定的家目錄路徑如果事先存在,則不會為用戶環境配置文件
         -s, --shell SHELL:指定用戶的默認shell,可用的所有shell列表存儲在/etc/shells文件中
         -r, --system:創建系統用戶
      注意:創建用戶時的諸多默認設定配置文件為/etc/login.defs

      useradd -D:顯示創建用戶的默認配置
      useradd -D:選項:修改默認選項的值,修改的結果保存于/etc/default/useradd

   usermod: 修改用戶屬性
      usermod [選項] 登錄名
         -u, --uid UID:指定UID
         -g, --gid GROUP:指定基本組ID,此組得事先存在
         -G, --group GROUP1[,GROUP2,...[GROUPN]]:指明用戶所屬的附加組,多個組之間用逗號分隔
         -c, --comment COMMENT:指明注釋信息
         -d, --home HOME_DIR:以指定的路徑為用戶的家目錄,通過復制/etc/skel此目錄并重命名實現;指定的家目錄路徑如果事先存在,則不會為用戶復制環境配置文件
         -s, --shell SHELL:指定用戶的默認shell,可用的所有shell列表在/etc/shells文件中
         -r, --system:創建系統用戶

   userdel:刪除用戶
      userdel [選項] 登錄
         -r:刪除用戶時一并刪除其家目錄


   groupadd:添加組
      groupadd [選項] GROUP
         -g GID:指定GID;默認是上一個組的GID+1
         -r:創建系統組

   groupmod:修改組屬性
      groupadd [選項] GROUP
         -g GID:修改GID
         -n new_name:修改組名

   groupdel:刪除組
      groupdel [選項] GROUP

 

   (1)、創建組distro,其GID為2016;

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

   (2)、創建用戶mandriva, 其ID號為1005;基本組為distro;

      [root@test ~]# useradd -u 1005 mandriva
      [root@test ~]# id mandriva
      uid=1005(mandriva) gid=1005(mandriva) groups=1005(mandriva)

   (3)、創建用戶mageia,其ID號為1100,家目錄為/home/linux;


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

   (4)、給用戶mageia添加密碼,密碼為mageedu;

      [root@test ~]# echo "mageedu"|passwd --stdin mageia

   (5)、刪除mandriva,但保留其家目錄;

      [root@test home]# userdel mandriva

   (6)、創建用戶slackware,其ID號為2002,基本組為distro,附加組peguin;

      [root@test home]# id slackware
      uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin) 

   (7)、修改slackware的默認shell為/bin/tcsh;

      [root@test home]# usermod -s /bin/tcsh slackware

   (8)、為用戶slackware新增附加組admins;

      [root@test home]# gpasswd -a slackware admins
      Adding user slackware to group admins
      [root@test home]# id slackware
      uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)

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

      [root@test home]# passwd -n 3 -x 180 -w 3 slackware;echo "mageedu"|passwd --stdin slackware

   (10)、添加用戶openstack,其ID號為3003, 基本組為clouds,附加組為peguin和nova;

      [root@test home]# useradd -u 3003 -g clouds -G peguin,nova openstack
      [root@test home]# id openstack
      uid=3003(openstack) gid=2019(clouds) groups=2019(clouds),2017(peguin),2020(nova)

   (11)、添加系統用戶mysql,要求其shell為/sbin/nologin;

      [root@test home]# useradd -r -s /sbin/nologin mysql
      [root@test home]# cat /etc/passwd|grep mysql
      mysql:x:496:492::/home/mysql:/sbin/nologin

   (12)、使用echo命令,非交互式為openstack添加密碼。
   
      [root@test home]# echo "mageedu" |passwd --stdin openstack

原創文章,作者:N22_上海_長清,如若轉載,請注明出處:http://www.www58058.com/41768

(0)
N22_上海_長清N22_上海_長清
上一篇 2016-08-30
下一篇 2016-08-30

相關推薦

  • 網絡模型知識點概括

       網絡模型分層 應用層 網絡進程訪問表示層 數據表示會話層 主機間通信傳輸層 端到端連接網絡層 數據傳輸   數據鏈路層 訪問介質物理層 物理線纜二進制封包與解封從下至上封包加包頭通過上層協議傳給對方機器,對方機器接受后需要從上至下拆解包頭,這個過程叫解封三種通訊模式單播廣播組播局域網:由服務器-》交換機-》pc …

    Linux干貨 2017-05-02
  • 位運算符及其應用

    一、C語言的六種位運算符: & 按位與 | 按位或 ^ 按位異或 ~ 取反 << 左移 >> 右移 1.   按位與運算 按位與運算符"&"是雙目運算符。     &nb…

    Linux干貨 2015-11-18
  • 面授20期2班-08月4號課堂與課后習題

    課堂習題 1、顯示/etc/init.d/functions文件中所有的單詞及出現的次數 cat /etc/init.d/functions | tr -sc "[:alpha:]" '\n' |sort | uniq -c 2、找出ifconfig命令結果中本機的所有IPv4地址 centos6:ifconfig | h…

    Linux干貨 2016-08-08
  • 馬哥教育網絡22期+第四周作業博客

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。    [root@centos-rpi3 skel]# cp -r /etc/skel /home/tuser1 && chmod -R g-rwx,o-rwx /home/tuser1 …

    Linux干貨 2016-09-08
  • Linux磁盤和文件系統基礎

    概述     Linux系統上,磁盤要正常的進行數據的存儲,需要先進行分區,再進行格式化生成文件系統,最后掛載到某個目錄下,才能進行正常的數據存取,本篇就介紹一下磁盤從分區到正常使用的幾個步驟,具體分為以下幾個部分:     1、磁盤的基礎概念   &nbsp…

    Linux干貨 2016-08-29
  • 邏輯卷管理工具lvm2

    lvm2:location Volume Manage Version 2 linux支持邏輯卷的模塊為dm模塊                        dm模塊是將一個或多個底層物理設備組織成一個邏輯設備的模塊。 在CentOS中,…

    Linux干貨 2016-03-27

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-09-14 08:10

    整體內容不錯,排版也清晰;繼續保持。

欧美性久久久久