服務器系統配置初始化
背景:購買10臺服務器並已安裝Linux操作
需求:對10臺服務器進行系統配置的初始化
問題:要做哪些初始化,要配置哪些東西?
- 1.時間同步(與互聯網時間同步)
- 2.禁用selinux(selinux是一個安全機制,不會用的話,會影響做其他操作)
- 3.關閉防火牆,清空防火牆默認策略
- 4.歷史命令顯示操作時間(默認是不顯示的,爲了查看,歷史審計,可以加上這個時間)
- 5.禁止root遠程登錄(root遠程登錄是不安全的,)
- 6.禁止定時任務發送郵件(默認定時任務產生的事件都會發送給默認的用戶名的。長期積累會產生很多的垃圾文件。佔用磁盤空間)
- 7.設置最大打開文件數(默認是很少的,很多應用無法滿足)
- 8.減少Swap使用(默認當物理內存不夠時,使用Swap作爲物理內存的交換。而Swap是磁盤上的空間,讀寫性能遠不如使用物理內存。儘量不使用Swap來提高引用程序的性能)
- 9.一些系統內核參數的優化
- 10.安裝一些性能分析工具及其他
上述可以寫一個shell腳本,可以一鍵部署的
1.時間同步(與互聯網時間同步)
設置時間的時候要考慮定期的修改時間
centos是使用最廣泛的,也是開源的
cat /etc/redhat-release
看系統的版本號
創建一個存放shell腳本的目錄
mkdir shell_scripts
將文件1.sh移動到shell_scripts目錄裏面
mv 1.sh shell_scripts
安裝ntp
yum install -y ntp
同步互聯網時間
ntpdate time.windows.com(time.windows.com是一個windows時間服務器)
查看時間
date
查看所有的定時任務
crontab -l
寫入定時任務(寫腳本的時候不需要這樣的交互輸入)
crontab -e
強制保存退出
wq!
免交互輸入
(echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
- 每天1點同步,將它的錯誤和輸出都追加爲空
- ;是緊接着再執行一個命令crontab -l
- ():是將兩個作爲一組作爲一個shell命令
- |:是接收左邊的標準輸入
- crontab:將左邊的定時任務追加進crontab裏面
if ! crontab -l |grep ntpdate &>/dev/null ; then
(echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
fi
判斷一下,有沒有這個定時任務,有的話就不追加,沒有的話就追加
2.禁用selinux(selinux是一個安全機制,不會用的話,會影響做其他操作)
在/etc/selinux/config
下面將對應的參數改掉
查看一下
vi /etc/selinux/config
退出 : :wq!
默認是允許的,現在將它設置爲disable
sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
- sed 流編譯器
- -i 對文件進行修改
- /SELINUX/:匹配到這一行
- {s/enforcing/disabled/}:替換這一行中的enforcing爲disabled
3.關閉防火牆,清空防火牆默認策略
if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
systemctl stop firewalld
systemctl disable firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
service iptables stop
chkconfig iptables off
fi
- 先看一下是什麼版本,如果是7點幾的話,就把firewalld關掉
- 如果是6點幾的話,就把iptables關掉
- &>/dev/null:是將標準的錯誤和標準的輸出重定向爲空
- &>/dev/null 等同於 >/dev/null 2>&1
4.歷史命令顯示操作時間(默認是不顯示的,爲了查看,歷史審計,可以加上這個時間)
if ! grep HISTTIMEFORMAT /etc/bashrc; then
echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc
fi
>> /etc/bashrc:把追加到/etc/bashrc這個裏面,下次啓動用戶終端時,會自動加載
centos裝vim編譯器
yum -y install vim*
查看前面執行的命令
history
前面是序號,現在想要加上執行的時間
export HISTTIMEFORMAT="%F %T `whoami` "
對審計,後續查看誰操作的,都很方便
5.SSH超時時間
終端不操作情況下的超時時間。提供安全性
if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
echo "export TMOUT=600" >> /etc/profile
fi
終端不操作情況下的超時時間是10分鐘
6.禁止root遠程登錄
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
默認是允許登錄的
vi /etc/ssh/sshd_config
將#PermitRootLogin yes改成PermitRootLogin no
7.禁止定時任務發送郵件
sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
很多情況下在/var/mail/下會看到好多小文件
主要原因就是crontab -l裏面的定時任務沒有將標準輸出和錯誤輸出指向爲空
會在/var/mail/這個下面生成許多小文件
設置上面的目的就是不讓發這個郵件,讓MAILTO="",默認是當前用戶
‘s/^MAILTO=root/MAILTO=""/’:將源內容MAILTO=root,替換爲MAILTO=""
vi /etc/crontab
8.設置最大打開文件數
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535
* hard nofile 65535
EOF
fi
vi /etc/security/limits.conf
將
* soft nofile 65535
* hard nofile 65535
寫入到這個文件的最後
- domain:域,就是限制那方面的。*就是所有
- type:有軟線和硬線
- item:項目,nofile打開文件名服務
- values:數量
將
* soft nofile 65535
* hard nofile 65535
追加到/etc/security/limits.conf裏面
9.減少SWAP使用
echo "0" > /proc/sys/vm/swappiness
默認是30,設置爲0之後就儘量不用這個
10.安裝系統性能分析工具及其他
yum install gcc make autoconf vim sysstat net-tools iostat iftop iotp lrzsz -y
chomd +x 1.sh
給這個文件加上可執行權限
安裝dos2unix
yum install dos2unix -y
將windows下的腳本要在linux上執行,最好用dos2unix轉化一下。不然很容易就出現語法或者格式錯誤
下面是完整的腳本
#/bin/bash
# 設置時區並同步時間
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
if ! crontab -l |grep ntpdate &>/dev/null ; then
(echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
fi
# 禁用selinux
sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config
# 關閉防火牆
if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
systemctl stop firewalld
systemctl disable firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
service iptables stop
chkconfig iptables off
fi
# 歷史命令顯示操作時間
if ! grep HISTTIMEFORMAT /etc/bashrc; then
echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc
fi
# SSH超時時間
if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
echo "export TMOUT=600" >> /etc/profile
fi
# 禁止root遠程登錄
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
# 禁止定時任務向發送郵件
sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
# 設置最大打開文件數
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535
* hard nofile 65535
EOF
fi
# 系統內核優化
cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_fin_timeout = 20
EOF
# 減少SWAP使用
echo "0" > /proc/sys/vm/swappiness
# 安裝系統性能分析工具及其他
yum install gcc make autoconf vim sysstat net-tools iostat iftop iotp lrzsz -y