前言 一般不购买RHEL/SLES订阅支持,通常会选择CentOS/Debian/Ubuntu等作为开源替代方案,由于工作的原因主要涉及RHEL,很多较新的技术我都会基于CentOS做实验,这里记录一些基础但实用的配置,生产环境不要直接生搬硬套。
了解CentOS不同版本之间的差异,快速构建实验学习环境
更新历史 2018年05月20日 - 初稿
阅读原文 - https://wsgzao.github.io/post/centos/
扩展阅读
CentOS - https://www.centos.org/
CentOS简介
如果不知道应该下载哪一个CentOS版本,Everything ISO也许是一个非常好的选择
The CentOS Project is a community-driven free software effort focused on delivering a robust open source ecosystem. For users, we offer a consistent manageable platform that suits a wide variety of deployments. For open source communities, we offer a solid, predictable base to build upon, along with extensive resources to build, test, release, and maintain their code.
https://www.centos.org/download/
CentOS 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 hostname centos6 vi /etc/sysconfig/network mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bakmv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bakmv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.bakwget -O /etc/yum.repos.d/Centos-6.repo http://mirrors.aliyun.com/repo/Centos-6.repo wget -O /etc/yum.repos.d/epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo yum makecache sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux setenforce 0 getenforce /etc/init.d/iptables stop chkconfig iptables off vi /etc/profile export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` " export HISTSIZE="10000" source /etc/profile
CentOS 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 hostnamectl set-hostname centos7 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bakmv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bakmv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.bakwget -O /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo yum makecache sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux setenforce 0 getenforce systemctl stop firewalld.service systemctl disable firewalld.service yum -y install iptables-services vim /etc/sysconfig/iptables systemctl restart iptables.service systemctl enable iptables.service vi /etc/profile export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` " export HISTSIZE="10000" source /etc/profileyum install python-devel gcc zlib zlib-devel openssl-devel -y yum install epel-release -y yum install python-pip -y pip install --upgrade pip yum -y install yum-cron vim /etc/yum/yum-cron.conf apply_updates = no apply_updates = yes systemctl start crond systemctl start yum-cron
CentOS通用优化和配置 优化
如果各位没有标准的baseline,以下配置可作为最简单的参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 export TMOUT=0sed -i '/^#UseDNS/s/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config /etc/init.d/sshd reload cat >>/etc/rc.local<<EOF #open files ulimit -SHn 65535 #stack size ulimit -s 65535 EOF
配置
以下配置非必须,仅供需要时参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bakvi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADDR=00:0C:29:D0:C7:B5 TYPE=Ethernet UUID=080a457b-6a53-4a3a-9155-a23c1146c2c6 ONBOOT=yes NM_CONTROLLED=no BOOTPROTO=static IPADDR=192.168.1.10 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 yum -y install gcc gcc-c++ 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-ltdl bison libtool vim-enhanced vim /etc/hosts vim /etc/resolv.conf nameserver DNS1 nameserver DNS2 iptables –F iptables –L vi /etc/sysconfig/iptables /etc/init.d/iptables save dd if =/dev/zero of=/home/swap bs=1M count=1024mkswap /home/swap swapon /home/swap swapon -s vi /etc/fstab /home/swap swap swap default 0 0 useradd test echo "test" :"test" | chpasswdvisudo test ALL=(ALL) NOPASSWD: ALLyum groupinstall chinese-support vim /etc/sysconfig/i18n LANG="zh_CN.GB18030" SUPPORTED="zh_CN.UTF-8:zh_CN:zh:en_US.UTF-8:en_US:en" SYSFONT="latarcyrheb-sun16″ # CentOS 7 vim /etc/locale.conf #LANG=" en_US.UTF-8" LANG=" zh_CN.GB18030" LANGUAGE=" zh_CN.GB18030:zh_CN.GB2312:zh_CN" SUPPORTED=" zh_CN.UTF-8:zh_CN:zh:en_US.UTF-8:en_US:en" SYSFONT=" lat0-sun16" # 选择source或注销或重启使语言配置生效 init 6 # 设置运行级别,5为图形化,3为命令行 # CentOS 6 vi /etc/inittab # Default runlevel. The runlevels used are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:5:initdefault: #id:3:initdefault # CentOS 7 # 查看当前运行级别 systemctl get-default graphical.target # 修改为命令行模式 systemctl set-default multi-user.target # 修改为图形化模式 systemctl set-default graphical.target
内核优化
以下内容不建议直接套用,该配置主要适用于 CentOS 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv4.icmp_echo_ignore_broadcasts = 1 net.ipv4.icmp_ignore_bogus_error_responses = 1 net.ipv4.ip_forward = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.all.accept_source_route = 0 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_sack = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 16384 4194304 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 262144 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_fin_timeout = 1 net.ipv4.tcp_keepalive_time = 30 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.default.accept_redirects = 0 net.ipv4.conf.all.secure_redirects = 0 net.ipv4.conf.default.secure_redirects = 0 sysctl -p