停止并屏蔽Centos7自带的防火墙firewalld:
systemctl status firewalld //查看状态
systemctl stop firewalld
systemctl mask firewalld //屏蔽FirewallD服务,如果需要取消屏蔽呢就是用systemctl unmask firewalld
systemctl disable firewalld //删除开机启动
安装iptables-services软件包:
yum install iptables-services
在引导时启用iptables服务:
systemctl enable iptables
启动iptables服务:
systemctl start iptables
增加防火墙规则(这里的20300是我们后面要修改的SSH的新端口):
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 20300 -j ACCEPT
保存防火墙规则:
service iptables save
或
/usr/libexec/iptables/iptables.init save
查看iptables开放的端口:
iptables -nL --line-number
或查看iptables列表:
iptables -L
管理iptables服务:
systemctl stop|start|restart iptables
修改或查看防火墙配置文件:
vi /etc/sysconfig/iptables
修改SSH端口(默认端口是22,为了安全修改下):
vi /etc/ssh/sshd_config
#Port 22
去掉#,改成
Port 20300
保存
重启sshd服务:
systemctl restart sshd.service
会报错:
Job for sshd.service failed because the control process exited with error code. See “systemctl status sshd.service” and “journalctl-xe” for details.
原因:
selinux不允许自定义sshd服务设置端口放行
但我们不能把selinux关闭了,会造成服务器安全性能下降
查看selinux状态:
getenforce
如果是Enforcing就是启用,否则请启用selinux:
setenforce 1
查看selinux中sshd服务放行的端口号:
semanage port -l | grep ssh
先安装SELinux的管理工具semanage和依赖包policycoreutils-python:
yum provides semanage
yum install policycoreutils-python
查看selinux中sshd服务放行的端口号:
semanage port -l | grep ssh
添加自定义sshd服务端口至selinux中:
semanage port -a -t ssh_port_t -p tcp 20300
再次查看selinux中sshd服务放行的端口号:
semanage port -l | grep ssh
重启sshd服务:
systemctl restart sshd.service
这样就可以使用新的SSH端口20300
禁止ping,这个不是必须的,你可以不做:
vi /etc/sysctl.conf
最后增加一行:
net.ipv4.icmp_echo_ignore_all = 1
使配置生效:
sysctl -p
租用服务器请自觉遵守服务器所在地的法律法规,国内服务器禁止任何违法违规用途!!!
香港、韩国、日本、美国、台湾、新加坡、菲律宾等境外服务器由机房直供(一手资源,价格最低),我公司只是代购,请自行遵守中国法律和服务器所在地的法规,勿用于违法犯罪用途,做什么用途虽然机房不过问,但是如果您私自做违法用途请自担责任!需要租用服务器请联系QQ16764407或微信18988993510













