以阿里云服务器为例 一、挂载硬盘 1、磁盘分区 fdisk -l #查看设备,一般可以看到设备名为/dev/xvdb fdisk /dev/xvdb #对磁盘进行分区 输入n #创建新分区 输入p #创建主分区 输入1 #创建第一个主分区 输入w #保存并执行以上命令,创建分区 待上面的命令执行完成之后,使用fdisk -l查看,会看到类似 /dev/xvdb1的分区 说明分区成功。 2、磁盘格式化 mkfs.ext4 /dev/xvdb1 #对分区进行格式化 说明:ext4是CentOS6.x默认的分区格式,CentOS5.x请使用ext3 待格式化完成之后(根据分区大小,格式化需要的时间不同,请耐心等待),对分区进行挂载 3、挂载磁盘 例如:要把/dev/xvdb1挂载到/data目录 mkdir -p /data #创建目录 mount /dev/xvdb1 /data #挂载 df -h #查看挂载结果 vi /etc/fstab #设置开机自动挂载,在最后一行输入以下代码 /dev/xvdb1 /data ext4 defaults 0 0 :wq! #保存退出 mount -a #使分区设置立即生效 二、创建SWAP分区 说明:默认阿里云服务没有swap分区,512M的云服务器安装php 5.6的时候会提示内存不足,安装失败! 强烈建议,不论内存有多大,都增加swap分区。 在512M的阿里云服务上增加1024M的swap空间 具体操作: 1、dd if=/dev/zero of=/tmp/swap bs=1M count=1024 #创建1024M的文件块 2、mkswap /tmp/swap #创建swap文件 3、swapon /tmp/swap #激活swap文件 4、swapon -s #查看swap 5、修改/etc/fstab文件,增加以下内容,让系统引导时自动启动 vi /etc/fstab #在最后添加下面代码 /tmp/swap swap swap default 0 0 :wq! #保存退出 系统运维 www.osyunwei.com 温馨提醒:qihang01原创内容版权所有,转载请注明出处及原文链接 三、系统内核优化 1、CentOS 5.x CentOS 6.x CentOS 7.x vi /etc/security/limits.conf #在最后一行添加以下代码 * soft nproc unlimited * hard nproc unlimited * soft nofile 655350 * hard nofile 655350 :wq! #保存退出 2、CentOS 5.x CentOS 6.x CentOS 7.x vi /etc/profile #在最后一行添加以下代码 ulimit -SHn 655350 ulimit -SHu unlimited ulimit -SHd unlimited ulimit -SHm unlimited ulimit -SHs unlimited ulimit -SHt unlimited ulimit -SHv unlimited :wq! #保存退出 source /etc/profile #使配置立即生效 ulimit -a #查看设置 3、CentOS 6.x(CentOS 5.x和CentOS 7.x不需要设置此项) vi /etc/security/limits.d/90-nproc.conf #在最后一行添加以下代码 * soft nproc unlimited * hard nproc unlimited * soft nofile 655350 * hard nofile 655350 :wq! #保存退出 4、 CentOS 5.x sed -i "s/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g" '/etc/sysctl.conf' echo -e "net.core.somaxconn = 262144" >> /etc/sysctl.conf echo -e "net.core.netdev_max_backlog = 262144" >> /etc/sysctl.conf echo -e "net.core.wmem_default = 8388608" >> /etc/sysctl.conf echo -e "net.core.rmem_default = 8388608" >> /etc/sysctl.conf echo -e "net.core.rmem_max = 16777216" >> /etc/sysctl.conf echo -e "net.core.wmem_max = 16777216" >> /etc/sysctl.conf echo -e "net.ipv4.route.gc_timeout = 20" >> /etc/sysctl.conf echo -e "net.ipv4.ip_local_port_range = 1025 65535" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_retries2 = 5" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_fin_timeout = 30" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_syn_retries = 1" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_synack_retries = 1" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_timestamps = 0" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_keepalive_time = 120" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_keepalive_probes = 3" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_keepalive_intvl = 15" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_max_tw_buckets = 200000" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_max_orphans = 3276800" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_max_syn_backlog = 262144" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_wmem = 8192 131072 16777216" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_rmem = 32768 131072 16777216" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_mem = 94500000 915000000 927000000" >> /etc/sysctl.conf echo -e "net.ipv4.ip_conntrack_max = 25000000" >> /etc/sysctl.conf echo -e "net.ipv4.netfilter.ip_conntrack_max = 25000000" >> /etc/sysctl.conf echo -e "net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 180" >> /etc/sysctl.conf echo -e "net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 1" >> /etc/sysctl.conf echo -e "net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait = 60" >> /etc/sysctl.conf echo -e "net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 120" >> /etc/sysctl.conf CentOS 6.x sed -i "s/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g" '/etc/sysctl.conf' echo -e "net.core.somaxconn = 262144" >> /etc/sysctl.conf echo -e "net.core.netdev_max_backlog = 262144" >> /etc/sysctl.conf echo -e "net.core.wmem_default = 8388608" >> /etc/sysctl.conf echo -e "net.core.rmem_default = 8388608" >> /etc/sysctl.conf echo -e "net.core.rmem_max = 16777216" >> /etc/sysctl.conf echo -e "net.core.wmem_max = 16777216" >> /etc/sysctl.conf echo -e "net.ipv4.route.gc_timeout = 20" >> /etc/sysctl.conf echo -e "net.ipv4.ip_local_port_range = 1025 65535" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_retries2 = 5" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_fin_timeout = 30" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_syn_retries = 1" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_synack_retries = 1" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_timestamps = 0" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_keepalive_time = 120" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_keepalive_probes = 3" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_keepalive_intvl = 15" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_max_tw_buckets = 200000" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_max_orphans = 3276800" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_max_syn_backlog = 262144" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_wmem = 8192 131072 16777216" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_rmem = 32768 131072 16777216" >> /etc/sysctl.conf echo -e "net.ipv4.tcp_mem = 94500000 915000000 927000000" >> /etc/sysctl.conf echo -e "net.nf_conntrack_max = 25000000" >> /etc/sysctl.conf echo -e "net.netfilter.nf_conntrack_max = 25000000" >> /etc/sysctl.conf echo -e "net.netfilter.nf_conntrack_tcp_timeout_established = 180" >> /etc/sysctl.conf echo -e "net.netfilter.nf_conntrack_tcp_timeout_time_wait = 1" >> /etc/sysctl.conf echo -e "net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60" >> /etc/sysctl.conf echo -e "net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120" >> /etc/sysctl.conf /sbin/sysctl -p #使配置立即生效 cat /var/log/secure #查看系统设置是否正确,没有error提示说明设置正确 备注: CentOS 5.x中模块名是ip_conntrack CentOS 6.x中模块名是nf_conntrack 在/etc/sysctl.conf优化时,在CentOS 6.x中要把 net.ipv4.netfilter.ip_conntrack_max 这种参数 改成net.netfilter.nf_conntrack_max 四、系统安全设置 1、创建普通账号 useradd osyunwei #创建普通账号 passwd 123456 #设置密码 2、禁用root直接登录 vi /etc/ssh/sshd_config #编辑 找到PermitRootLogin,将后面的yes改为no :wq! #保存退出 3、给系文件加锁,防止未经许可的删除或添加 chattr +ia /etc/passwd chattr +ia /etc/shadow chattr +ia /etc/group chattr +ia /etc/gshadow chattr +ia /etc/services lsattr /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/services #显示文件的属性 注意:执行以上权限修改之后,就无法添加删除用户了。 如果再要添加删除用户,需要先取消上面的设置,等用户添加删除完成之后,再执行上面的操作 chattr -ia /etc/passwd chattr -ia /etc/shadow chattr -ia /etc/group chattr -ia /etc/gshadow chattr -ia /etc/services 4、开启防火墙 yum install iptables #安装防火墙 yum install wget 先安装下载工具 chkconfig iptables on #设置开机启动 vi /etc/sysconfig/iptables #编辑,添加以下代码 # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -s 192.168.1.1/24 -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT # Iptables For OsYunWei.Com Date 2015/05/22 service iptables start #启动防火墙 备注:-s 192.168.1.1/24 表示只允许这个ip段访问3306端口,可以根据需求修改 4、修改ssh默认端口 把ssh默认远程连接端口22修改为222 vi /etc/ssh/sshd_config 在端口#Port 22下面增加Port 222 :wq! #保存退出 vi /etc/ssh/ssh_config 在端口#Port 22下面增加Port 222 :wq! #保存退出 /etc/init.d/sshd restart #重启sshd服务 vi /etc/sysconfig/iptables #编辑 把22端口修改为222 :wq! #保存退出 /etc/init.d/iptables restart #重启防火墙,使配置生效 五、修改主机名称 这里设置主机名为:www.osyunwei.com 1、hostname “www.osyunwei.com” #设置主机名为www.osyunwei.com 2、 vi /etc/sysconfig/network #编辑配置文件CentOS 5.x CentOS 6.x HOSTNAME= www.osyunwei.com #修改localhost.localdomain为www.osyunwei.com :wq! #保存退出 vi /etc/hostname #编辑配置文件CentOS 7.x www.osyunwei.com #修改localhost.localdomain为www.osyunwei.com :wq! #保存退出 3、vi /etc/hosts #编辑配置文件 127.0.0.1 www.osyunwei.com localhost #修改localhost.localdomain为www.osyunwei.com :wq! #保存退出 六、同步系统时间 yum install -y ntp #安装ntp ntpdate cn.pool.ntp.org #执行时间同步 hwclock --systohc #系统时钟和硬件时钟同步 CentOS 5.x echo -e "0 0 * * * /sbin/ntpdate cn.pool.ntp.org > /dev/null" >> /var/spool/cron/root #添加计划任务 CentOS 6.x echo -e "0 0 * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null" >> /var/spool/cron/root #添加计划任务 service crond restart #重启服务 七、安装基础软件包 yum install -y apr* autoconf automake bison cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng* libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* libX* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils ppl telnet t1lib t1lib* nasm nasm* wget zlib-devel 至此,CentOS服务器初始化设置设置完成。
一、注释掉系统不需要的用户和用户组
由于系统与硬件的兼容性问题,有可能升级内核后导致服务器不能正常启动,这是非常可怕的,没有特别的需要,建议不要随意升级内核。 net.ipv4.ip_forward = 1 #修改为1 net.core.somaxconn = 262144 net.core.netdev_max_backlog = 262144 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.netfilter.ip_conntrack_max = 131072 net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 180 net.ipv4.route.gc_timeout = 20 net.ipv4.ip_conntrack_max = 819200 net.ipv4.ip_local_port_range = 10024 65535 net.ipv4.tcp_retries2 = 5 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_len = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_keepalive_time = 120 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_keepalive_intvl = 15 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_wmem = 8192 131072 16777216 net.ipv4.tcp_rmem = 32768 131072 16777216 net.ipv4.tcp_mem = 94500000 915000000 927000000
/sbin/sysctl -p #使配置立即生效 (责任编辑:IT) |