以阿里云服务器为例
一、挂载硬盘
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
var cpro_id = "u6885494";