第三周

1、列出當(dāng)前系統(tǒng)上所有已經(jīng)登錄的用戶的用戶名,注意:同一用戶登錄多次,則只顯示一次即可。

[root@localhost ~]# who | cut -d" " -f1 | uniq
使用who命令列出列出當(dāng)明顯登錄的所有用戶,使用cut命令取出用戶名,使用uniq命令去重

2、取出最后登錄到當(dāng)前系統(tǒng)的用戶的相關(guān)信息。

[root@localhost ~]# who |tail -n 1 |cut -d' ' -f1 |id
取當(dāng)前用戶最后登錄的信息的一行數(shù)據(jù),取出用戶名,查詢用戶相關(guān)信息

3、取出當(dāng)前系統(tǒng)上被用戶當(dāng)作其默認(rèn)shell的最多的那個shell。

[root@localhost ~]# cut -d: -f7 /etc/passwd|uniq -c|sort -n
[root@localhost ~]# cut -d: -f7 /etc/passwd|uniq -c|sort -n|tail -n 1
取出passwd上所有使用的shell名稱,使用uniq –c統(tǒng)計重復(fù)次數(shù),按數(shù)值排序后取最大值

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

[root@localhost ~]# sort -t: -k3 /etc/passwd –n | tail -10 | tr a-z A-Z >/tmp/maxusers.txt
將passwd文件按第三個字段排序,用tail取最后十行,使用tr 命令轉(zhuǎn)換大小寫,重定向保存么maxusers.txt文件

5、取出當(dāng)前主機(jī)的IP地址,提示:對ifconfig命令的結(jié)果進(jìn)行切分。

[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:A4:57:BC
inet addr:192.168.50.177 Bcast:192.168.50.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fea4:57bc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:230917 errors:0 dropped:0 overruns:0 frame:0
TX packets:175189 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:309476096 (295.1 MiB) TX bytes:15819665 (15.0 MiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:990 errors:0 dropped:0 overruns:0 frame:0
TX packets:990 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:333209 (325.3 KiB) TX bytes:333209 (325.3 KiB)
[root@localhost ~]# ifconfig | grep "inet addr" | cut -d: -f2| cut -d" " -f1
192.168.50.177
127.0.0.1
ifconfig命令結(jié)果如下:對結(jié)果進(jìn)行正規(guī)則表達(dá)式匹配,然后用cut命令切分,得到ip地址

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

[root@localhost ~]# ls /etc/*.conf | tr a-z A-Z >/tmp/etc.conf
[root@localhost ~]# cat /tmp.etc.conf

7、顯示/var目錄下一級子目錄或文件的總個數(shù)

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

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

[root@localhost ~]# cat /etc/group|sort -n -t: -k3|head -n 10
[root@localhost ~]# cat /etc/group|sort -n -t: -k3|head -n 10|cut -d: -f1
先按第三個字段排序,取前十行,然后用cut命令取出組名

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

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

10、總結(jié)描述用戶和組管理類命令的使用方法

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

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

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

[root@localhost ~]# useradd mandriva -u 1005 -g distro
[root@localhost ~]# id mandriva
uid=1005(mandriva) gid=2016(distro) groups=2016(distro)

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

[root@localhost ~]# useradd mageia -u 1100 -d /home/linux
[root@localhost ~]# tail -1 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash

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

[root@localhost ~]# echo 'mageedu' | passwd mageia --stdin

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

[root@localhost ~]# userdel mandriva
[root@localhost ~]# ls /home/
linux mandriva user1 user2 user3
[root@localhost ~]# id mandriva
id: mandriva: no such user

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

[root@localhost ~]# groupadd peguin
[root@localhost ~]# useradd slackware -u 2002 -g distro -G peguin
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)

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

[root@localhost ~]# usermod -s /bin/tcsh slackware

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

[root@localhost ~]# groupadd admins
[root@localhost ~]# usermod -a -G admins slackware
-a不刪除原有附加組
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)

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

[root@localhost ~]# echo "123456" | passwd --stdin slackware
[root@localhost ~]# passwd -n3 -x180 -w3 slackware
[root@localhost ~]# chage -l slackware
Last password change : Aug 22, 2016
Password expires : Feb 18, 2017
Password inactive : never
Account expires : never
Minimum number of days between password change : 3
Maximum number of days between password change : 180
Number of days of warning before password expires : 3

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

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

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

[root@localhost ~]# useradd mysql -r -s /sbin/nologin
[root@localhost ~]# cat /etc/passwd | tail -1
mysql:x:995:993::/home/mysql:/sbin/nologin

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

[root@localhost ~]# echo "password" | passwd --stdin openstack

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容