张子阳的博客

首页 读书 技术 店铺 关于
张子阳的博客 首页 读书 技术 关于

linux常用命令(防火墙)

2018-07-12 张子阳 分类: Linux

在部署和配置集群的时候,集群中的多台服务器之间需要通信,对于一些复杂的应用,例如consul、hadoop等,往往是客户端通信占用一个端口、Web UI占用一个端口、集群内的主机之间通信占用一个或多个端口。这样在测试环境部署的时候,如果启用防火墙,往往会因为漏掉放行某台服务器的某个端口,而造成连接失败。

所以,在测试环境下,为了节约时间,尽快地测试和部署集群,可以关掉防火墙(正式环境下根据情况自行判断了,如果是在阿里云、腾讯云,在云主机外部有一层可配置的“安全组”策略,在这个位置也可以配置防火墙)。

禁用SELinux

SELinux是一个很复杂的Linux安全机制,一般情况下用不到,可以使用下面的方法关掉它:

使用vim编辑器修改 /etc/selinux/config:

# vim /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted

将SELINUX=enforcing修改为SELINUX=disabled。

重启linux系统,然后使用getenforce命令查看当前的SELINUX状态:

# getenforce Disabled

查看、启用、关闭 防火墙

我使用的Linux发行版是CentOS,CentOS 7的默认防火墙工具是firewalld,Cent OS6的默认防火墙工具是iptables。

查看防火墙状态

首先,需要知道当前系统安装的防火墙工具的是firewalld,还是iptables,可以使用下面的命令进行验证:

# 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 Thu 2018-07-12 10:50:29 CST; 6min ago Docs: man:firewalld(1) Main PID: 726 (firewalld) CGroup: /system.slice/firewalld.service └─726 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

可以看到Active: active (running),说明默认的firewalld已经在运行了。

# systemctl status iptables Unit iptables.service could not be found.

说明没有安装iptables。

停用防火墙

关闭防火墙:

# systemctl stop firewalld

此时如果重新执行systemctl status firewalld,可以看到状态为:Active: inactive (dead)

禁止开机启动:

# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

启用防火墙

打开防火墙

# systemctl start firewalld

设置开机启动

# 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/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.

添加端口

# firewall-cmd --permanent --zone=public --add-port=14101-14200/tcp # firewall-cmd --reload

注意添加完后,要使用firewall-cmd --reload重新加载一下配置。

查看防火墙规则

# firewall-cmd --zone=public --list-ports

对于更复杂的规则,比如限制来自某个IP的访问等,以后有时候再继续完善。

感谢阅读,希望这篇文章能给你带来帮助!