Linux 7 中防火墙默认使用firewalld 来进行管理了,7之前的iptables 被取消,默认也没有安装。 所以想继续使用旧的iptables需要重新安装。 这篇来看重新启用iptables和配置firewalld 的方法。
1 启用旧的iptables 防火墙
1.1 关闭firewall
systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动
--停止firewall:
[root@cndba.cn ~]# systemctl stop firewalld
--禁止firewall开机启动:
[root@cndba.cn ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
--查看状态:
[root@cndba.cn ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead)
1.2 安装iptables防火墙
[root@cndba.cn ~]# yum install iptables-services -y Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. dave | 4.1 kB 00:00:00 (1/2): dave/group_gz | 136 kB 00:00:00 (2/2): dave/primary_db | 3.6 MB 00:00:00 Resolving Dependencies --> Running transaction check ---> Package iptables-services.x86_64 0:1.4.21-16.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================== Package Arch Version Repository Size =================================================================================================== Installing: iptables-services x86_64 1.4.21-16.el7 dave 50 k Transaction Summary =================================================================================================== Install 1 Package Total download size: 50 k Installed size: 24 k Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Warning: RPMDB altered outside of yum. Installing : iptables-services-1.4.21-16.el7.x86_64 1/1 Verifying : iptables-services-1.4.21-16.el7.x86_64 1/1 Installed: iptables-services.x86_64 0:1.4.21-16.el7 Complete! [root@cndba.cn ~]#
关于YUM 的配置参考:
Linux 7 中Yum 配置 说明
http://www.cndba.cn/account/article/details/155
1.3 配置iptables 参数
修改/etc/sysconfig/iptables文件,添加对应端口信息:
[root@cndba.cn ~]# cat /etc/sysconfig/iptables # sample configuration for iptables service # you can edit this manually or use system-config-firewall # please do not ask us to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT [root@cndba.cn ~]#
1.4 启动iptables
[root@cndba.cn ~]# service iptables start Redirecting to /bin/systemctl start iptables.service [root@cndba.cn ~]# systemctl status iptables ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: active (exited) since Wed 2016-03-02 10:39:48 CST; 13s ago Process: 25132 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 25132 (code=exited, status=0/SUCCESS) Mar 02 10:39:48 dave systemd[1]: Starting IPv4 firewall with iptables... Mar 02 10:39:48 dave iptables.init[25132]: iptables: Applying firewall rules: [ OK ] Mar 02 10:39:48 dave systemd[1]: Started IPv4 firewall with iptables. [root@cndba.cn ~]#
2 Firewall 配置
2.1 关闭iptables 防火墙
[root@cndba.cn ~]# systemctl stop iptables [root@cndba.cn ~]# systemctl disable iptables [root@cndba.cn ~]# systemctl status iptables ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: inactive (dead) since Wed 2016-03-02 10:41:34 CST; 17s ago Main PID: 25132 (code=exited, status=0/SUCCESS) Mar 02 10:39:48 dave systemd[1]: Starting IPv4 firewall with iptables... Mar 02 10:39:48 dave iptables.init[25132]: iptables: Applying firewall rules: [ OK ] Mar 02 10:39:48 dave systemd[1]: Started IPv4 firewall with iptables. Mar 02 10:41:34 dave systemd[1]: Stopping IPv4 firewall with iptables... Mar 02 10:41:34 dave iptables.init[25183]: iptables: Setting chains to policy ACCEPT: filter [ OK ] Mar 02 10:41:34 dave iptables.init[25183]: iptables: Flushing firewall rules: [ OK ] Mar 02 10:41:34 dave iptables.init[25183]: iptables: Unloading modules: [ OK ] Mar 02 10:41:34 dave systemd[1]: Stopped IPv4 firewall with iptables. [root@cndba.cn ~]#
因为在上节中我们禁用了firewall,所以这里我们启用firewall,在配置:
[root@cndba.cn ~]# systemctl start firewalld [root@cndba.cn ~]# systemctl enable firewalld Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service. Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service. [root@cndba.cn ~]# 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-03-02 10:42:50 CST; 8s ago Main PID: 25288 (firewalld) CGroup: /system.slice/firewalld.service └─25288 /usr/bin/python -Es /usr/sbin/firewalld --nofork –nopid
2.2 图形化配置firewall-config
直接使用firewall-config命令进行配置。
[root@cndba.cn ~]# firewall-config
运行后会弹出窗口,在界面上选中要打开的服务即可,会自动保存选项:
2.3 命令行配置命令
命令行的管理命令是:firewall-cmd ,可以用help 查看帮助:
[root@cndba.cn ~]# firewall-cmd --help Usage: firewall-cmd [OPTIONS...] General Options -h, --help Prints a short help text and exists -V, --version Print the version string of firewalld -q, --quiet Do not print status messages Status Options --state Return and print firewalld state --reload Reload firewall and keep state information --complete-reload Reload firewall and loose state information --runtime-to-permanent Create permanent from runtime configuration Permanent Options --permanent Set an option permanently Usable for options maked with [P] Zone Options --get-default-zone Print default zone for connections and interfaces --set-default-zone=<zone> Set default zone --get-active-zones Print currently active zones --get-zones Print predefined zones [P] --get-services Print predefined services [P] --get-icmptypes Print predefined icmptypes [P] --get-zone-of-interface=<interface> Print name of the zone the interface is bound to [P] --get-zone-of-source=<source>[/<mask>] Print name of the zone the source[/mask] is bound to [P] --list-all-zones List everything added for or enabled in all zones [P] --new-zone=<zone> Add a new zone [P only] --delete-zone=<zone> Delete an existing zone [P only] --zone=<zone> Use this zone to set or query options, else default zone Usable for options maked with [Z] --get-target Get the zone target [P] [Z] --set-target=<target> Set the zone target [P] [Z]
FirewallD 使用Zones来进行配置,Zone 定义的是网络连接的信任级别。 大部分的zones 都是可变的(mutable),也有不可变的zone(immutable)。 不可变的不是用户定义的,并且不能覆盖。
具体zone和对应的说明如下::
Zone Description ----------------------------------------------------- drop (immutable) Deny all incoming connections, outgoing ones are accepted. block (immutable) Deny all incoming connections, with ICMP host prohibited messages issued. trusted (immutable) Allow all network connections public Public areas, do not trust other computers external For computers with masquerading enabled, protecting a local network dmz For computers publicly accessible with restricted access. work For trusted work areas home For trusted home network connections internal For internal network, restrict incoming connections
--查看所有的Zone:
[root@cndba.cn ~]# firewall-cmd --get-zones block dmz drop external home internal public trusted work
--查看当前默认Zone:
[root@cndba.cn ~]# firewall-cmd --get-default-zone Public
默认zone的定义在/etc/firewalld/firewalld.conf 文件中指定。
[root@cndba.cn ~]# cat /etc/firewalld/firewalld.conf # firewalld config file # default zone # The default zone used if an empty zone string is used. # Default: public DefaultZone=public
Zone 的配置文件在/usr/lib/firewalld/zones目录下对应的xml文件中:
[root@cndba.cn zones]# pwd /usr/lib/firewalld/zones [root@cndba.cn zones]# ls block.xml drop.xml home.xml public.xml work.xml dmz.xml external.xml internal.xml trusted.xml
查看某个区域的所有配置信息:
[root@cndba.cn zones]# firewall-cmd --list-all public (default, active) interfaces: enp0s25 sources: services: dhcpv6-client http nfs ssh ports: masquerade: no forward-ports: icmp-blocks: rich rules:
firewall-cmd命令配置有2种模式:Permanent 和 runtime。 这个在图形界面上配置的时候也可以选择。
如果是Runtime,那么配置的规则只对当前环境有效,系统重启后就失效了。
如果是Permanent则永久生效。
每个Zone可以配置多个服务,对应多个接口
[root@cndba.cn zones]# firewall-cmd --get-services RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imaps ipp ipp-client ipsec iscsi-target kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind rsyncd samba samba-client smtp ssh telnet tftp tftp-client transmission-client vdsm vnc-server wbem-https [root@cndba.cn zones]#
注意:这里的服务名就是可以添加到防火墙里的service。
查看允许的服务
[root@cndba.cn zones]#firewall-cmd --list-services --zone=public dhcpv6-client http nfs ssh
某些服务,对应多个端口,在防火墙允许某项服务则可能意味着允许该服务对应的多个端口:
给区域添加服务:
[root@cndba.cn zones]# firewall-cmd --add-service=ftp --zone=public --permanent success [root@cndba.cn zones]# firewall-cmd --list-services --zone=public dhcpv6-client http nfs ssh [root@cndba.cn zones]# firewall-cmd --add-service=ftp --zone=public success [root@cndba.cn zones]# firewall-cmd --list-services --zone=public dhcpv6-client ftp http nfs ssh
这里可以看到,如果是permanent的方法,那么重新加载防火墙才能生效,如果是runtime则立即生效。
删除区域服务:
[root@cndba.cn zones]# firewall-cmd --remove-service=ftp --zone=public Success
查看某项服务是否被允许:
[root@cndba.cn zones]# firewall-cmd --query-service=http --zone=public Yes
添加service 之后,对应的端口还需要手工添加.
区域添加端口:
[root@cndba.cn zones]# firewall-cmd --add-port=8080/tcp --zone=public --permanent success [root@cndba.cn zones]# iptables -L -n | grep 8080 [root@cndba.cn zones]# firewall-cmd --list-all public (default, active) interfaces: enp0s25 sources: services: dhcpv6-client http nfs ssh ports: 3128/tcp 8000/tcp masquerade: no forward-ports: icmp-blocks: rich rules:
--永久生效的需要重启防火墙:
[root@cndba.cn zones]# systemctl restart firewalld [root@cndba.cn zones]# firewall-cmd --list-all public (default, active) interfaces: enp0s25 sources: services: dhcpv6-client ftp http https ssh ports: 21/tcp 8000/tcp 8080/tcp masquerade: no forward-ports: icmp-blocks: rich rules: [root@cndba.cn zones]# iptables -L -n | grep 8080 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 ctstate NEW [root@cndba.cn zones]#
删除端口:
[root@cndba.cn zones]# firewall-cmd --remove-port=8080/tcp --zone=public Success
查看端口是否被允许:
[root@cndba.cn zones]# firewall-cmd --query-port=8000/tcp --zone=public Yes
2.4 runtime区域配置帮助
启用区域中的一种服务
firewall-cmd [--zone=<zone>] --add-service=<service> [--timeout=<seconds>]
此举启用区域中的一种服务。如果未指定区域,将使用默认区域。如果设定了超时时间,服务将只启用特定秒数。如果服务已经活跃,将不会有任何警告信息。
例: 使区域中的ipp-client服务生效60秒:
firewall-cmd --zone=home --add-service=ipp-client --timeout=60
例: 启用默认区域中的http服务:
firewall-cmd --add-service=http
禁用区域中的某种服务
firewall-cmd [--zone=<zone>] --remove-service=<service>
此举禁用区域中的某种服务。如果未指定区域,将使用默认区域。
例: 禁止home区域中的http服务:
firewall-cmd --zone=home --remove-service=http
区域种的服务将被禁用。如果服务没有启用,将不会有任何警告信息。
查询区域中是否启用了特定服务
firewall-cmd [--zone=<zone>] --query-service=<service>
如果服务启用,将返回1,否则返回0。没有输出信息。
启用区域端口和协议组合
firewall-cmd [--zone=<zone>] --add-port=<port>[-<port>]/<protocol> [--timeout=<seconds>]
此举将启用端口和协议的组合。端口可以是一个单独的端口 <port> 或者是一个端口范围 <port>-<port> 。协议可以是 tcp 或 udp。
禁用端口和协议组合
firewall-cmd [--zone=<zone>] --remove-port=<port>[-<port>]/<protocol>
查询区域中是否启用了端口和协议组合
firewall-cmd [--zone=<zone>] --query-port=<port>[-<port>]/<protocol>
如果启用,此命令将有返回值。没有输出信息。
启用区域中的IP伪装功能
firewall-cmd [--zone=<zone>] --add-masquerade
此举启用区域的伪装功能。私有网络的地址将被隐藏并映射到一个公有IP。这是地址转换的一种形式,常用于路由。由于内核的限制,伪装功能仅可用于IPv4。
禁用区域中的IP伪装
firewall-cmd [--zone=<zone>] --remove-masquerade
查询区域的伪装状态
firewall-cmd [--zone=<zone>] --query-masquerade
如果启用,此命令将有返回值。没有输出信息。
启用区域的ICMP阻塞功能
firewall-cmd [--zone=<zone>] --add-icmp-block=<icmptype>
此举将启用选中的Internet控制报文协议(ICMP)报文进行阻塞。ICMP报文可以是请求信息或者创建的应答报文,以及错误应答。
禁止区域的ICMP阻塞功能
firewall-cmd [--zone=<zone>] --remove-icmp-block=<icmptype>
查询区域的ICMP阻塞功能
firewall-cmd [--zone=<zone>] --query-icmp-block=<icmptype>
如果启用,此命令将有返回值。没有输出信息。
例: 阻塞区域的响应应答报文:
firewall-cmd --zone=public --add-icmp-block=echo-reply
在区域中启用端口转发或映射
firewall-cmd [--zone=<zone>] --add-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
端口可以映射到另一台主机的同一端口,也可以是同一主机或另一主机的不同端口。端口号可以是一个单独的端口 <port> 或者是端口范围 <port>-<port> 。协议可以为 tcp 或udp 。目标端口可以是端口号 <port> 或者是端口范围 <port>-<port> 。目标地址可以是 IPv4 地址。受内核限制,端口转发功能仅可用于IPv4。
禁止区域的端口转发或者端口映射
firewall-cmd [--zone=<zone>] --remove-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
查询区域的端口转发或者端口映射
firewall-cmd [--zone=<zone>] --query-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
如果启用,此命令将有返回值。没有输出信息。
例: 将区域home的ssh转发到127.0.0.2
firewall-cmd --zone=home --add-forward-port=port=22:proto=tcp:toaddr=127.0.0.2
2.5 permanent区域配置帮助
永久选项不直接影响运行时的状态。这些选项仅在重载或者重启服务时可用。为了使用运行时和永久设置,需要分别设置两者。 选项 -permanent 需要是永久设置的第一个参数。
获取永久选项所支持的服务
firewall-cmd --permanent --get-services
获取永久选项所支持的ICMP类型列表
firewall-cmd --permanent --get-icmptypes
获取支持的永久区域
firewall-cmd --permanent --get-zones
启用区域中的服务
firewall-cmd --permanent [--zone=<zone>] --add-service=<service>
此举将永久启用区域中的服务。如果未指定区域,将使用默认区域。
禁用区域中的一种服务
firewall-cmd --permanent [--zone=<zone>] --remove-service=<service>
查询区域中的服务是否启用
firewall-cmd --permanent [--zone=<zone>] --query-service=<service>
如果服务启用,此命令将有返回值。此命令没有输出信息。
例: 永久启用 home 区域中的 ipp-client 服务
firewall-cmd --permanent --zone=home --add-service=ipp-client
永久启用区域中的一个端口-协议组合
firewall-cmd --permanent [--zone=<zone>] --add-port=<port>[-<port>]/<protocol>
永久禁用区域中的一个端口-协议组合
firewall-cmd --permanent [--zone=<zone>] --remove-port=<port>[-<port>]/<protocol>
查询区域中的端口-协议组合是否永久启用
firewall-cmd --permanent [--zone=<zone>] --query-port=<port>[-<port>]/<protocol>
如果服务启用,此命令将有返回值。此命令没有输出信息。
例: 永久启用 home 区域中的 https (tcp 443) 端口
firewall-cmd --permanent --zone=home --add-port=443/tcp
永久启用区域中的伪装
firewall-cmd --permanent [--zone=<zone>] --add-masquerade
此举启用区域的伪装功能。私有网络的地址将被隐藏并映射到一个公有IP。这是地址转换的一种形式,常用于路由。由于内核的限制,伪装功能仅可用于IPv4。
永久禁用区域中的伪装
firewall-cmd --permanent [--zone=<zone>] --remove-masquerade
查询区域中的伪装的永久状态
firewall-cmd --permanent [--zone=<zone>] --query-masquerade
如果服务启用,此命令将有返回值。此命令没有输出信息。
永久启用区域中的ICMP阻塞
firewall-cmd --permanent [--zone=<zone>] --add-icmp-block=<icmptype>
此举将启用选中的 Internet 控制报文协议 (ICMP) 报文进行阻塞。 ICMP 报文可以是请求信息或者创建的应答报文或错误应答报文。
永久禁用区域中的ICMP阻塞
firewall-cmd --permanent [--zone=<zone>] --remove-icmp-block=<icmptype>
查询区域中的ICMP永久状态
firewall-cmd --permanent [--zone=<zone>] --query-icmp-block=<icmptype>
如果服务启用,此命令将有返回值。此命令没有输出信息。
例: 阻塞公共区域中的响应应答报文:
firewall-cmd --permanent --zone=public --add-icmp-block=echo-reply
在区域中永久启用端口转发或映射
firewall-cmd --permanent [--zone=<zone>] --add-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
端口可以映射到另一台主机的同一端口,也可以是同一主机或另一主机的不同端口。端口号可以是一个单独的端口 <port> 或者是端口范围 <port>-<port> 。协议可以为 tcp 或udp 。目标端口可以是端口号 <port> 或者是端口范围 <port>-<port> 。目标地址可以是 IPv4 地址。受内核限制,端口转发功能仅可用于IPv4。
永久禁止区域的端口转发或者端口映射
firewall-cmd --permanent [--zone=<zone>] --remove-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
查询区域的端口转发或者端口映射状态
firewall-cmd --permanent [--zone=<zone>] --query-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
如果服务启用,此命令将有返回值。此命令没有输出信息。
例: 将 home 区域的 ssh 服务转发到 127.0.0.2
firewall-cmd --permanent --zone=home --add-forward-port=port=22:proto=tcp:toaddr=127.0.0.2
版权声明:本文为博主原创文章,未经博主允许不得转载。