Linux基础实践

丈夫欲遂平生志,一载寒窗一举汤。这篇文章主要讲述Linux基础实践相关的知识,希望能为你提供帮助。
1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

## 用命令 grep "/sbin/nologin" /etc/passwd 列出包含/sbin/nologin的行,并显示 [root@centos8301]# grep"/sbin/nologin" /etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pipewire:x:996:992:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin libstoragemgmt:x:995:989:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin unbound:x:994:988:Unbound DNS resolver:/etc/unbound:/sbin/nologin gluster:x:993:987:GlusterFS daemons:/run/gluster:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin dnsmasq:x:985:985:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin sssd:x:984:984:User for sssd:/:/sbin/nologin cockpit-ws:x:983:982:User for cockpit web service:/nonexisting:/sbin/nologin cockpit-wsinstance:x:982:981:User for cockpit-ws instances:/nonexisting:/sbin/nologin chrony:x:981:980::/var/lib/chrony:/sbin/nologin colord:x:980:979:User for colord:/var/lib/colord:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin setroubleshoot:x:979:978::/var/lib/setroubleshoot:/sbin/nologin flatpak:x:978:977:User for flatpak system helper:/:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin clevis:x:977:976:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin gnome-initial-setup:x:976:975::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin slackware:x:2002:2019::/home/slackware:/sbin/nologin [root@centos8301]# ##用 -v, --invert-match ,查出不包含/sbin/nologin的行,并输出 [root@centos8301]# grep -v "/sbin/nologin" /etc/passwd root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt summer:x:1000:1000:summer:/home/summer:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash user1:x:2003:2003::/home/user1:/bin/bash user2:x:2004:2004::/home/user2:/bin/bash user3:x:2005:2005::/home/user3:/bin/bash sabrina:x:2006:2006::/home/sabrina:/bin/bash mage:x:2007:2007::/home/mage:/bin/bash [root@centos8301]# ## 用cut命令以:分号为分隔符,取第1列,即用户名 [root@centos8301]# grep -v "/sbin/nologin" /etc/passwd | cut -d: -f1 root sync shutdown halt summer mageia user1 user2 user3 sabrina mage [root@centos8301]#

2、查出用户UID最大值的用户名、UID及shell类型
[root@centos8301]# cat /etc/passwd | sort -t: -k3 -n | tail -1 | cut -d: -f1,3,7 nobody:65534:/sbin/nologin [root@centos8301]# [root@centos8301]# getent passwd | sort -t: -k3 -n | tail -1 | cut -d: -f1,3,7 nobody:65534:/sbin/nologin [root@centos8301]###思路: 1.只有在passwd才能查到UID、用户名及shell类型,命令:getent passwd 或 cat /etc/passwd 2.对UID先排序,用sort,以:为分隔符分列 -t:,对第3列UID -k3,数字排序 -n 3.tail -1取出UID最大值的UID 4.用cut分别把第1列用户名、第3列UID、第7列shell类型列出来

