Linux初始化脚本

以下脚本用于linux系统的初始化脚本,可以在服务器系统安装完毕之后立即执行。脚本结合各位大牛一些参数,已经在CentOS 5下通过。
使用方法:将其复制,保存为一个shell文件,比如init.sh。将其上传到linux服务器上,执行sh init.sh。建议大家在系统安装后立即执行。
脚本代码:

#!/bin/bash
# Configure yum source
cd /tmp
wget -c http://yum.baseurl.org/download/3.4/yum-3.4.3.tar.gz
tar zxf yum-3.4.3.tar.gz
cd yum-3.4.3
./yummain.py install yum -y
cd ..
rm -rf yum-3.4.3*
sed -i ‘[email protected]^[email protected]#[email protected]‘ /etc/yum.conf
yum clean all
yum check-update
 
mv /etc/yum.repos.d/CentOS-Debuginfo.repo /etc/yum.repos.d/CentOS-Debuginfo.repo$(date +%m%d)
mv /etc/yum.repos.d/CentOS-Media.repo /etc/yum.repos.d/CentOS-Media.repo$(date +%m%d)
mv /etc/yum.repos.d/CentOS-Vault.repo /etc/yum.repos.d/CentOS-Vault.repo$(date +%m%d)
 
# Remove obsolete rpm package
if [ -z "$(cat /etc/redhat-release | grep ‘5\.‘)" ];then
       yum -y groupremove "FTP Server" "Text-based Internet" "Windows File Server" "PostgreSQL Database" "News Server" "MySQL Database" "DNS Name Server" "Web Server" "Dialup Networking Support" "Mail Server" "Ruby" "Office/Productivity" "Sound and Video" "X Window System" "X Software Development" "Printing Support" "OpenFabrics Enterprise Distribution"
else
       yum -y groupremove "FTP Server" "PostgreSQL Database client" "PostgreSQL Database server" "MySQL Database server" "MySQL Database client" "Web Server" "Office Suite and Productivity" "Ruby Support" "X Window System" "Printing client" "Desktop*"
fi
 
# Update rpm package
yum -y update
 
# Install dependencies package
yum -y install gcc gcc-c++ make autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel openldap-clients openldap-servers libxslt-devel libevent-devel ntp libtool libtool-ltdl bison gd-devel vim-enhanced pcre-devel zip unzip sendmail
 
# chkconfig 
chkconfig --list | awk ‘{print "chkconfig " $1 " off"}‘ > /tmp/chkconfiglist.sh;/bin/sh /tmp/chkconfiglist.sh;rm -rf /tmp/chkconfiglist.sh
chkconfig crond on
chkconfig irqbalance on
chkconfig network on
chkconfig sshd on
chkconfig rsyslog on #CentOS 6
chkconfig syslog on #CentOS/RHEL 5
chkconfig iptables on
chkconfig sendmail on
service sendmail restart
 
# /etc/hosts
[ "$(hostname -i)" != "127.0.0.1" ] && sed -i "[email protected]^127.0.0.1\(.*\)@127.0.0.1   `hostname` \[email protected]" /etc/hosts
 
# Close SELINUX
setenforce 0
sed -i ‘s/^SELINUX=.*$/SELINUX=disabled/‘ /etc/selinux/config
 
