1、列出當前系統上所有已經登陸的用戶名,注意同一個用戶登陸多次,則只顯示一次即可。
[root@localhost ~]# w
11:50:45 up 24 min, ?3 users, ?load average: 0.04, 0.04, 0.10
USER ? ? TTY ? ? ?FROM ? ? ? ? ? ? LOGIN@ ? IDLE ? JCPU ? PCPU WHAT
root ? ? :0 ? ? ? :0 ? ? ? ? ? ? ? 11:27 ? ?xdm? ? 1:47 ? 0.33s gdm-session-worker [pam/gdm-password]
root ? ? pts/0 ? ?:0 ? ? ? ? ? ? ? 11:27 ? ?5.00s ?0.09s ?0.00s w
root ? ? pts/1 ? ?:0 ? ? ? ? ? ? ? 11:32 ? 53.00s ?0.06s ?0.00s less -s
[root@localhost ~]# w -h
root ? ? :0 ? ? ? :0 ? ? ? ? ? ? ? 11:27 ? ?xdm? ? 1:47 ? 0.33s gdm-session-worker [pam/gdm-password]
root ? ? pts/0 ? ?:0 ? ? ? ? ? ? ? 11:27 ? ?0.00s ?0.09s ?0.00s w -h
root ? ? pts/1 ? ?:0 ? ? ? ? ? ? ? 11:32 ? 56.00s ?0.06s ?0.00s less -s
[root@localhost ~]# w -h | cut -d” ” -f1
root
root
root
[root@localhost ~]# w -h | cut -d” ” -f1 ?| uniq
root
[root@localhost ~]#
2、取出最后登錄到當前系統的用戶的詳細信息。
[root@localhost ~]# w
11:59:01 up 33 min, ?4 users, ?load average: 0.02, 0.06, 0.09
USER ? ? TTY ? ? ?FROM ? ? ? ? ? ? LOGIN@ ? IDLE ? JCPU ? PCPU WHAT
root ? ? :0 ? ? ? :0 ? ? ? ? ? ? ? 11:27 ? ?xdm? ? 2:12 ? 0.34s gdm-session-worker [pam/gdm-password]
root ? ? pts/0 ? ?:0 ? ? ? ? ? ? ? 11:27 ? ?5.00s ?0.14s ?0.00s w
root ? ? pts/1 ? ?:0 ? ? ? ? ? ? ? 11:32 ? ?9:09 ? 0.06s ?0.00s less -s
root ? ? pts/2 ? ?:0 ? ? ? ? ? ? ? 11:57 ? ?1:25 ? 0.05s ?0.05s bash
[root@localhost ~]# w -h | sort -t” ” -k4 | tail -1
root ? ? pts/2 ? ?:0 ? ? ? ? ? ? ? 11:57 ? ?1:31 ? 0.05s ?0.05s bash
[root@localhost ~]#
3、取出當前系統上被用戶當作其默認shell的最多的那個shell
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 |sort ?| uniq -c
4 /bin/bash
1 /bin/sync
1 /sbin/halt
53 /sbin/nologin
1 /sbin/shutdown
[root@localhost ~]#
4、將/etc/passwd中的第三個字段數值最大的后10個用戶信息全部改成大寫后保存至/tmp/maxusers.txt文件中。
~]# sort -t: -k3 -n ?/etc/passwd | tail -10 | tr ‘a-z’ ‘A-Z’ >/tmp/maxusers.txt
~]# cat /tmp/maxusers.txt
5、取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分。
方法1:
~]# ifconfig | cut -d” ” -f10,13,16 | sort -n -u
方法2:由于ifconfig地址x.x.x.x中的x不可能大于255,也不會出現這種格式有大于255的數值,因此這是模糊匹配,如果有300.400.500.600,也會被匹配出來
~]# ifconfig | grep -o “\<[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}”
192.168.5.128
255.255.255.0
192.168.5.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
[root@localhost ~]#
6、列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf文件中。
[root@localhost ~]# ls /etc/*.conf | tr ‘a-z’ ‘A-Z’ ?>/tmp/etc.conf
7、顯示/var目錄下一級子目錄或文件的總個數。
[root@localhost ~]# ls /var/ | wc -w
8、取出/etc/group文件中的第三個字段數值最小的10個組的名字。
[root@localhost ~]# sort -t: -k3 -n ?/etc/group | head | cut -d: -f1
9、取出/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中。
[root@localhost ~]# cat /etc/{fstab,issue} >/tmp/etc.test
10、請總結描述用戶和組管理命令的使用方法并完成以下練習:
新增用戶:useradd
useradd [options] LOGIN
useradd -D????//顯示創建用戶的默認設置
useradd -D [options]????//修改創建用戶的默認選項,修改的結果保存于/etc/default/useradd文件中,也可以通過修改此文件實現。
option:
-u:指定用戶UID;
-g:指定用戶基本組GID,此組需事先存在
-c:注釋信息
-G:指定用戶所屬附加組,多個組之間用逗號分隔
-d:指定用戶家目錄路徑,通過復制/etc/skel目錄并重命名實現,如果指定路徑存在,則不會初始化用戶環境配置。
-s:指定用戶的默認登陸shell,可用的shell列表/etc/shells
-r:創建系統用戶,
-M:不為用戶創建家目錄
-f:密碼過期后,賬戶被徹底禁用之前的天數
-D:顯示用戶創建時的默認參數設置。
修改用戶信息:
usermod [options] LOGIN
options:
-u:修改用戶的UID
-g:修改用戶基本組GID,組需事先存在
-G:修改用戶所屬附加組,會從原來的附加組中脫離,增加到新的附加組中
-a:只能和-G一起使用,用于追加新的附加組,原來的附加組和現在的附加組同時存在
-c:修改注釋信息
-s:修改用戶的shell
-d:修改用戶的家目錄,原家目錄中的文件不會被轉移至新的位置,
-m:只能和-d一起使用,轉移家目錄路徑時,將原來家目錄中的文件一起轉移至新的家目錄位置。
-l:修改用戶名
-L:鎖定用戶密碼,禁止用戶登陸,即在原來的密碼字符串之前添加!
-u:解鎖用戶的密碼。
刪除用戶:
userdel [options] LOGIN
-f:強制刪除,即使用戶已經登陸。
-r:刪除用戶時,一并刪除其家目錄。
新增組:
groupadd [options] group
options:
-r:創建系統組
-g:指定新建組的GID。
修改組:
groupmod [options] GROUP
options:
-g:修改組的GID
-n:修改組名
-p:修改組密碼
刪除組:
groupdel [options] GROUP
10.1、創建組distro,其GID為為2016;
[root@localhost home]# groupadd -g 2016 distro
[root@localhost home]# tail -1 /etc/group
distro:x:2016:
10.2、創建用戶mandriva,其ID號為1005;基本組為distro;
[root@localhost home]# useradd -u 1005 -g 2016 mandriva
[root@localhost home]# tail -1 /etc/passwd
mandriva:x:1005:2016::/home/mandriva:/bin/bash
[root@localhost home]#
10.3、創建用戶mageia,其ID號為1100,家目錄為/home/linux;
[root@localhost home]# useradd -u 1100 -d /home/linux mageia
[root@localhost home]# ls
123 ?linux ?mandriva ?user ?user1 ?zhang2
[root@localhost home]# tail -1 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash
[root@localhost home]#
10.4、給用戶mageia添加密碼,密碼為mageedu;
[root@localhost home]# passwd mageia
Changing password for user mageia.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost home]#
10.5、刪除mandriva,但保留其家目錄
[root@localhost home]# userdel mandriva
[root@localhost home]# ls
123 ?linux ?mandriva ?user ?user1 ?zhang2
[root@localhost home]#
10.6、創建用戶slackware,其ID號為2002,基本組為distro,附加組peguin
[root@localhost home]# groupadd peguin
[root@localhost home]# tail -1 /etc/group
peguin:x:2017:
[root@localhost home]# useradd -u 2002 -g 2016 -G 2017 slackware
[root@localhost home]# tail -1 /etc/group
peguin:x:2017:slackware
[root@localhost home]#
10.7、修改slackware的默認shell為/bin/tcsh;
[root@localhost home]# usermod -s /bin/tcsh slackware
[root@localhost home]# tail -1 /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/tcsh
[root@localhost home]#
10.8、為用戶slackware新增附加組admins;
[root@localhost home]# usermod -aG admins slackware
[root@localhost home]# tail -5 /etc/group
user:x:1000:
distro:x:2016:
mageia:x:1100:
peguin:x:2017:slackware
admins:x:2018:slackware
[root@localhost home]#
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/87670