## cat命令先查出 /etc/passwd 文件的第1/3/7列的内容,以:为分隔符 [root@centos8301]# cat /etc/passwd| cut -d: -f1,3,7 root:0:/bin/bash bin:1:/sbin/nologin daemon:2:/sbin/nologin adm:3:/sbin/nologin lp:4:/sbin/nologin sync:5:/bin/sync shutdown:6:/sbin/shutdown halt:7:/sbin/halt mail:8:/sbin/nologin operator:11:/sbin/nologin games:12:/sbin/nologin ftp:14:/sbin/nologin nobody:65534:/sbin/nologin dbus:81:/sbin/nologin systemd-coredump:999:/sbin/nologin systemd-resolve:193:/sbin/nologin tss:59:/sbin/nologin polkitd:998:/sbin/nologin geoclue:997:/sbin/nologin rtkit:172:/sbin/nologin pipewire:996:/sbin/nologin pulse:171:/sbin/nologin libstoragemgmt:995:/sbin/nologin qemu:107:/sbin/nologin usbmuxd:113:/sbin/nologin unbound:994:/sbin/nologin gluster:993:/sbin/nologin rpc:32:/sbin/nologin avahi:70:/sbin/nologin saslauth:992:/sbin/nologin dnsmasq:985:/sbin/nologin radvd:75:/sbin/nologin sssd:984:/sbin/nologin cockpit-ws:983:/sbin/nologin cockpit-wsinstance:982:/sbin/nologin chrony:981:/sbin/nologin colord:980:/sbin/nologin rpcuser:29:/sbin/nologin setroubleshoot:979:/sbin/nologin flatpak:978:/sbin/nologin gdm:42:/sbin/nologin clevis:977:/sbin/nologin gnome-initial-setup:976:/sbin/nologin sshd:74:/sbin/nologin tcpdump:72:/sbin/nologin summer:1000:/bin/bash postfix:89:/sbin/nologin mageia:1100:/bin/bash slackware:2002:/sbin/nologin user1:2003:/bin/bash user2:2004:/bin/bash user3:2005:/bin/bash sabrina:2006:/bin/bash mage:2007:/bin/bash##sort 命令对UID进行排序 [root@centos8301]# cat /etc/passwd| cut -d: -f1,3,7 | sort -t: -k2 -n root:0:/bin/bash bin:1:/sbin/nologin daemon:2:/sbin/nologin adm:3:/sbin/nologin lp:4:/sbin/nologin sync:5:/bin/sync shutdown:6:/sbin/shutdown halt:7:/sbin/halt mail:8:/sbin/nologin operator:11:/sbin/nologin games:12:/sbin/nologin ftp:14:/sbin/nologin rpcuser:29:/sbin/nologin rpc:32:/sbin/nologin gdm:42:/sbin/nologin tss:59:/sbin/nologin avahi:70:/sbin/nologin tcpdump:72:/sbin/nologin sshd:74:/sbin/nologin radvd:75:/sbin/nologin dbus:81:/sbin/nologin postfix:89:/sbin/nologin qemu:107:/sbin/nologin usbmuxd:113:/sbin/nologin pulse:171:/sbin/nologin rtkit:172:/sbin/nologin systemd-resolve:193:/sbin/nologin gnome-initial-setup:976:/sbin/nologin clevis:977:/sbin/nologin flatpak:978:/sbin/nologin setroubleshoot:979:/sbin/nologin colord:980:/sbin/nologin chrony:981:/sbin/nologin cockpit-wsinstance:982:/sbin/nologin cockpit-ws:983:/sbin/nologin sssd:984:/sbin/nologin dnsmasq:985:/sbin/nologin saslauth:992:/sbin/nologin gluster:993:/sbin/nologin unbound:994:/sbin/nologin libstoragemgmt:995:/sbin/nologin pipewire:996:/sbin/nologin geoclue:997:/sbin/nologin polkitd:998:/sbin/nologin systemd-coredump:999:/sbin/nologin summer:1000:/bin/bash mageia:1100:/bin/bash slackware:2002:/sbin/nologin user1:2003:/bin/bash user2:2004:/bin/bash user3:2005:/bin/bash sabrina:2006:/bin/bash mage:2007:/bin/bash nobody:65534:/sbin/nologin [root@centos8301]# ##tail取出最后一行,即为UID最大 [root@centos8301]# cat /etc/passwd| cut -d: -f1,3,7 | sort -t: -k2 -n | tail -n1 nobody:65534:/sbin/nologin

## 其他命令摘录 [root@centos8301]# cat /etc/passwd | cut -d: -f3 | sort -rn | head -1 65534 [root@centos8301]# grep `cat /etc/passwd | cut -d: -f3 | sort -rn | head -1` /etc/passwd | cut -d: -f1,3,7 nobody:65534:/sbin/nologin [root@centos8301]# [root@centos8301]# cat /etc/passwd | grep 65534 nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin [root@centos8301]# cat /etc/passwd | awk -F: -v u="" -v maxid=0 { if($3> maxid) {maxid=$3; u=$1"--"$3"--"$7} } END{print u} nobody--65534--/sbin/nologin [root@centos8301]#

