分享

linux 中 开放端口,以及防火墙的相关命令

 科技强我 2018-06-13
最近公司需要在 生产环境上线系统,碰到一些防火墙以及开放端口的问题,在此来 复习mark下
1、设定
[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
2、保存
[root@localhost ~]# /etc/rc.d/init.d/iptables save
         
3、重启防火墙
[root@localhost ~]# service iptables restart
         
  
4、查看
[root@localhost ~]# /etc/init.d/iptables status
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination        
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination        
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination
  

永久性关闭防火墙:

命令:#chkconfig --level 2345 iptables off

或者 #chkconfig  iptables off

 

其中2345 代表”执行等级“

 

等级0表示:表示关机

等级1表示:单用户模式

等级2表示:无网络连接的多用户命令行模式

等级3表示:有网络连接的多用户命令行模式

等级4表示:不可用

等级5表示:带图形界面的多用户模式

等级6表示:重新启动

临时关闭防火墙:

命令:/etc/init.d/iptables stop    

出现三个OK,关闭成功,此时防火墙已经关闭,不许重启已经生效。

命令: /etc/init.d/iptables status

关闭后查看状态,应该显示 firewall is not running上

防火墙常用命令:

service iptables status可以查看到iptables服务的当前状态。
      但是即使服务运行了,防火墙也不一定起作用,你还得看防火墙规则的设置 iptables -L
      在此说一下关于启动和关闭防火墙的命令:
      1) 重启后生效
      开启: chkconfig iptables on
      关闭: chkconfig iptables off
      2) 即时生效,重启后失效
     开启: service iptables start
     关闭: service iptables stop

设置防火墙 iptables 如何 禁止某个IP访问

方法一:

1.vi /etc/sysconfig/iptables里:RH-Firewall-1-INPUT – [0:0]下面添加一行

下面是只允许某个IP访问xx端口

  1. -A INPUT -s 192.168.5.244 -j DROP
2.重启生效

#service iptables restart

方法二:

1.先备份iptables

# cp /etc/sysconfig/iptables /var/tmp

添加拒绝IP

# iptables -I INPUT -s 118.109.231.217 -j DROP

以上是临时设置。

2.然后保存iptables

# service iptables save

3.重启防火墙

#service iptables restart

—————-下面是参考————————————

1. 查看本机关于IPTABLES的设置情况
# iptables -L -n

2. 清除原有规则
# iptables -F 清除预设表filter中的所有规则链的规则
# iptables -X 清除预设表filter中使用者自定链中的规则

3. 保存设置规则(因配置随系统重启而失效)
# /etc/rc.d/init.d/iptables save
保存规则到/etc/sysconfig/iptables文件中, 也可手工编辑该文件.

4. 设定预设规则
# iptables -p INPUT DROP
# iptables -p OUTPUT DROP
# iptables -p FORWARD DROP
上面的规则是不允许任何包通过.

5. 添加规则
# iptables -A INPUT -p tcp –dport 22 -j ACCEPT
# iptables -A OUTPUT -p tcp –sport 22 -j ACCEPT
开启22端口, 允许SSH登录, 如开启80端口:
# iptables -A INPUT -p tcp –dport 80 -j ACCEPT
# iptables -A OUTPUT -p tcp –sport 80 -j ACCEPT

6. 禁止某个IP访问
# iptables -I INPUT -s x.x.x.x -j DROP

也可进行更细致的设置, 如只允许192.168.1.14的机器进行SSH连接:
# iptables -A INPUT -p tcp –dport 22 -s 192.168.1.14 -j ACCEPT
如果要允许或限制一段IP地址可用192.168.1.0/24 表示192.168.1.1-255端的所有IP.

防止同步包洪水(Sync Flood)
# iptables -A FORWARD -p tcp –syn -m limit –limit 1/s -j ACCEPT

防止各种端口扫描
# iptables -A FORWARD -p tcp –tcp-flags SYN,ACK,FIN,RST RST -m limit –limit 1/s -j ACCEPT

Ping 洪水攻击(Ping of Death)
# iptables -A FORWARD -p icmp –icmp-type echo-request -m limit –limit 1/s -j ACCEPT

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多