7 Essential Everyday Shell Scripts for Linux System Administration
This article presents seven practical Bash scripts that help Linux administrators quickly gather system status, back up MySQL databases, monitor services, scan network hosts, manage user passwords, and verify MySQL replication, each accompanied by clear code examples and usage instructions.
1. list_sys_status.sh – Display basic system network information
This script prints the host name, IP address, subnet mask, gateway, and DNS server of the machine by parsing ifconfig and /etc/resolv.conf, then echoes each value with a Chinese label.
#!/bin/bash
IP=`ifconfig eth0 | head -2 | tail -1 | awk '{print $2}' | awk -F":" '{print $2}'`
ZW=`ifconfig eth0 | head -2 | tail -1 | awk '{print $3}' | awk -F":" '{print $2}'`
GW=`route -n | tail -1 | awk '{print $2}'`
HN=`hostname`
DNS=`head -1 /etc/resolv.conf | awk '{print $2}'`
echo '此机IP地址是' $IP
echo '此机子网掩码是' $ZW
echo '此机网关是' $GW
echo '此机主机名是' $HN
echo '此机DNS是' $DNS2. mysqlbak.sh – Backup MySQL data directory
The script records the current date and the size of /var/lib/mysql, writes this information to /tmp/dbinfo.txt, creates a tar.gz archive of the MySQL directory together with the info file, and schedules itself via crontab to run every three hours.
#!/bin/bash
DAY=`date +%Y%m%d`
SIZE=`du -sh /var/lib/mysql`
echo "Date: $DAY" >> /tmp/dbinfo.txt
echo "Data Size: $SIZE" >> /tmp/dbinfo.txt
cd /opt/dbbak &>/dev/null || mkdir /opt/dbbak
tar zcf /opt/dbbak/mysqlbak-${DAY}.tar.gz /var/lib/mysql /tmp/dbinfo.txt &>/dev/null
rm -f /tmp/dbinfo.txt
crontab -e
55 23 */3 * * /opt/dbbak/dbbak.sh3. Weekly full backup of the webdb database
Scheduled at 23:30 every Sunday, this script dumps the webdb database to /mysqlbak with a date‑based filename, flushes MySQL logs, archives the binary logs, and stores them in /mysqlbinlog.tar.gz.
#!/bin/bash
day=`date +%F`
mysqldump -hlocalhost -uroot -p123 webdb > /mysqlbak/webdb-${day}.sql
mysql -hlocalhost -uroot -p -e "flush logs"
tar zcf /mysqlbinlog.tar.gz /var/lib/mysql/mysqld-bin.0*
# crontab entry: 30 23 * * 7 /shell/webdb.sh4. very.ser.sh – Check the status of a specific service
The script accepts a service name (limited to vsftpd, httpd, sshd, crond, mysqld), verifies whether it is running, prints "service is running" or starts the service if it is stopped. Two implementations are provided: one using read and another using positional parameters.
#!/bin/bash
read -p "请输入你的服务名:" service
if [ $service != 'crond' -a $service != 'httpd' -a $service != 'sshd' -a $service != 'mysqld' -a $service != 'vsftpd' ]; then
echo "只能够检查'vsftpd,httpd,crond,mysqld,sshd'"
exit 5
fi
service $service status &>/dev/null
if [ $? -eq 0 ]; then
echo "服务在线"
else
service $service start
fi if [ -z $1 ]; then
echo "You must specify a servername!"
echo "Usage: `basename $0` servername"
exit 2
fi
if [ $1 == "crond" ] || [ $1 == "mysql" ] || [ $1 == "sshd" ] || [ $1 == "httpd" ] || [ $1 == "vsftpd" ]; then
service $1 status &>/dev/null
if [ $? -eq 0 ]; then
echo "$1 is running"
else
service $1 start
fi
else
echo "Usage:`basename $0` server name"
echo "But only check for vsftpd httpd sshd crond mysqld" && exit 2
fi5. pc_noline.sh – List online hosts in a subnet
This script pings IPs in the 192.168.1.10‑12 range, prints the addresses of reachable hosts, counts unreachable hosts, and logs the IPs and timestamps of failures to /tmp/ip.txt.
#!/bin/bash
ip=192.168.1.
j=0
for i in `seq 10 12`
do
ping -c 3 $ip$i &>/dev/null
if [ $? -eq 0 ]; then
echo "在线的主机有:$ip$i"
else
let j++
echo $ip$i >> /tmp/ip.txt
date >> /tmp/ip.txt
fi
done
echo "不在线的主机台数有 $j"6. Simple forum password‑reset script
Interactive script that asks for the website directory, database credentials, and a username, then verifies the user exists in the forum’s MySQL table, prompts for a new password, hashes it with openssl md5, updates the password field, and flushes privileges.
#!/bin/bash
End=ucenter_members
MYsql=/home/lnmp/mysql/bin/mysql
read -p "Enter a website directory : " webdir
WebPath=/home/WebSer/$webdir/config
read -p "Enter dbuser name : " dbuser
read -sp "Enter dbuser password : " dbpass
read -p "Enter db name : " dbname
read -p "Enter db tablepre : " dbtablepre
Globalphp=`grep "tablepre*" $WebPath/config_global.php |cut -d "'" -f8`
Ucenterphp=`grep "UC_DBTABLEPRE*" $WebPath/config_ucenter.php |cut -d '.' -f2 | awk -F "'" '{print $1}'`
if [ $dbtablepre == $Globalphp ] && [ $dbtablepre == $Ucenterphp ]; then
Start=$dbtablepre
Pre=`echo $Start$End`
read -p "Enter you name : " userset
Result=`$MYsql -u$dbuser -p$dbpass $dbname -e "select username from $Pre where username='$userset'\G" | cut -d ' ' -f2 | tail -1`
if [ $userset == $Result ]; then
read -p "Enter your password : " userpass
passnew=`echo -n $userpass | openssl md5 | cut -d ' ' -f2`
$MYsql -u$dbuser -p$dbpass $dbname -e "update $Pre set password='$passnew' where username='$userset';"
$MYsql -u$dbuser -p$dbpass $dbname -e "flush privileges;"
else
echo "$userset is not right user!"
exit 1
fi
else
exit 2
fi7. slave_status.sh – Verify MySQL master‑slave health
The script checks whether the MySQL service is running, whether the network to the master is reachable, whether a connection can be made with the provided credentials, and whether both Slave_IO_Running and Slave_SQL_Running flags are set to YES, reporting success or failure for each step.
#!/bin/bash
netstat -tulnp | grep :3306 &>/dev/null
if [ $? -eq 0 ]; then
echo "服务正在运行"
else
service mysqld start
fi
ping -c 3 192.168.1.100 &>/dev/null
if [ $? -eq 0 ]; then
echo "网络连接正常"
else
echo "网络连接失败"
fi
mysql -h192.168.1.100 -uroot -p123456 &>/dev/null
if [ $? -eq 0 ]; then
echo "数据库连接成功"
else
echo "数据库连接失败"
fi
IO=$(mysql -uroot -p123 -e "show slave status\G" | grep Slave_IO_Running | awk '{print $2}')
SQL=$(mysql -uroot -p123 -e "show slave status\G" | grep Slave_SQL_Running | awk '{print $2}')
if [ "$IO" == "Yes" ] && [ "$SQL" == "Yes" ]; then
echo "IO and SQL 连接成功"
else
echo "IO线程和SQL线程连接失败"
fiThese seven scripts provide a quick toolbox for routine Linux system administration tasks such as status reporting, data backup, service monitoring, network scanning, and MySQL maintenance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
