linux系統最小化安裝後的初始化指令碼

2022-05-10 15:39:11 字數 4807 閱讀 6145

作為運維人員,經常會初始化系統,系統在安裝過程中基本都會選擇最小化安裝,這樣安裝好的系統裡會缺少很多環境。

下面分享乙個系統安裝後的初始化指令碼:

#!/bin/bash

#系統時最小化安裝的,這裡要安裝系統的軟體庫

yum groupinstall -y "development tools"

#建立目錄

[ ! -d /server/tools ] && mkdir -p /server/tools

[ ! -d /data ] && mkdir -p /data

[ ! -d /server/backup ] && mkdir -p /server/backup

[ ! -d /delete ] && mkdir -p /delete

#每週六凌晨1點0分更新伺服器系統時間

echo "############### auto update time ###############" >> /var/spool/cron/root

echo "00 01 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1" >> /var/spool/cron/root

[ `grep ntpdate /var/spool/cron/root |wc -l` -ne 0 ] && action "uptime set" /bin/true || action "uptime set" /bin/false

#配置yum源

/bin/mv /etc/yum.repos.d/centos-base.repo /etc/yum.repos.d/centos-base.repo.bak

/bin/cp /etc/yum.repos.d/centos6-base-163.repo /etc/yum.repos.d/centos-base.repo

[ `grep 163.com /etc/yum.repos.d/centos-base.repo | wc -l` -ne 0 ] && action "yum set" /bin/true || action "yum set" /bin/false

#關閉selinux及iptables

/bin/cp /etc/selinux/config /etc/selinux/config.bak

sed -i 's/selinux=enforcing/selinux=disabled/' /etc/selinux/config 2>&1

/etc/init.d/iptables stop >/dev/null

chkconfig iptables off >/dev/null

[ `chkconfig --list |grep iptables|grep 3:on|wc -l` -eq 0 -a `grep "selinux=enforcing" /etc/selinux/config|wc -l` -eq 0 ] && action "iptables and selinux close" /bin/true || action "iptables and selinux close" /bin/false

#調整檔案描述符數量

/bin/cp /etc/security/limits.conf /etc/security/limits.conf.bak

echo '* - nofile 65535'>>/etc/security/limits.conf

[ `tail -1 /etc/security/limits.conf|grep 65535|wc -l` -eq 1 ] && action "limit set" /bin/true || action "limit set" /bin/false

#更改字符集

/bin/cp /etc/sysconfig/i18n /etc/sysconfig/i18n.bak

echo 'lang="en_us.utf-8"' >/etc/sysconfig/i18n

#定時清理/var/spool/clientmqueue/目錄下的垃圾檔案,防止inodes節點被佔滿

##建立指令碼目錄

[ ! -d /server/scripts ] && mkdir -p /server/scripts

if [ `rpm -qa sendmail |wc -l` -ne 0 ];then

##建立查詢刪除指令碼

echo >/server/scripts/del.sh&1

eof##新增到定時任務,每週一凌晨0點0分執行

echo '################ clean /var/spool/clientmqueue/ ################' >>/var/spool/cron/root

echo '00 00 * * 1 /bin/sh /server/scripts/del.sh >/dev/null 2>&1' >>/var/spool/cron/root

[ "$?" -eq 0 ] && action "clean /var/spool/clientmqueue/ set" /bin/true || action "clean /var/spool/clientmqueue/ set" /bin/false

else

action "service sendmail is not installed,do not need set" /bin/false

fi#精簡開機自啟動服務(只啟動crond,sshd,network,syslog)

##篩選出所有在執行級別3自啟動的服務並關閉自啟動

for cgt in `chkconfig --list | grep 3:on | awk ''`;do chkconfig --level 3 $cgt off;done

##僅設定crond,sshd,network,syslog自啟動

for cgt in ;do chkconfig --level 3 $cgt on;done

flag=0

[ `chkconfig --list|grep 3:on|wc -l` -eq 4 ] && action "auto_start services set" /bin/true || action "auto_start services set" /bin/false

#核心引數優化

[ -f /etc/sysctl.conf.bak ] && /bin/cp /etc/sysctl.conf.bak /etc/sysctl.conf.bak.$(date +%f-%h%m%s) ||/bin/cp /etc/sysctl.conf /etc/sysctl.conf.bak

cat >> /etc/sysctl.conf &1

[ `grep "net.ipv4.tcp_max_orphans = 16384" /etc/sysctl.conf|wc -l` -ne 0 ] && action "kernel set" /bin/true || action "kernel set" /bin/false

#更改預設的ssh服務埠,禁止root使用者遠端連線,禁止空密碼連線

/bin/cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

#sed -i 's/\#port 22/port 52113/' /etc/ssh/sshd_config

sed -i 's/\#permitrootlogin yes/permitrootlogin no/' /etc/ssh/sshd_config

sed -i 's/\#permitemptypasswords no/permitemptypasswords no/' /etc/ssh/sshd_config

sed -i 's/\#usedns yes/usedns no/' /etc/ssh/sshd_config

#[ `grep "port 52113" /etc/ssh/sshd_config |wc -l` -ne 0 -a `grep "permitrootlogin no" /etc/ssh/sshd_config|wc -l` -ne 0 -a `grep "permitemptypasswords no" /etc/ssh/sshd_config|wc -l` -ne 0 -a `grep "usedns no" /etc/ssh/sshd_config|wc -l` -ne 0 ] &&action "ssh set" /bin/true || action "ssh set" /bin/false

[ `grep "permitrootlogin no" /etc/ssh/sshd_config|wc -l` -ne 0 -a `grep "permitemptypasswords no" /etc/ssh/sshd_config|wc -l` -ne 0 -a `grep "usedns no" /etc/ssh/sshd_config|wc -l` -ne 0 ] &&action "ssh set" /bin/true || action "ssh set" /bin/false

#鎖定關鍵系統檔案

chattr +ai /etc/passwd

chattr +ai /etc/shadow

chattr +ai /etc/group

chattr +ai /etc/gshadow

chattr +ai /etc/inittab

#清空/etc/issue,去除系統及核心版本登陸前的螢幕顯示

/bin/cp /etc/issue /etc/issue.bak

>/etc/issue

[ `cat /etc/issue|wc -l` -eq 0 ] && action "/etc/issue set" /bin/true || action "/etc/issue set" /bin/false

提取密碼:d2xr

Linux最小化安裝後優化

1.配置網絡卡 bin bash 修改網絡卡名為eth0 eth1形式 sed i linux16.s net.ifnames 0 biosdevname 0 g boot grub2 grub.cfg 刪除原網絡卡名對應配置檔案 rm rf etc sysconfig network script...

最小化安裝Linux系統後出現的問題

一 使用的系統是centos系統,剛搭建好的cent 最小安裝模式是沒有ifconfig命令的。root bogon ifconfig bash ifconfig 未找到命令 1 修改網路配置檔案 root bogon network scripts cd etc sysconfig network...

centos最小化安裝系統後的基本調優及安全設定

清理開機自動啟動的服務 顯示出所有服務的所有執行級別的啟動狀態 chkconfig list 停止所有在執行級別3上開機啟動的服務 for oldboy in chkconfig list grep 3 on awk do chkconfig level 3 oldboy off done 在開啟常...