390 lines
5.9 KiB
Markdown
390 lines
5.9 KiB
Markdown
Centos之防火墙/ 服务管理 命令详细说明
|
|
-----------------------
|
|
|
|
### 1、Centos7
|
|
|
|
> 在Centos7中使用firewall来代替ipables,但是iptables也可以继续安装使用,以下是firewall的操作命令
|
|
|
|
#### 1、启动防火墙
|
|
|
|
```
|
|
`systemctl start firewalld`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
2、查看防火墙的状态
|
|
|
|
```
|
|
`systemctl status firewalld`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 3、关闭防火墙
|
|
|
|
```
|
|
`systemctl stop firewalld`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 4、设置防火墙开机自启
|
|
|
|
```
|
|
`systemctl enable firewalld.service`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 5、关闭防火墙开机启动(需先关闭防火墙)
|
|
|
|
```
|
|
`systemctl disable firewalld.service`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|
#### 6、查看firewall的状态
|
|
|
|
```
|
|
`firewall-cmd --state`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 7、查看防火墙规则
|
|
|
|
```
|
|
`firewall-cmd --list-all`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 8、重新加载防火墙配置
|
|
|
|
```
|
|
`firewall-cmd --reload`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 9、查看当前开放端口(允许任意IP访问)
|
|
|
|
```
|
|
`firewall-cmd --zone=public --list-ports`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 10、开放指定端口
|
|
|
|
```
|
|
`firewall-cmd --zone=public --add-port=80/tcp --permanent
|
|
firewall-cmd --reload
|
|
// --permanent为永久生效,没有此参数 服务器重启后配置失效`
|
|
|
|
* 1
|
|
* 2
|
|
* 3
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
查看规则
|
|
|
|

|
|
|
|
#### 11、移除端口
|
|
|
|
```
|
|
`firewall-cmd --zone=public --remove-port=80/tcp --permanent
|
|
firewall-cmd --reload`
|
|
|
|
* 1
|
|
* 2
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
> 可以看到80端口已经被移除了
|
|
|
|
#### 12 、其他 命令 暂时用不到
|
|
|
|
##### 查看允许指定IP访问端口的规则
|
|
|
|
```
|
|
`firewall-cmd --zone=public --list-rich-rules`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|
##### 开放指定端口(只允许指定IP访问)
|
|
|
|
以“只允许192.168.1.1的主机连接3306端口”为例
|
|
|
|
```
|
|
`firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.1.1" port protocol="tcp" port="3306" accept" --permanent
|
|
firewall-cmd --reload`
|
|
|
|
* 1
|
|
* 2
|
|
|
|
|
|
```
|
|
|
|
PS. --permanent为永久生效,没有此参数centos重启后该配置将失效
|
|
|
|
##### 移除规则(只允许指定IP访问的端口)
|
|
|
|
以“移除只允许192.168.1.1的主机连接3306端口”为例
|
|
|
|
```
|
|
`firewall-cmd --remove-rich-rule="rule family="ipv4" source address="192.168.1.1" port protocol="tcp" port="3306" accept" --permanent
|
|
firewall-cmd --reload`
|
|
|
|
* 1
|
|
* 2
|
|
|
|
|
|
```
|
|
|
|
ps --permanent为永久生效,没有此参数centos重启后该配置将失效
|
|
|
|
##### 移除默认的ssh服务
|
|
|
|
移除默认的ssh服务前,先确认是否已经额外开放了22端口,否则会导致无法使用ssh 远程连接 。
|
|
|
|
```
|
|
`firewall-cmd --remove-service=ssh --permanent
|
|
firewall-cmd --reload`
|
|
|
|
* 1
|
|
* 2
|
|
|
|
|
|
```
|
|
|
|
PS. --permanent为永久生效,没有此参数centos重启后该配置将失效
|
|
|
|
##### 查看某个端口是否开放
|
|
|
|
以检查80端口为例(yes:开放;no:未开放)
|
|
|
|
```
|
|
`firewall-cmd --zone=public --query-port=80/tcp`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|
##### TCP端口转发
|
|
|
|
\# 允许防火墙伪装IP
|
|
|
|
```
|
|
`firewall-cmd --add-masquerade`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|
\# 将本地80端口的流量转发到192.168.1.1的80端口
|
|
|
|
```
|
|
`firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.1:toport=80 --permanent`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|
PS. --permanent为永久生效,没有此参数centos重启后该配置将失效
|
|
|
|
```
|
|
`查看版本:firewall-cmd --version
|
|
|
|
查看帮助: firewall-cmd --help
|
|
|
|
查看状态: firewall-cmd --state
|
|
|
|
查看区域信息: firewall-cmd --get-active-zones
|
|
|
|
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
|
|
|
|
拒绝所有包:firewall-cmd --panic-on
|
|
|
|
取消拒绝状态: firewall-cmd --panic-off
|
|
|
|
查看是否拒绝: firewall-cmd --query-panic`
|
|
|
|

|
|
|
|
* 1
|
|
* 2
|
|
* 3
|
|
* 4
|
|
* 5
|
|
* 6
|
|
* 7
|
|
* 8
|
|
* 9
|
|
* 10
|
|
* 11
|
|
* 12
|
|
* 13
|
|
* 14
|
|
* 15
|
|
|
|
|
|
```
|
|
|
|
### 2、Centos6
|
|
|
|
#### 1、查看当前防火墙的状态
|
|
|
|
```
|
|
`service iptables status`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 2、关闭防火墙
|
|
|
|
```
|
|
`service iptables stop`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 3、查看服务名
|
|
|
|
```
|
|
`setup
|
|
ls /etc/init.d/`
|
|
|
|
* 1
|
|
* 2
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
#### 4、自启动
|
|
|
|
```
|
|
`chkconfig功能说明:检查,设置系统的各种服务
|
|
语法:
|
|
chkconfig [--add][--del][--list][系统服务] 或
|
|
chkconfig [--level<等级代号>][系统服务][on/off/reset]
|
|
--add 添加服务
|
|
--del 删除服务
|
|
--list 查看各服务启动状态`
|
|
|
|
* 1
|
|
* 2
|
|
* 3
|
|
* 4
|
|
* 5
|
|
* 6
|
|
* 7
|
|
|
|
|
|
```
|
|
|
|
给每个服务的各个运行级别设置自启动/关闭
|
|
|
|
##### 1、查看服务
|
|
|
|
```
|
|
`chkconfig --list`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|

|
|
|
|
##### 2 查看单个服务 信息
|
|
|
|
```
|
|
`chkconfig iptables --list
|
|
chkconfig --list | grep iptables`
|
|
|
|
* 1
|
|
* 2
|
|
|
|
|
|
```
|
|
|
|
##### 3关闭自启动
|
|
|
|
```
|
|
`chkconfig --level 5 sshd off`
|
|
|
|
* 1
|
|
|
|
|
|
```
|
|
|
|
##### 4在所有模式下关闭/打开自启动
|
|
|
|
```
|
|
`chkconfig 服务名 on/off`
|
|
|
|
* 1
|
|
|
|
|
|
``` |