3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@centos8301]# ss -tnH ESTAB096192.168.250.101:22192.168.250.250:61082 ESTAB00192.168.250.101:22192.168.250.250:61198 ESTAB00192.168.250.101:2210.0.96.253:56072 CLOSE-WAIT320192.168.250.101:597148.43.85.13:443 ESTAB00192.168.250.101:22192.168.250.201:48570 [root@centos8301]# ss -tnH| awk -F +|: {print $6} | sort | uniq -c | sort -nr 2 192.168.250.250 1 8.43.85.13 1 192.168.250.201 1 10.0.96.253 [root@centos8301]#

[root@centos8301-shone-cn ~]# ss -tnH ESTAB00192.168.250.101:22192.168.250.250:61275 ESTAB096192.168.250.101:2210.0.96.253:56893 ESTAB00192.168.250.101:22192.168.250.250:61361 ESTAB00192.168.250.101:22192.168.250.250:61359 ESTAB00192.168.250.101:22192.168.250.250:61360 [root@centos8301-shone-cn ~]# ss -tnH| awk -F +|: {print $6} | sort | uniq -c | sort -nr 4 192.168.250.250 1 10.0.96.253 [root@centos8301-shone-cn ~]#

[root@centos8301]#netstat -nt | grep "tcp"|tr -s " " ":"|cut -d: -f6 |sort |uniq -c 1 10.0.96.253 4 192.168.250.250 [root@centos8301]#

[root@centos8301]#w -h | tr -s " " | cut -d " " -f3 | sort | uniq -c | sort -nr 4 192.168.250.250 1 10.0.96.253 [root@centos8301]#

4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
## 脚本编写可以定义一个 .vimrc 文件用于vim的统一格式,放在/home/xxx目录下面供自己使用 [root@centos8301]# vim disk.sh #!/bin/bash # #******************************************************************** #Author:Wu-Dong-Wu-Xia #QQ:XXXXXXXXXX #Date:2021-12-05 #FileName:disk.sh #URL:http://www.XXXX.cn #Description:The test script #Copyright (C):2021 All rights reserved #******************************************************************** MAX_SPACE_OCCUPY=$(df -lh | grep / | tr -s% |cut -d"%" -f5 | sort -nr | head -n 1) echo Disk-Space utilization-MAX=$MAX_SPACE_OCCUPY% [root@centos8301]# # 脚本的解读 # dh -lh 获取易读的磁盘分区信息 # grep / 将第一行过滤掉 # tr -s% 将连续的空格" "替换为百分号"%" # cut 以百分号"%"作为分隔符,取出第5列,即空间利用率 # sort -nr 对空间利用率按数字从大到小排序 # head 取出第一行,即空间利用率最大的值## 列出磁盘信息,用于比对结果 [root@centos8301-shone-cn ~]# df -lh FilesystemSizeUsed Avail Use% Mounted on devtmpfs1.8G01.8G0% /dev tmpfs1.9G01.9G0% /dev/shm tmpfs1.9G9.3M1.9G1% /run tmpfs1.9G01.9G0% /sys/fs/cgroup /dev/sda2100G6.0G94G6% / /dev/sda1200M7.3M193M4% /boot/efi /dev/sda450G390M50G1% /data tmpfs374M1.2M373M1% /run/user/42 tmpfs374M0374M0% /run/user/0 [root@centos8301-shone-cn ~]# ## 赋予脚本文件的执行权限 [root@centos8301]# chmod 755 disk.sh [root@centos8301]# lldisk.sh -rwxr-xr-x. 1 root root 506 Dec5 23:22 disk.sh [root@centos8301]# ## 在bash下运行 disk.sh 脚本 [root@centos8301]# bash disk.sh 6 [root@centos8301]###验证脚本的信息 [root@centos8301]# cat disk.sh #!/bin/bash # #******************************************************************** #Author:Wu-Dong-Wu-Xia #QQ:1050572574 #Date:2021-12-05 #FileName:disk.sh #URL:http://www.shone.cn #Description:The test script #Copyright (C):2021 All rights reserved #******************************************************************** MAX_SPACE_OCCUPY=$(df -h | grep / | tr -s% |cut -d"%" -f5 | sort -nr | head -n 1) echo Disk-Space utilization-MAX=$MAX_SPACE_OCCUPY% [root@centos8301]#

