Centos7經常使用操做

1.虛擬機環境下centos的防火牆能夠關閉,不然主機調用虛擬機的相關服務時須要調整相應端口:vim

CentOS7:centos

systemctl stop firewalld.service #中止
systemctl disable firewalld.service #禁用
以前的版本:
service iptables stop #中止
chkconfig iptables off #禁用
網絡

 

永久關閉防火牆: chkconfig iptables offssh

檢查防火牆狀態: service iptables statustcp

 

CentOS7防火牆firewalld詳解

centos7的防火牆:工具

centos7再也不使用iptables,而是使用firewalldcentos7

若不想使用firewalld,而繼續使用iptables,能夠先停掉firewalld,而且安裝iptables-services包spa

查看firewalld狀態:systemctl status firewalld3d

停掉firewalld:systemctl stop firewalldxml

開機不啓動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 //從新加載

相關文章
相關標籤/搜索