0%

CentOS6 系统初始化配置

CentOS6 系统初始化配置

Linux 服务器初始化配置。

以下配置适用于:CentOS 6

安装必备程序

1
2
3
4
5
6
7
cd /etc/yum.repos.d
rename .repo .bak *.repo
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
sed -i 's@^exclude@#exclude@' /etc/yum.conf
yum clean all
yum makecache
yum -y install deltarpm gcc gcc-c++ make cmake 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 libaio readline-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel libxslt-devel libevent-devel libtool libtool-ltdl bison gd-devel vim-enhanced pcre-devel zip unzip ntpdate sysstat patch bc expect rsync git lsof lrzsz

修改时区同步时间

1
2
3
4
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo "*/20 * * * * `which ntpdate` pool.ntp.org; hwclock -w > /dev/null 2>&1" >> /var/spool/cron/root;chmod 600 /var/spool/cron/root;
service crond restart

修改 console

1
2
sed -i 's@^start@#start@' /etc/init/control-alt-delete.conf
sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@' /etc/sysconfig/init

修改 sysctl 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat >> /etc/sysctl.conf << EOF
fs.file-max=65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 65536
net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_max_orphans = 262144
EOF
sysctl -p

添加系统启动资源

1
echo "ulimit -SH 65535" >> /etc/rc.local

修改 vim 配置

1
sed -i "s@alias mv=\(.*\)@alias mv=\1\nalias vi=vim@" ~/.bashrc && echo 'syntax on' >> /etc/vimrc

修改系统资源限制

1
2
3
4
5
6
7
8
sed -i '/^# End of file/,$d' /etc/security/limits.conf
cat >> /etc/security/limits.conf <<EOF
# End of file
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF

修改 history 记录数

1
sed -i 's/^HISTSIZE=.*$/HISTSIZE=100/' /etc/profile

修改用户环境信息

1
2
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'" >> ~/.bashrc
echo 'PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\$ "' >> ~/.bashrc

关闭 selinux

1
2
setenforce 0
sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config

针对 mysql 服务优化

慎用,有可能造成 MySQL OOM。

1
echo "vm.swappiness = 0" >>/etc/sysctl.conf

编辑 /etc/grub.conf 文件,在 kernel 行最后添加:

1
elevator=deadline numa=off