【Linux基础实践】5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
## 编写脚本 [root@centos8301-shone-cn ~]# vim systeminfo.sh [root@centos8301-shone-cn ~]# cat systeminfo.sh #!/bin/bash # ############################################################################ #Author:Wu-Dong-Wu-Xia #QQ:XXXXXXXXXXX #Date:2021-12-05 #FileName:systeminfo.sh #URL:http://www.XXXXX.cn #Description:The test script #Copyright (C):2021 All rights reserved ############################################################################BLUE="\\e[34m" END="\\e[0m" WIDTH=90 MID=$(($WIDTH / 2)) SEPAR_RECORD=$( for i in `seq 1 $WIDTH`; do echo -n "-"; done) SEPAR_FILE="|" MID_SEPAR="\\033[${MID}G${SEPAR_FILE}" LEFT_SEPAR="\\033[1G${SEPAR_FILE}" RIGHT_SEPAR="\\033[${WIDTH}G${SEPAR_FILE}"HOST=$(hostname) IP=$(ip a| grep global | awk {print $2}) OS_VERSION=$(cat /etc/redhat-release) KERNEL_VERSION=$(uname -r) CPU_TYPE=$(cat /proc/cpuinfo| grep model name | cut -d":" -f2 | head -n 1) MEMORY_SIZE=$(cat /proc/meminfo | grep MemTotal | tr -s| cut -d":" -f2) DISK_SIZE=$(lsblk | grep disk | tr -s| cut -d--output-delimiter ":"-f1,4)echo $SEPAR_RECORD# echo hostname ATTR="hostname" SKIP_ATTR=$(( MID/2 -${#ATTR}/2 )) SKIP_VALUE=https://www.songbingjia.com/android/$(( MID/2 - ${#HOST}/2 + MID)) echo -en"\\033[${SKIP_ATTR}G" & & echo -en "${ATTR}" echo -en "\\033[${SKIP_VALUE}G" & & echo -en "${BLUE}${HOST}${END}" echo -e "${LEFT_SEPAR}${MID_SEPAR}${RIGHT_SEPAR}" echo $SEPAR_RECORD# echo IP ATTR="IP" IP_NUM=$( echo $IP | awk "END{print NF}") LINE=1 SKIP_ATTR=$(( MID/2 -${#ATTR}/2 )) for i in $IP; do SKIP_VALUE=https://www.songbingjia.com/android/$(( MID/2 - ${#i}/2 + MID)) if (( LINE==(IP_NUM + 1)/2 )); then echo -en"\\033[${SKIP_ATTR}G" & & echo -en "${ATTR}" fi (( LINE++ )) echo -en "\\033[${SKIP_VALUE}G" & & echo -en "${BLUE}${i}${END}" echo -e "${LEFT_SEPAR}${MID_SEPAR}${RIGHT_SEPAR}"done echo $SEPAR_RECORD# echo os version ATTR="os version" SKIP_ATTR=$(( MID/2 -${#ATTR}/2 )) SKIP_VALUE=https://www.songbingjia.com/android/$(( MID/2 - ${#OS_VERSION}/2 + MID)) echo -en"\\033[${SKIP_ATTR}G" & & echo -en "${ATTR}" echo -en "\\033[${SKIP_VALUE}G" & & echo -en "${BLUE}${OS_VERSION}${END}" echo -e "${LEFT_SEPAR}${MID_SEPAR}${RIGHT_SEPAR}" echo $SEPAR_RECORD# echo kernel version ATTR="kernel version" SKIP_ATTR=$(( MID/2 -${#ATTR}/2 )) SKIP_VALUE=https://www.songbingjia.com/android/$(( MID/2 - ${#KERNEL_VERSION}/2 + MID)) echo -en"\\033[${SKIP_ATTR}G" & & echo -en "${ATTR}" echo -en "\\033[${SKIP_VALUE}G" & & echo -en "${BLUE}${KERNEL_VERSION}${END}" echo -e "${LEFT_SEPAR}${MID_SEPAR}${RIGHT_SEPAR}" echo $SEPAR_RECORD# echo cpu type ATTR="cpu type" SKIP_ATTR=$(( MID/2 -${#ATTR}/2 )) SKIP_VALUE=https://www.songbingjia.com/android/$(( MID/2 - ${#CPU_TYPE}/2 + MID)) echo -en"\\033[${SKIP_ATTR}G" & & echo -en "${ATTR}" echo -en "\\033[${SKIP_VALUE}G" & & echo -en "${BLUE}${CPU_TYPE}${END}" echo -e "${LEFT_SEPAR}${MID_SEPAR}${RIGHT_SEPAR}" echo $SEPAR_RECORD# echo memory size ATTR="memory size" SKIP_ATTR=$(( MID/2 -${#ATTR}/2 )) SKIP_VALUE=https://www.songbingjia.com/android/$(( MID/2 - ${#MEMORY_SIZE}/2 + MID)) echo -en"\\033[${SKIP_ATTR}G" & & echo -en "${ATTR}" echo -en "\\033[${SKIP_VALUE}G" & & echo -en "${BLUE}${MEMORY_SIZE}${END}" echo -e "${LEFT_SEPAR}${MID_SEPAR}${RIGHT_SEPAR}" echo $SEPAR_RECORD# echo disk size ATTR="disk size" DISK_NUM=$( echo $DISK_SIZE | awk "END{print NF}") LINE=1 SKIP_ATTR=$(( MID/2 -${#ATTR}/2 )) for i in $DISK_SIZE; do SKIP_VALUE=https://www.songbingjia.com/android/$(( MID/2 - ${#i}/2 + MID)) if (( LINE==(DISK_NUM + 1)/2 )); then echo -en"\\033[${SKIP_ATTR}G" & & echo -en "${ATTR}" fi (( LINE++ )) echo -en "\\033[${SKIP_VALUE}G" & & echo -en "${BLUE}${i}${END}" echo -e "${LEFT_SEPAR}${MID_SEPAR}${RIGHT_SEPAR}"done echo $SEPAR_RECORD## 授予脚本的运行权限 [root@centos8301-shone-cn ~]# chmod 755 systeminfo.sh ## 运行脚本 [root@centos8301-shone-cn ~]# bash systeminfo.sh ------------------------------------------------------------------------------------------ |hostname|centos8301-shone-cn| ------------------------------------------------------------------------------------------ |IP|192.168.250.101/24| ||192.168.122.1/24| ------------------------------------------------------------------------------------------ |os version|CentOS Linux release 8.4.2105| ------------------------------------------------------------------------------------------ |kernel version|4.18.0-305.3.1.el8.x86_64| ------------------------------------------------------------------------------------------ |cpu type| Intel(R) Xeon(R) CPU E7-4809 v4 @ 2.10GHz| ------------------------------------------------------------------------------------------ |memory size|3825304 kB| ------------------------------------------------------------------------------------------ |disk size|sda:200G| ------------------------------------------------------------------------------------------ [root@centos8301-shone-cn ~]#


    推荐阅读