您现在的位置是:网站首页> 编程资料编程资料
linux下通过iptables只允许指定ip地址访问指定端口的设置方法_LINUX_操作系统_
2023-03-19
746人已围观
简介 linux下通过iptables只允许指定ip地址访问指定端口的设置方法_LINUX_操作系统_
首先,清除所有预设置
iptables -F#清除预设表filter中的所有规则链的规则
iptables -X#清除预设表filter中使用者自定链中的规则 其次,设置只允许指定ip地址访问指定端口
其次,设置只允许指定ip地址访问指定端口
iptables -A INPUT -s xxx.xxx.xxx.xxx -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -d xxx.xxx.xxx.xxx -p tcp --sport 22 -j ACCEPT
iptables -A INPUT -s xxx.xxx.xxx.xxx -p tcp --dport 3306 -j ACCEPT
iptables -A OUTPUT -d xxx.xxx.xxx.xxx -p tcp --sport 3306 -j ACCEPT
上面这两条,请注意--dport为目标端口,当数据从外部进入服务器为目标端口;反之,数据从服务器出去则为数据源端口,使用 --sport
同理,-s是指定源地址,-d是指定目标地址。
然后,关闭所有的端口
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
最后,保存当前规则
/etc/rc.d/init.d/iptables save
service iptables restart
这种iptables的规则设定适用于只充当MySQL服务器的管理和维护,外部地址不提供任何服务。
如果你希望yum可以运行的话,还需要添加以下内容,允许DNS请求的53端口,允许下载随机产生的高端口
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 10000:65535 -j ACCEPT
/etc/rc.d/init.d/iptables save
service iptables restart
复制代码
代码如下:iptables -F#清除预设表filter中的所有规则链的规则
iptables -X#清除预设表filter中使用者自定链中的规则 其次,设置只允许指定ip地址访问指定端口
其次,设置只允许指定ip地址访问指定端口
复制代码
代码如下:iptables -A INPUT -s xxx.xxx.xxx.xxx -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -d xxx.xxx.xxx.xxx -p tcp --sport 22 -j ACCEPT
iptables -A INPUT -s xxx.xxx.xxx.xxx -p tcp --dport 3306 -j ACCEPT
iptables -A OUTPUT -d xxx.xxx.xxx.xxx -p tcp --sport 3306 -j ACCEPT
上面这两条,请注意--dport为目标端口,当数据从外部进入服务器为目标端口;反之,数据从服务器出去则为数据源端口,使用 --sport
同理,-s是指定源地址,-d是指定目标地址。
然后,关闭所有的端口
复制代码
代码如下:iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
最后,保存当前规则
复制代码
代码如下:/etc/rc.d/init.d/iptables save
service iptables restart
这种iptables的规则设定适用于只充当MySQL服务器的管理和维护,外部地址不提供任何服务。
如果你希望yum可以运行的话,还需要添加以下内容,允许DNS请求的53端口,允许下载随机产生的高端口
复制代码
代码如下:iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 10000:65535 -j ACCEPT
/etc/rc.d/init.d/iptables save
service iptables restart
相关内容
- linux系统怎么用SecureCRT上传和下载文件?_LINUX_操作系统_
- 在Linux 中产生和加密解密随机密码的教程_LINUX_操作系统_
- iptables防火墙只允许指定ip连接指定端口、访问指定网站_LINUX_操作系统_
- 通过OTPW来用SSH安全登录Linux服务器的教程_LINUX_操作系统_
- 新装Linux系统没有网卡驱动的解决办法和步骤_LINUX_操作系统_
- Linux服务器端SSH远程连接速度慢的解决方法_LINUX_操作系统_
- ssh encountered 1 errors during the transfer错误3种解决方法_LINUX_操作系统_
- Linux下一些在命令行中发送电子邮件的工具简介_LINUX_操作系统_
- Linux系统如何安装mongodb数据库Mongo扩展_LINUX_操作系统_
- 在Linux中使用Inxi获取系统和硬件信息的教程_LINUX_操作系统_
