centos 7 的 iptables 规则在重启后失效了? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
RandomUser
V2EX    问与答

centos 7 的 iptables 规则在重启后失效了?

  •  
  •   RandomUser 2016-12-14 09:20:41 +08:00 11100 次点击
    这是一个创建于 3225 天前的主题,其中的信息可能已经有所发展或是发生改变。
    问题描述:
    搬瓦工 centos 7 x64 最小化安装,网上原始的 ocserv 一键安装脚本直接用有问题,稍微修改了一下
    在安装成功后, putty 远程连接 22222 端口和 anyconnect 连接 11111 端口都是能正常使用的
    但是重启后,以上两种都无法连接上了
    按理说脚本里也有 systemctl restart iptables.service ,应该和系统重启对规则的影响是一样的吧
    linux 新手不太懂,是不是防火墙的规则有问题?



    参考:
    一键安装脚本里的 iptables 修改

    elif [[ ${iptablesisactive} = 'active' ]]; then
    iptables -P INPUT ACCEPT
    iptables -F
    iptables -X
    iptables -Z
    iptables -A INPUT -i lo -j ACCEPT
    iptables -A INPUT -i venet0 -j ACCEPT
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    iptables -I INPUT -p udp --dport 443 -j ACCEPT
    iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
    iptables -A INPUT -p tcp --dport 22222-j ACCEPT
    iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
    iptables -I INPUT -p tcp --dport 11111 -j ACCEPT
    iptables -I INPUT -p udp --dport 11111 -j ACCEPT
    iptables -I FORWARD -s ${vpnnetwork} -j ACCEPT
    iptables -t nat -A POSTROUTING -s ${vpnnetwork} -o venet0 -j MASQUERADE
    # iptables -t nat -A POSTROUTING -o venet0 -j MASQUERADE
    iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
    iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    iptables -P OUTPUT ACCEPT
    service iptables save
    systemctl restart iptables.service




    通过搬瓦工后台 iptables -L 查看现在的所有规则

    Chain INPUT (policy ACCEPT)
    target prot opt source destination
    ACCEPT udp -- anywhere anywhere udp dpt:11111
    ACCEPT tcp -- anywhere anywhere tcp dpt:11111
    ACCEPT udp -- anywhere anywhere udp dpt:https
    ACCEPT all -- anywhere anywhere
    ACCEPT all -- anywhere anywhere
    ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
    ACCEPT tcp -- anywhere anywhere tcp dpt:https
    ACCEPT tcp -- anywhere anywhere tcp dpt:ftp
    ACCEPT tcp -- anywhere anywhere tcp dpt:http
    ACCEPT tcp -- anywhere anywhere tcp dpt:webcache
    ACCEPT tcp -- anywhere anywhere tcp dpt:22222
    ACCEPT icmp -- anywhere anywhere icmp echo-request
    ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED

    Chain FORWARD (policy ACCEPT)
    target prot opt source destination
    ACCEPT all -- 192.168.8.0/24 anywhere
    TCPMSS tcp -- anywhere anywhere tcp flags:SYN,RST/SYN TCPMSS clamp to PMTU

    Chain OUTPUT (policy ACCEPT)
    target prot opt source destination
    第 1 条附言    2016-12-14 13:35:50 +08:00
    试了一下 firewalld ,把 ssh 端口加了进去, ocserv 也是 ok 的
    没有装 iptables ,但是一 reboot 还是又都连不上了

    firewall-cmd --permanent --add-port=${port}/tcp
    firewall-cmd --permanent --add-port=${port}/udp
    firewall-cmd --permanent --add-masquerade
    firewall-cmd --reload
    19 条回复    2016-12-14 18:09:53 +08:00
    Halry
        1
    Halry  
       2016-12-14 09:27:02 +08:00 via Android
    直接用 firewall-cmd 啊
    RandomUser
        2
    RandomUser  
    OP
       2016-12-14 09:35:30 +08:00
    @Halry 搬瓦工上 centos 默认没有 firewalld ,是 iptables

    脚本里 firewalld 的设置是下面这样的
    if [[ ${firewalldisactive} = 'active' ]]; then
    echo "Adding firewall ports."
    firewall-cmd --permanent --add-port=${port}/tcp
    firewall-cmd --permanent --add-port=${port}/udp
    echo "Allow firewall to forward."
    firewall-cmd --permanent --add-masquerade
    echo "Reload firewall configure."
    firewall-cmd --reload

    这里不用像 iptables 一样设置转发什么的吗? ocserv 能正常工作吗?
    mmmyc
        3
    mmmyc  
       2016-12-14 09:42:05 +08:00 via Android
    听说 7 和以前的版本改变很大了
    ragnaroks
        4
    ragnaroks  
       2016-12-14 09:54:40 +08:00   1
    先停止 iptables-services 再写 conf,保存后再启动.
    RandomUser
        5
    RandomUser  
    OP
       2016-12-14 10:17:44 +08:00
    @ragnaroks 你的意思是脚本里没有先停止 iptables 所以修改无效?但是 iptables -L 能查到规则已经生效了啊
    ragnaroks
        6
    ragnaroks  
       2016-12-14 10:21:38 +08:00
    @RandomUser
    我之前也无法理解为什么会这样,但是先停止编辑后再启动就正常.
    另外还有一个 bug(?),你使用命令添加并保存的文件并不是 iptables 启动时加载的那个文件.(这一条在 hostus 的 vps 上成功复现)
    LuvF
        7
    LuvF  
       2016-12-14 10:28:36 +08:00 via Android
    1 楼 +1
    RandomUser
        8
    RandomUser  
    OP
       2016-12-14 10:39:23 +08:00
    @LuvF 脚本里的几条命令看上去很简单,不用设置针对 ocserv 的转发?
    RandomUser
        9
    RandomUser  
    OP
       2016-12-14 10:51:08 +08:00
    @ragnaroks 把脚本改成下面这样又试了一次, reboot 后还是连不上

    systemctl stop iptables.service
    iptables -P INPUT ACCEPT
    ..........
    service iptables save
    systemctl enable iptables.service
    systemctl start iptables.service
    ragnaroks
        10
    ragnaroks  
       2016-12-14 10:55:12 +08:00
    service iptables save?
    是 iptables-servers 的还是重定向 iptables-save 的?
    ragnaroks
        11
    ragnaroks  
       2016-12-14 10:57:24 +08:00
    对了,我差点忘了,你检查一下 selinux 是不是开启状态的,是开启中的话是需要用 semanage 放行的,这个优先级比 iptables 高
    ragnaroks
        12
    ragnaroks  
       2016-12-14 11:01:32 +08:00
    #新增允许的 sshd 监听端口
    semanage port -a -t ssh_port_t -p tcp <sshd 端口>
    #删除允许的 sshd 监听端口
    semanage port -d -t ssh_port_t -p tcp <sshd 端口>

    先添加你自己的端口,再删除 22 端口就好
    winterock
        13
    winterock  
       2016-12-14 12:39:49 +08:00
    不要折腾 iptables 了,用 firewall-cmd ,没有就装一个。
    RandomUser
        14
    RandomUser  
    OP
       2016-12-14 12:56:04 +08:00
    @ragnaroks
    [root@default ~]# semanage
    -bash: semanage: command not found
    //这个版本的 centos 应该是没有 selinux 的,没有 /etc/selinux/这个目录

    是 iptables-servers 的还是重定向 iptables-save 的?
    //这句话是什么意思?我不太清楚 service iptables save 到底是干嘛的,以为就是保存配置文件
    RandomUser
        15
    RandomUser  
    OP
       2016-12-14 12:57:12 +08:00
    @winterock 嗯我可以试试,就是 firewalld 只要上面那几条命令就足够 ocserv 用了吗?
    hjc4869
        16
    hjc4869  
       2016-12-14 13:34:45 +08:00 via Android
    现在还能 service iptables save?
    应该 iptables-save>/etc/sysconfig/iptables 吧?
    RandomUser
        17
    RandomUser  
    OP
       2016-12-14 13:38:45 +08:00
    @hjc4869 不知道呀,我没研究过,都是照着脚本来的
    那 firewalld 我刚试了一下为什么 reboot 也不行
    RandomUser
        18
    RandomUser  
    OP
       2016-12-14 13:41:55 +08:00
    @winterock 这是什么原因啊?

    [root /]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
    Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
    Active: active (running) since Wed 2016-12-14 00:31:08 EST; 9min ago
    Docs: man:firewalld(1)
    Main PID: 123 (firewalld)
    CGroup: /system.slice/firewalld.service
    └─123 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

    Dec 14 00:20:15 default.hostname firewalld[1011]: WARNING: ebtables not usable, disabling ethernet bridge firewall.
    Dec 14 00:25:40 default.hostname firewalld[1011]: WARNING: ipset not usable, disabling ipset usage in firewall.
    Dec 14 00:31:05 default.hostname systemd[1]: Stopping firewalld - dynamic firewall daemon...
    Dec 14 00:31:05 default.hostname systemd[1]: Stopped firewalld - dynamic firewall daemon.
    Dec 14 00:31:08 default systemd[1]: Starting firewalld - dynamic firewall daemon...
    Dec 14 00:31:08 default systemd[1]: Started firewalld - dynamic firewall daemon.
    Dec 14 00:31:08 default firewalld[123]: WARNING: ipset not usable, disabling ipset usage in firewall.
    Dec 14 00:31:08 default firewalld[123]: WARNING: ebtables not usable, disabling ethernet bridge firewall.
    Dec 14 00:31:08 default firewalld[123]: WARNING: '/usr/sbin/ip6tables-restore -n' failed:
    Dec 14 00:31:08 default firewalld[123]: ERROR: COMMAND_FAILED
    winterock
        19
    winterock  
       2016-12-14 18:09:53 +08:00
    @RandomUser 具体的我也不熟悉。建议使用 stackoverflow.com ,上面一大把。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     6097 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 33ms UTC 02:21 PVG 10:21 LAX 19:21 JFK 22:21
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86