Linux网络相关、firewalld和netfilter 、netfilter5表5链介绍 、 iptables语法

linux网络相关

ifdown ens33 关闭网卡&& ifup ens33 启动网卡  (&&一起执行)

应用场景:当想单独更改某个网卡的信息时,可使用此命令

设定虚拟网卡

cd /etc/sysconfig/network-scripts/ 进入网卡目录
 cp ifcfg-ens33 ifcfg-ens33\:0 拷贝网卡配置文件

vi !$编辑配文件

修改NAME :ens33:0

DEVICE :ens33 :0

IPADDR 更换ip

删除dns1 gateway

修改后关闭,开启网卡。

查看网卡是否连接(link ok表示连接)

[root@g_linux01 network-scripts]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok

如果mii-tool ens33不支持  也可用ethtool ens33

[root@g_linux01 network-scripts]# 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

更改主机名

hostnamectl set-hostname [主机名] (centos7的命令)

配置文件在/etc/hostname 

dns配置文件

/etc/resolv.conf(可临时更改,想永久更改要在网卡配置文件中改)

etc/hosts 更改本机域名解析

 以先出现的记录为主。  这个文件也是从上到下依次执行的,127.0.0.1是特例,本机ip优先级高

firewalld和netfilter

复习selinux

修改配置文件中SELINUX=disabled

getenforce 可查询selinux是否开启

可能返回结果有三种:EnforcingPermissive 和 Disabled。Disabled 代表 SELinux 被禁用,Permissive 代表仅记录安全警告但不阻止可疑行为,Enforcing 代表记录警告且阻止可疑行为。

firewalld

centos7 默认开启firewalld 关闭netfilter,netfilter 是centos6的防火墙。但7依然可以用

关闭firewalld 流程

  1. systemctl disable firewalld 不开机启动
  2. systemctl stop firewalld    在关掉

开启netfilter

  1. 先安装yum install -y iptables-services
  2. systemctl enable iptables 设为开机启动
  3. systemctl start iptables 启动服务

netfilter5个表5个链介绍

filter,nat,mangle,raw,默认表是filter(没有指定表的时候就是filter表)。

          filter一般的过滤功能

          nat: 用于nat功能(端口映射,地址映射等)

          mangle: 用于对特定数据包的修改

          raw:优先级最高,设置raw时一般是为了不再让iptables做数据包的链接跟踪处理,提高性能

数据报从进入系统,进行IP校验以后,首先经过第一个HOOK函数NF_IP_PRE_ROUTING进行处理;
然后就进入路由代码,其决定该数据报是需要转发还是发给本机的;
若该数据报是发被本机的,则该数据经过HOOK函数NF_IP_LOCAL_IN处理以后然后传递给上层协议;
若该数据报应该被转发则它被NF_IP_FORWARD处理;
经过转发的数据报经过最后一个HOOK函数NF_IP_POST_ROUTING处理以后,再传输到网络上。
本地产生的数据经过HOOK函数NF_IP_LOCAL_OUT 处理后,进行路由选择处理,然后经过NF_IP_POST_ROUTING处理后发送出去。

 iptables语法

案例:http://blog.chinaunix.net/uid-9950859-id-98279.html 

查看默认规则

[root@g_linux01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 116K   28M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
11342  963K INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
11342  963K INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
11342  963K INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
11330  963K REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

重启规则service iptabales restart(重启后会加载配置文件中的规则)

保存规则service iptables save(修改后不保存,重启后会失效)

规则保存在 cat /etc/sysconfig/iptables(如果没有安装yum install iptables-services)

iptables -F 清空规则

iptables -t filter -nvL (指定显示哪个表的规则,不加-t 默认显示filter)

iptables -t nat -nvL

iptables -Z 清空计数器

限制某ip登录

iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DORP/REJECT

  • -A 增加一条规则,针对INPUT链(在最后插入规则)
  • -s 来源
  • -p 指定协议tcp 或者udp
  • --sport  端口1234
  • -d 目标ip
  • --dport 目标端口
  • -j 具体操作 DORP 拒绝(数据包来了,看都不看直接扔掉) REJECT(数据包来了,看后告诉他不能进来)

增加的规则在最后

-I 在规则最前插入

数据包进来后,会优先从前往后匹配规则(一旦匹配到就执行)

显示规则编号iptables -nvL --line-number

[root@g_linux01 ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      165 14676 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
5       83  7241 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

删除规则 iptables -D INPUT 5

更改默认规则,红框里就是默认规则

iptables -P OUTPUT DROP (只是例子哦,别执行,要不就断开远程连接了)

猜你喜欢

转载自my.oschina.net/u/3771583/blog/1649193
今日推荐