標籤:shell
1.關閉不必要的服務
ntsysv chkconfig
必須開啟的服務有:crond/messagebus/network/iptables/sshd/syslog/sysstat/snmpd
關閉不必要的服務指令碼:
for i in `chkconfig --list |awk ‘/3:on/ {print $1}‘|grep -v "crond\|messagebus\|sshd\|iptables\|network\|syslog\|snmpd\|sysstat"`;
do chkconfig --level 345 ${i} off;
done
開啟需要的服務指令碼:
for i in "crond" "messagebus" "iptables" "network" "snmpd" "sshd" "syslog" "sysstat";
do chkconfig --level 345 ${i} on;
done
3.檢查服務的基本配置
dmidecode | grep "Product" | head -n 1 檢查伺服器型號
cat /proc/cpuinfo | grep name | cut -f2 -d: |uniq -c 檢查CUP訊號
free -m | grep Mem | awk ‘{print $2}‘
ifconfig | grep "inet addr" | awk -F: ‘{print $2}‘| awk ‘{print $1}‘
4.系統最佳化修改/etc/sysctl.conf檔案,sysctl -p 是修改生效;
關閉不必要的服務使用ntsysv;查看自啟動服務chkconfig;
5.修改系統的檔案控制代碼書/etc/security/limits.conf 確認控制代碼允許資料ulimit -n
6.修改sshd監聽連接埠,並禁止密碼驗證登入設定檔/etc/ssh/sshd_config
7.帳號安全,清除除root使用者之外的其他使用者的登入許可權
#!/bin/bash
for k in `cat /etc/passwd | grep -i "\/bin\/bash" | grep -v "root" | cut -d: -f1`;
do
usermod -s /sbin/nologin ${k};
done
更改root使用者密碼:
echo "xxxxxxxx" >/root/tmp.txt “xxxxxxxx”為設定的密碼
passwd root --stdin </root/tmp.txt ;rm –f /root/tmp.txt
8.查看系統的基本資料指令碼
#!/bin/bash
dmidecode | grep "Product" | head -n 1
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
free -m | grep Mem | awk ‘{print $2}‘
ifconfig | grep "inet addr" | awk -F: ‘{print $2}‘| awk ‘{print $1}‘
route -n
cat /etc/issue | head -n 1
uname -a
df -h | awk ‘{print $1,$2}‘
9.載入ext4模組
modprobe ext4
lsmod | grep ext4
yum -y install e4fsprogs
10.統計登陸使用者數
uptime | cut -d‘,‘ -f 2 | sed ‘s/^[[:space:]]*//g‘
who | wc -l
11.添加使用者指令碼
#!/bin/bash
id $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "$1 is exsit!"
else
useradd $1
echo "123456" | passwd --stdin "$1" >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "$1 create seccuss!"
fi
fi
12.sed刪除空行
sed ‘/^$/d‘ file
13.sed刪除行首的空格
sed -i ‘s/^[[:space:]]*//‘ filename
sed -i ‘s/[[:space:]]*//g‘ filename ##刪除所有空格
sed s/[[:space:]]//g filename
#sed ‘s/^[ ]*//g‘ file
#sed ‘s/^ *//g‘ file
#sed ‘s/^[[:space:]]*//g‘ file
14.#刪除所有行尾Null 字元
sed -i ‘s/[[:blank:]]+$//‘ filename
15.刪除行的首數字
sed -i ‘s/^[0-9]*//g‘ filename
sed ‘s#^[0-9]{1,} ##g‘ filename
sed ‘s#^[0-9]* ##g‘ filename
本文出自 “咚咚的部落格” 部落格,謝絕轉載!
shell基礎練習筆記