firewalld和netfilter、netfilter5表5鏈介紹、iptables語法

Linux網絡相關

ifconfig命令查看網卡IP
剛接觸linux系統開始的時候咱們學習到查看網卡IP方式是使用ip addr,而ifconfig命令和ip addr命令的結果是類似的,若是系統裏沒有ifconfig命令,安裝便可;linux

[root@Ask-02 ~]# yum install -y net-toolsvim

在linux下修改ip須要進入到/etc/sysconfig/network-scripts/目錄下面修改相關的網卡文件便可,好比;安全

[root@Ask-02 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33網絡

在linux上有多個網卡,而你只想重啓某個網卡的時候,可使用如下操做;dom

[root@Ask-02 ~]# ifdown ens33; ifup ens33
或者
[root@Ask-02 ~]# ifdown ens33 && ifup ens33tcp

因爲是遠程登陸的,因此在使用ifdown的時候須要謹慎,不要單獨使用ifdownide

給一個網卡設定多個ip學習

[root@Ask-02 ~]# cd /etc/sysconfig/network-scripts/ //先到網卡配置文件的目錄下
[root@Ask-02 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:1 //拷貝一個ens33文件名並重命名爲ens33:1 這裏用到反斜槓使用脫義rest

而後開始編輯ens33:1文件便可,修改NAMEDEVICEens33:1 設定一個IPADDR地址保存並退出,重啓網卡便可
這個時候在ifconfig查看就能夠看到多了一個網卡code

查看網卡鏈接狀態

[root@Ask-02 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok

這裏顯示link ok,就說明網卡爲鏈接狀態,若是顯示no link,說明網卡存在壞了或者沒有鏈接網線的狀況,除了這個mii-tool命令外還有一個命令也能夠查看;

[root@Ask-02 ~]# ethtool ens33
Settings for ens33:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
MDI-X: off (auto)
Supports Wake-on: d
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes

若是網卡沒有鏈接成功,最後面一行link detected顯示爲no

更改主機名

使用hostname命令能夠查看主機名,hostname命令後面跟一個自定義的名字則是臨時修改主機名,重啓後無效

[root@Ask-02 ~]# hostname
Ask-02
[root@Ask-02 ~]# hostname Ask-03**

想要永久改主機名,執行如下命令修改

[root@Ask-02 ~]# hostnamectl set-hostname Ask-03

修改後從新登陸便可看到變化

設置DNS

在linux下設置DNS很是簡單,只要把DNS地址寫入配置文件便可

[root@Ask-02 ~]# cat /etc/resolv.conf
·# Generated by NetworkManager
nameserver 119.29.29.29
[root@Ask-02 ~]# vi /etc/resolv.conf
[root@Ask-02 ~]# cat /etc/resolv.conf
·# Generated by NetworkManager
nameserver 119.29.29.29
nameserver 8.8.8.8

在linux下還有一個特殊文件/etc/hosts也能解析域名

[root@Ask-02 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

使用Vim編輯該文件,增長一行192.168.135.133 www.baidu.com,保存後再ping如下www.baidu.com就會鏈接到192.168.135.133

[root@Ask-02 ~]# vim /etc/hosts
[root@Ask-02 ~]# ping -c 2 www.baidu.com
PING www.baidu.com (192.168.135.133) 56(84) bytes of data.
64 bytes from www.baidu.com (192.168.135.133): icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from www.baidu.com (192.168.135.133): icmp_seq=2 ttl=64 time=0.370 ms

--- www.baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.045/0.207/0.370/0.163 ms

hosts文件格式很簡單,注意如下幾點;

* 一個ip後面能夠跟多個域名,能夠是幾十個甚至上百個;
* 每一行只能有一個ip,也就是說一個域名不能對應多個ip;
* 若是有多行中出現相同的域名(對應的ip不同),就會按最前面出現的記錄來解析

firewalld和netfilter

linux的防火牆

SELinux是linux系統特有的安全機制。由於這種機制的限制太多,配置也繁瑣,因此幾乎沒有人真正的應用它,安裝玩系統咱們通常都把SELinux關閉,以避免引發沒必要要的麻煩,臨時關閉的犯法;

[root@Ask-02 ~]# setenforce 0

永久關閉的方法,須要更改配置文件/etc/selinux/config,把SELINUX=enforcing改爲SELINUX=disabled保存並退出便可,重啓系統後生效,此處切記不要改做地方以避免照成系統沒法開機。

[root@Ask-02 ~]# vi /etc/selinux/config
[root@Ask-02 ~]# getenforce //修改好重啓系統後可使用該命令查看selinux防火牆狀態
Disabled

netfilter是在Centos7以前使用的防火牆,Centos7使用的是firewalld,因爲大部分公司仍是使用Centos6的狀況,咱們能夠先經過以前版本的iptables瞭解和學習,firewalldnetfilter 使用的命令一樣支持的,意味這二者兼容。
所以咱們先關閉firewalld後再開啓netfilter ,操做以下;

[root@Ask-02 ~]# systemctl stop firewalld //關閉firewalld服務
[root@Ask-02 ~]# systemctl disable firewalld //禁止firewalld服務開機啓動
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@Ask-02 ~]# yum install -y iptables-services //安裝iptables-services ,使以前的版本可使用(即netfilter )
[root@Ask-02 ~]# systemctl enable iptables //讓服務開機啓動
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@Ask-02 ~]# systemctl start iptables //啓動iptables服務

到此就設置好了,可使用以前版本的iptables
使用如下命令能夠查看規則;

[root@Ask-02 ~]# iptables -nvL

netfilter5表5鏈介紹

netfilter的5個表
filter:表主要用於過濾包,是系統預設的表,該表內建3個鏈:INPUT、OUTPUT以及FORWARDINPUT鏈做用於進入本機的包,OUTPUT鏈做用於本機送出的包,FORWARD鏈做用於那些跟本機無關的包。
nat:表主要用於網絡地址轉換,一樣也有3個鏈,PREROUTING鏈的做用是在包剛剛到達防火牆時改變它的目的地址,OUTPUT鏈的做用是改變本地產生的包的目的地址,POSTROUTING鏈的做用是在包即將離開防火牆時改變其源地址。該表阿銘僅偶爾會用到。
mangle:表主要用於給數據包作標記,而後根據標記去操做相應的包。這個表幾乎不怎麼用
raw:表能夠實現不追蹤數據包作標記,默認系統的數據包都會被追蹤,但追蹤勢必消耗必定的資源,因此能夠用raw表來指定某些端口的包不被追蹤。
security:表在Centos6裏是沒有的,它用於強制訪問控制(MAC)的網絡規則。

netfilter的5個鏈
5個鏈分別爲PREROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING
PREROUTING:數據包進入路由表以前
INPUT:經過路由表後目的地爲本機
FORWARD:經過路由表後,目的地不爲本機
OUTPUT:由本機產生,向外轉發
POSTROUTING:發送到網卡接口以前

參考圖
firewalld和netfilter、netfilter5表5鏈介紹、iptables語法
firewalld和netfilter、netfilter5表5鏈介紹、iptables語法

iptables語法

查看iptables默認規則配置文件:

[root@Ask-02 ~]# cat /etc/sysconfig/iptables

查看iptables默認規則:

[root@Ask-02 ~]# iptables -nvL

保存iptables規則(當前規則,保存到配置文件):

[root@Ask-02 ~]# service iptables save

清空iptables規則:

[root@Ask-02 ~]# iptables -F

重啓iptables規則:

[root@Ask-02 ~]# service iptables restart

指定表:

[root@Ask-02 ~]# iptables -t nat -nvL
[root@Ask-02 ~]# iptables -t filter -nvL

把表的計數器清零:

[root@Ask-02 ~]# iptables -nvL

若是不加-t,則默認的是filter

  • -A/-D:表示增長/刪除一條規則
  • -I:表示插入一條規則其實效果跟-A同樣
  • -p:表示指定協議,能夠是tcp、udp或者icmp
  • --dport:跟-p一塊兒使用,表示指定目標端口
  • --sport:跟=p一塊兒使用,表示指定源端口
  • -s:表示指定源IP(能夠是一個IP段)
  • -d:表示指定目的IP(能夠是一個IP段)
  • -j:後面跟動做,其中ACCEPT表示容許包,DROP表示丟掉包,REJECT表示拒絕包
  • -i:表示指定網卡
相關文章
相關標籤/搜索