Linux用戶管理
一、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。
##########################################################################
? # 使用who可以查看所有登錄的用戶,然后使用cut命令將結果進行切分,取得用戶名,最后使用sort命令,去重。
[liuqing@localhost ~]$ who
root tty1 2017-11-13 18:02
root pts/0 2017-12-15 11:05 (192.168.58.158)
liuqing pts/1 2017-12-15 12:04 (192.168.58.158)
[liuqing@localhost ~]$ who | cut -d’ ‘ -f1 | sort -u
liuqing
root
##########################################################################
二、列出最后登錄到當前系統的用戶的相關信息
##########################################################################
# 使用last命令,查找登錄的最后的用戶列表,使用head取出第一行數據,使用cut取得用戶名,最后使用id獲取用戶的相關信息
[liuqing@localhost ~]$ last | head -1 | cut -d ‘ ‘ -f1 | id
uid=1000(liuqing) gid=1000(liuqing) 組=1000(liuqing) 環境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
##########################################################################
三、 取出當前系統上被用戶當作其默認shell最多的那個shell
##########################################################################
# 先讀取passwd文件,再使用cut取出所有的shell,然后使用uniq進行統計每行出現的次數,再使用sort按數值排序,然后取出最后一行。
[liuqing@localhost ~]$ cat /etc/passwd | cut -d: -f7 | uniq -c | sort -n | tail -1
16 /sbin/nologin
#########################################################################
四、 將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保至/tmp/maxusers.txt文件中
#########################################################################
# 先使用cat讀取文件,然后使用sort將第3字段按數值排序,使用tail取出后10行,接著使用tr轉換小寫字母到大寫字母,然后寫入/tmp/maxusers.txt文件中。
[root@localhost tmp]# cat /etc/passwd | sort -n -t: -k3 | tail -10 | tr ‘a-z’ ‘A-Z’ > /tmp/maxusers.txt
[root@localhost tmp]# cat /tmp/maxusers.txt
NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN
ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN
SYSTEMD-NETWORK:X:192:192:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN
CHRONY:X:997:995::/VAR/LIB/CHRONY:/SBIN/NOLOGIN
LIBSTORAGEMGMT:X:998:996:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/SBIN/NOLOGIN
POLKITD:X:999:997:USER FOR POLKITD:/:/SBIN/NOLOGIN
LIUQING:X:1000:1000:LIUQING:/HOME/LIUQING:/BIN/BASH
USER2:X:1002:1002::/HOME/USER2:/BIN/BASH
USER3:X:1003:1003::/HOME/USER3:/BIN/BASH
USER1:X:1004:1004::/HOME/USER1:/BIN/BASH
##########################################################################
五、 取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分
##########################################################################
ifconfig eth0 | egrep -o “\<(([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][1-9]|[2][0-5][0-4])\.){3}([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][1-9]|[2][0-5][0-4])\>”
[root@localhost tmp]# ifconfig eth0 | egrep “\<(([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][1-9]|[2][0-5][0-4])\.){3}([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][1-9]|[2][0-5][0-4])\>” | cut -d’ ‘ -f10
##########################################################################
六、 列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf文件中
##########################################################################
find /etc -name “*.conf” | tr ‘a-z’ ‘A-Z’ > /tmp/etc.conf
[root@localhost tmp]# cat /tmp/etc.conf
/ETC/RESOLV.CONF
/ETC/PKI/CA-TRUST/CA-LEGACY.CONF
/ETC/YUM/PLUGINCONF.D/FASTESTMIRROR.CONF
/ETC/YUM/PLUGINCONF.D/LANGPACKS.CONF
/ETC/YUM/PROTECTED.D/SYSTEMD.CONF
/ETC/YUM/VERSION-GROUPS.CONF
……
##########################################################################
七、 顯示/var目錄下一級子目錄或者文件的總個數。
##########################################################################
[root@localhost tmp]# tree -L 1 /etc | tail -1 | cut -d’ ‘ -f1
89
##########################################################################
八、 取出/etc/group文件中第三個字段數值最小的10個組的名字
#########################################################################
[root@localhost tmp]# cat /etc/group | sort -n -k3 -t: | head -10 | cut -d: -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
##########################################################################
九、 將/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中
##########################################################################
[root@localhost tmp]# cat /etc/fstab /etc/issue | tee > /tmp/etc.test
[root@localhost tmp]# cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Tue Nov 14 01:28:16 2017
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=3e44d68c-8902-4591-935d-57ea49691301 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
\S
Kernel \r on an \m
##########################################################################
十、 請總結描述用戶和組管理類命令的使用方法并完成以下練習:
總結和描述詳見:另一篇博客。網址為:http://www.www58058.com/90202
##########################################################################
1. 創建組distro,其GID為2016;
[root@localhost ~]# groupadd -g 2016 distro
[root@localhost ~]# cat /etc/group | grep distro
distro:x:2016:
2. 創建用戶mandriva,其ID號是1005,基本組為distro;
[root@localhost tmp]# useradd -u 1100 -g distro mandriva
[root@localhost tmp]# id mandriva
uid=1100(mandriva) gid=2016(distro) 組=2016(distro)
3. 創建用戶mageia,其ID號是1100,家目錄為/home/linux
[root@localhost tmp]# useradd -u 1100 -d /home/linux mageia
[root@localhost tmp]# cat /etc/passwd | grep “mageia”
mageia:x:1100:1100::/home/linux:/bin/bash
4. 組用戶mageia添加密碼,密碼為mageedu;
[root@localhost tmp]# passwd mageia
更改用戶 mageia 的密碼 。
新的 密碼:
無效的密碼: 密碼少于 8 個字符
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
5. 刪除mandriva,但保留其家目錄;
[root@localhost test3]# userdel mandriva
6. 創建用戶slackware,其ID號為2002,基本組distro,附加組peguin;
[root@localhost test3]# groupadd peguin
[root@localhost test3]# useradd -u 2002 -g distro -G peguin slackware
[root@localhost test3]# id slackware
uid=2002(slackware) gid=2016(distro) 組=2016(distro),2017(peguin)
7. 修改slackware的默認shell為/bin/tcsh;
[root@localhost test3]# usermod -s /tin/tcsh slackware
[root@localhost test3]# id slackware
uid=2002(slackware) gid=2016(distro) 組=2016(distro),2017(peguin)
8. 為用戶slackware新增附加組admins;
[root@localhost ~]# groupadd admins
[root@localhost ~]# usermod -a -G admins slackware
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) 組=2016(distro),2017(peguin),2019(admins)
##########################################################################
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/90123