Centos7常用操作

1.虚拟机环境下centos的防火墙可以关闭,否则主机调用虚拟机的相关服务时需要调整相应端口:

CentOS7:

systemctl stop firewalld.service #停止
systemctl disable firewalld.service #禁用
之前的版本:
service iptables stop #停止
chkconfig iptables off #禁用

永久关闭防火墙: chkconfig iptables off

检查防火墙状态: service iptables status

CentOS7防火墙firewalld详解

centos7的防火墙:

centos7不再使用iptables,而是使用firewalld

若不想使用firewalld,而继续使用iptables,可以先停掉firewalld,并且安装iptables-services包

查看firewalld状态:systemctl status firewalld

扫描二维码关注公众号,回复: 110982 查看本文章

停掉firewalld:systemctl stop firewalld

开机不启动firewalld:systemctl disable firewalld

安装iptables服务:yum install -y iptables-service

开机启动iptables:systemctl enable iptables

启动iptables:systemctl start iptables

firewalld的使用:

centos6上使用的是静态防火墙,规则保存在/etc/sysconfig/iptables,更新规则后需要重新加载或者重启iptables服务,相当于把之前的规则全部清空,再把配置文件中的规则加载一遍。

而centos7使用了动态防火墙,即firewalld,这种防火墙更改了规则不需要重新加载配置。

无论是firewalld还是iptables服务,底层都是使用iptables工具来配置规则的。

firewalld有zone和service的概念,每一个zone的规则不一样,默认有9个zone。centos7默认使用的zone是public。

查看所有的zone:firewall-cmd --get-zones

查看默认的zone:firewall-cmd --get-default-zone

firewalld的9个zone介绍:

drop(丢弃):任何接收的网络数据包都被丢弃,没有任何回复,仅能有发送出去的网络连接。

block(限制):任何接收的网络连接都被IPv4的icmp-host-prohibited信息和IPv6的icmp6-adm-prohibited信息所拒绝。

public(公共):在公共区域内使用,不能相信网络内其他计算机不会对您的计算机造成危害,只能接收经过选择的连接。

external(外部):特别是为路由器启用了伪装功能的外部网,您不能信任来自网络的其他计算机,不能想他他们不会对你的计算机造成危害,只能接收经过选择的连接。

dmz(非军事区):用于您的非军事区内的电脑,此区域内可公开访问,可以有限的进入你的内部网络,仅仅接收经过选择的连接。

work(工作):用于工作区,你可以基本相信网络内的其他电脑不会危害你的电脑,仅仅接收经过选择的连接。

home(家庭):用于家庭网络,你可以基本相信网络内的其他计算机不会危害你的计算机,仅仅接收经过选择的连接。

internal(内部):用于内部网络,你可以基本相信网络内的其他计算机不会危害你的计算机,仅仅接收经过选择的连接。

trusted(信任):可接受所有的网络连接。

指定其中一个zone为默认的zone是可行的。当接口连接加入了NetworkManager,它们就被分配为默认的zone,安装时firewalld默认的zone为public。

firewalld zone相关命令的用法

设置默认的zone:firewalld-cmd --set-default-zone=work

查看指定网卡所在的zone:firewall-cmd --get-zone-of-interface=eno16777736

设置指定网卡所在的zone:firewall-cmd --zone=public --add-interface=lo

更改指定网卡所在的zone:firewall-cmd --zone=dmz --change-interface=lo

删除指定网卡所在的zone:firewall-cmd --zone=dmz --remove-interface=lo

查看所有网卡所在的zone:firewall-cmd --get-active-zones

firewalld service介绍

在/usr/lib/firewalld/services/目录中,还保存了另外一类配置文件,每个文件对应一项具体的网络服务,如ssh服务等。

与之对应的配置文件中记录了各项服务所使用的tcp/udp端口,在最新版的firewalld中默认已经定义了70多种服务供我们使用。

zone就是调用了不同的service而实现了不同的效果。

列出所有的service:firewall-cmd --get-services

列出当前zone下加载的service:firewall-cmd --list-services

查看指定zone下面的service:firewal-cmd --zone=public --list-services

给指定zone添加service:firewall-cmd --zone=public --ad-service=http

这个改动不会保存到配置文件里,要想保存到配置文件里面,需要加上--permanent:

firewall-cmd --zone=public --add-service=http --permanent

既然可以增加,同样也可以删除,下面命令会从配置文件中永久删除public zone中的http服务:firewall-cmd --zone=public --remove-service=http --permanent

firewalld规则

假设自定义的ssh端口号为12222,使用下面的命令来添加新端口的防火墙规则:

firewall-cmd --add-port=12222/tcp --permanent

使用--permanent参数可以将更改保存到配置文件。

防火墙配置文件也可以手动修改,修改后记得重新加载:firewall-cmd --reload

这种情况下重新加载防火墙并不会中断用户连接,也可以彻底加载,这时候会中断用户连接,也会丢弃状态信息:firewall-cmd --complete-reload

还可以添加一个端口范围:firewall-cmd --add-port=2000-4000/tcp

针对指定zone添加端口:firewall-cmd --permanent --zone=home --add-port=443/tcp

启动端口转发,例如把22端口转发到127.0.0.2:firewall-cmd --permanent --zone=home --add-forward-port=port=22:proto=tcp:toaddr=127.0.0.2

firewalld配置文件

两个路径/etc/firewalld/和/usr/lib/firewalld

系统使用的是/etc/firewalld下面的配置文件

在/etc/firewalld/zones下面只有一个public.xml,如果给另外一个zone做一些改动,并永久保存,那么会自动生成对应的配置文件

比如,给work zone增加一个端口:firewall-cmd --permanent --zone=work --add-port=1000/tcp

此时就会生成一个work.xml的配置文件

需求:修改ftp的端口,默认ftp的端口是21,需要改为1121

cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/

vim /etc/firewalld/services/ftp.xml //把21改为1121

vim /etc/firewalld/zones/work.xml //work为默认zone,所以要编辑这个,增加一行<service name="ftp"/>

firewall-cmd --reload //重新加载

猜你喜欢

转载自www.cnblogs.com/wusheng2016/p/8992146.html