# initdefault
sed -i ‘s/^id:.*$/id:3:initdefault:/‘ /etc/inittab
/sbin/init q
# PS1
echo ‘PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\$ \[\e[33;40m\]"‘ >> /etc/profile
 
# Record command
sed -i ‘s/^HISTSIZE=.*$/HISTSIZE=100/‘ /etc/profile
echo "export PROMPT_COMMAND=‘{ msg=\$(history 1 | { read x y; echo \$y; });user=\$(whoami); echo \$(date \"+%Y-%m-%d %H:%M:%S\"):\$user:\`pwd\`/:\$msg ---- \$(who am i); } >> /tmp/\`hostname\`.\`whoami\`.history-timestamp‘" >> /root/.bash_profile
 
# Wrong password five times locked 180s
sed -i ‘4a auth        required      pam_tally2.so deny=5 unlock_time=180‘ /etc/pam.d/system-auth
 
# alias vi
sed "[email protected] mv=.*@alias mv=‘mv -i‘\nalias [email protected]" /root/.bashrc
echo ‘syntax on‘ >> /etc/vimrc
 
# /etc/security/limits.conf
cat >> /etc/security/limits.conf <<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
echo "ulimit -SH 65535" >> /etc/rc.local
 
# /etc/sysctl.conf
sed -i ‘s/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g‘ /etc/sysctl.conf
cat >> /etc/sysctl.conf << EOF
fs.file-max=65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 1024 65000
EOF
sysctl -p
 
if [ -z "$(cat /etc/redhat-release | grep ‘6\.‘)" ];then
       sed -i ‘s/3:2345:respawn/#3:2345:respawn/g‘ /etc/inittab
       sed -i ‘s/4:2345:respawn/#4:2345:respawn/g‘ /etc/inittab
       sed -i ‘s/5:2345:respawn/#5:2345:respawn/g‘ /etc/inittab
       sed -i ‘s/6:2345:respawn/#6:2345:respawn/g‘ /etc/inittab
       sed -i ‘s/ca::ctrlaltdel/#ca::ctrlaltdel/g‘ /etc/inittab
       sed -i ‘[email protected]=.*[email protected]="en_US.UTF-8"@g‘ /etc/sysconfig/i18n
else
       sed -i ‘[email protected]^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@‘ /etc/sysconfig/init 
       sed -i ‘[email protected]^[email protected]#[email protected]‘ /etc/init/control-alt-delete.conf
fi
/sbin/init q
 
# Set timezone
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 
# Update time
/usr/sbin/ntpdate pool.ntp.org 
echo ‘*/5 * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1‘ > /var/spool/cron/root;chmod 600 /var/spool/cron/root
/sbin/service crond restart
 
# iptables
cat > /etc/sysconfig/iptables << EOF
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:syn-flood - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
-A syn-flood -j REJECT --reject-with icmp-port-unreachable
COMMIT
EOF
/sbin/service iptables restart
source /etc/profile
 
###install tmux
mkdir tmux
cd tmux
wget -c http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz 
wget -c http://downloads.sourceforge.net/tmux/tmux-1.8.tar.gz
tar xzf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure
make && make install
cd ../
 
tar xzf tmux-1.8.tar.gz
cd tmux-1.8
CFLAGS="-I/usr/local/include" LDFLAGS="-L//usr/local/lib" ./configure
make && make install
cd ../../
rm -rf tmux
 
if [ `getconf WORD_BIT` = ‘32‘ ] && [ `getconf LONG_BIT` = ‘64‘ ] ; then
    ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
else
    ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
fi
 
###install htop
mkdir htop
cd htop
wget -c http://downloads.sourceforge.net/project/htop/htop/1.0.2/htop-1.0.2.tar.gz 
tar xzf htop-1.0.2.tar.gz
cd htop-1.0.2
./configure
make && make install
cd ../../
rm -rf htop

Linux初始化脚本

时间: 2024-08-07 04:08:00

Linux初始化脚本的相关文章

linux初始化脚本1.1

version:1.1des:主要针对redhat系列操作系统虚拟机的初始化配置更新以下内容: 添加网络检测 添加网卡名称判断 添加主机变量配置 精简yum和.bashrc配置文件生成,通过'EOF'在配置文件中直接引用变量,去掉了变量$前的\ 修复yum配置时移动命令报错不能移动bak目录本身 修复未配置网关错误 #!/bin/bash #==================================================== # Author: Mr.Song # Creat

linux系统监控工具汇总及几个小脚本 , 系统初始化脚本

重要性能监测工具:top.vmstat.w.uptime.ps.free.iostat.netstat./proc等 需要监视Linux服务器的性能?大多数Linux发行版都集成了一些监视工具.这些工具可以获取有关系统活动的信息的详细指标.通过这些工具,你可以发现产生系统性能问题可能存在原因.下面讨论的是一些最基本的命令,它涉及到系统分析和调试服务器等一些问题,如:1.    找出系统瓶颈问题.2.    磁盘 (储存) 瓶颈问题.3.    CPU和内存瓶颈问题.4.    网络瓶颈问题.#

linux系统最小化安装后的初始化脚本

作为运维人员,经常会初始化系统,系统在安装过程中基本都会选择最小化安装,这样安装好的系统里会缺少很多环境. 下面分享一个系统安装后的初始化脚本: #!/bin/bash #系统时最小化安装的,这里要安装系统的软件库yum groupinstall -y "development tools" #创建目录[ ! -d /server/tools ] && mkdir -p /server/tools[ ! -d /application ] && mkdi

Linux 初始化 init 系统,第 1 部分: sysvinit

什么是 Init 系统,init 系统的历史和现状 Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化.内核初始化的最后一步就是启动 pid 为 1 的 init 进程.这个进程是系统的第一个进程.它负责产生其他所有用户进程. init 以守护进程方式存在,是所有其他进程的祖先.init 进程非常独特,能够完成其他进程无法完成的任务. Init 系统能够定义.管理和控制 init 进程的行为.它负责组织和运行许

Linux shell脚本流程控制

博主搬家至51CTO,初来乍到,请多指教. 此次我们来通过实例解析Linux shell脚本流程控制 Linux shell脚本流程控制可分为三类:顺序执行,条件选择执行,循环执行 顺序执行:简单理解就是逐行执行脚本内容,逐行解读,逐行执行.(此处不做实例解析) 条件选择执行:可以理解为先进行某一条件的判断选择,再决定执行怎样的脚本内容.常见语句if case 条件选择语句:if if语句用法: 单分支 if 判断条件;then 条件为真的分支代码 fi 双分支 if 判断条件; then 条件

Linux链接脚本学习--lds(转)

Linux链接脚本学习--lds 一.概论 ld: GNU的链接器. 用来把一定量的目标文件跟档案文件链接在一起,并重新定位它们的数据,链接符号引用. 一般编译一个程序时,最后一步就是运行ld进行链接 每一个链接都被一个链接脚本所控制,这个脚本是用链接命令语言书写的. 二.链接脚本 链接脚本的一个主要目的是描述输入文件中的各个段(数据段,代码段,堆,栈,bss)如何被映射到输出文件中,并控制输出文件的内存排布. 链接器总是使用链接脚本的,如果你不提供,则链接器会使用一个缺省的脚本,这个脚本是被编

浅析 Linux 初始化 init 系统,第 1 部分: sysvinit

本文转载自:http://www.ibm.com/developerworks/cn/linux/1407_liuming_init1/index.html (本人学习使用添加了个人成分) 近年来,Linux 系统的 init 进程经历了两次重大的演进,传统的 sysvinit 已经淡出历史舞台,新的 init 系统 UpStart 和 systemd 各有特点,而越来越多的 Linux 发行版采纳了 systemd.本文简要介绍了这三种 init 系统的使用和原理,每个 Linux 系统管理员

LINUX SHELL脚本攻略笔记[速查]

Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述符和重定向 cat 数组和关联数组 alias date 调试脚本 函数和参数 管道 读取命令输出 read 字段分隔符和迭代器 循环 比较和测试 find xargs tr md5sum sha1sum 对目录进行校验 sort uniq tempfile split bash变量匹配切分 exp

Linux 初始化及系统启动过程

从按下电源键到进入系统的时间里,系统发生了哪些事情?经历了怎样的一个过程?是如何进入到登陆界面的?下面是整理的一个简单Linux启动过程. 1.服务器硬件要完整,CPU.内存.硬盘.主板等硬件缺一不可,并且接通电源,然后按下开机键,CPU开始自检所连接的硬件,比如鼠标键盘.显示器.内存.USB设备.硬盘等硬件,如果再自检过程中出现错误,会发出滴滴的声音提示,假如自检没有问题,则会按照BIOS设置的启动顺序,选择从光盘或光驱启动 2.假如从硬盘启动,会读取硬盘的0磁道的0扇区,供512个字节,称为