一、Linux网络相关的一些命令
1. 查看网卡的命令
ifconfig -a
ip addr 这两个命令都可以查看网卡,当网卡down掉的时候是看不到的,使用 -a可以查看到
ifdown ifup +网卡名 可以关闭和开启网卡
hostnamectl set-hostname lx003 更改主机名为lx003
/etc/hostname hostname 的配置文件存放地址
/etc/resolv.conf 查看DNS的配置文件
2. 单独针对网卡进行操作,最好是两条命令一起用,避免出错
[root@lx01 ~]# ifdown ens33 && ifup ens33
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/24)
3.设置虚拟网卡
1.[root@localhost ~]# cd /etc/sysconfig/network-scripts/ #进入网卡配置的目录
2.[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:1 # 复制一份网卡的配置文件,更名为ens33:1 反斜杠表示脱义
3.[root@localhost network-scripts]# vi ifcfg-ens33:1 #编辑配置文件
NAME=ens33:1
DEVICE=ens33:1
可以删除网关和DNS
重新设置ip在一个网段
4.[root@localhost network-scripts]# ifdown ens33:1 && ifup ens33:1 #重启网卡配置生效
4.
mii-tool
ethtool 这两个命令都可以查看网卡是否连接成功, mii-tool 不能使用的时候可以用 ethtool
[root@localhost network-scripts]#
mii-tool ens33
SIOCGMIIPHY on 'ens33' failed: Operation not supported #提示不支持
[root@localhost network-scripts]# ethtool ens33 #使用ethtool命令
Settings for ens33:
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes #Link detected为yes表示连接成功,no为位连接
5.编辑 /etc/hosts 文件,添加IP和域名,实现域名跳转
1. [root@localhost network-scripts]# ping www.qq.com #ping
www.qq.com 可以正常ping
PING www.qq.com (14.17.42.40) 56(84) bytes of data.
64 bytes from 14.17.42.40 (14.17.42.40): icmp_seq=1 ttl=128 time=11.0 ms
--- www.qq.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 11028ms
rtt min/avg/max/mdev = 7.342/42.114/107.962/46.586 ms
2. [root@localhost network-scripts]# vim /etc/hosts #编辑配置文件,将域名跳转到
192.168.177.66
在配置文件里添加以下行:
192.168.177.66 www.qq.com
3.[root@localhost network-scripts]# ping www.qq.com
PING www.qq.com (192.168.177.66) 56(84) bytes of data.
64 bytes from www.qq.com (192.168.177.66): icmp_seq=1 ttl=64 time=0.049 ms #可以看到ping
www.qq.com 时跳转到
192.168.177.66
--- www.qq.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.049/0.094/0.179/0.060 ms
二、firewalld和netfilter
1.永久关闭selinux防火墙
[root@localhost ~]#
vi /etc/selinux/config
#编辑配置文件
将SELINUX=enforcing更改为
disabled
2.
getenforce 和
setenforce 0
1. [root@localhost ~]#
getenforce
#使用
getenforce可以查看selinux的状态,
Enforcing 是打开的意思
Enforcing
2. [root@localhost ~]#
setenforce 0
#
使用
setenforce 0 临时关闭selinux,
3. [root@localhost ~]#
getenforce
Permissive #
临时关闭selinux后,状态变更为
Permissive,要到阻断的时候,不会去阻拦,会警告和记录
3. netfilter和firewalld都是linux的防火墙,在centos7之前使用的是netfilter,之后使用的是firewalld。
今天学习的是netfilter,所以要先关闭firewalld,再开启netfilter。
1.[root@lx003 ~]#
systemctl disable firewalld
##暂停
firewalld服务
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
2.[root@lx003 ~]#
systemctl stop firewalld
##停止进程
3.[root@lx003 ~]#
yum install -y iptables-services
##安装
iptables-services
4.[root@lx003 ~]#
systemctl enable iptables
##启动服务
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
5.[root@lx003 ~]#
systemctl start iptables
##启动进程
4.
iptables -nvl
查看iptables的规则
[root@localhost ~]#
iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
60 3952 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 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)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 38 packets, 5864 bytes)
pkts bytes target prot opt in out source destination
三、
netfilter5表5链介绍
1.netfilter的5个表
filter : INPUT FORWARD OUTPUT
nat : PREROUTING POSTROUTING OUTPUT
mangle : PREROUTING INPUT OUTPUT POSTROUTING
raw : PREROUTING OUTPUT
security : SECMARK and CONNSECMARK INPUT OUTPUT FORWARD
iptables的基础知识(表->链->规则)
1、规则(rules)
它其实就是网络管理员预定义的条件,规则一般的定义为“如果数据包头符合这样的条件,就这样处理这个数据包”。规则存储在内核空间的信息包过滤表中,这些规 则分别指定了源地址、目的地址、传输协议(如TCP、UDP、ICMP)和服务类型(如HTTP、FTP和SMTP)等。当数据包与规则匹配 时,iptables就根据规则所定义的方法来处理这些数据包,如放行(accept)、拒绝(reject)和丢弃(drop)等。配置防火墙的主要工 作就是添加、修改和删除这些规则
2、链(chains)
它是数据包传播的路径,每一条链其实就是众多规则中的一个检查清单,每一条链中可以有一条或数条规则。当一个数据包到达一个链时,iptables就会从链中第一条规则开始检查(即:检查的顺序:从上到下),看该数据包是否满足规则所定义的条件。如果满足,系统就会根据该条规则所定义的方法处理该数据包;否则iptables将继续检查 下一条规则,如果该数据包不符合链中任一条规则,iptables就会根据该链预先定义的默认策略来处理数据包
3、表(tables)
它提供特定的功能,iptables内置了5个表,即filter表、nat表、mangle表、raw表、security表,分别用于实现包过滤,网络地址转换、包重构(修改),数据跟踪处理、强制访问控制(MAC)的网络规则
链的作用:input(进入)、output(出去)、forward(转发),而prerouting与postrouting用于网络地址转换(NAT)
四、iptables语法
1. iptables的一些选项:
/etc/sysconfig/iptables #
iptables的配置文件保存在这里
iptables -F #清空iptables的规则,但是不会更改配置文件
service
iptables save #如果要使规则保存到配置文件,那么执行这个命令
service iptables restart #如果不想保存到配置文件,那么重启服务即可,规则会重新加载
iptables -t #指定表,如果指定表,那么默认更改的为filter表
-t<表>:指定要操纵的表;
-A:向规则链中添加条目;
-D:从规则链中删除条目;
-I:向规则链中插入条目;
-R:替换规则链中的条目;
-L:显示规则链中已有的条目;
-F:清楚规则链中已有的条目;
-Z:清空规则链中的数据包计算器和字节计数器;
-N:创建新的用户自定义规则链;
-P:定义规则链中的默认目标;
-h:显示帮助信息;
-p:指定要匹配的数据包协议类型;
-j<目标>:指定要跳转的目标;
-i<网络接口>:指定数据包进入本机的网络接口;
-o<网络接口>:指定数据包要离开本机所使用的网络接口
2. iptables的一些示例
1. 增加一条规则
iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
-A 为增加,指定链为 INPUT链,-s 指定来源IP -p 指定协议
tcp ,--sport 指定端口 ,-d 指定目标IP,--dport 指定目标端口,-j 指定动作,DROP为扔掉 REJECT拒绝
2.插入一条规则
[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
这种用法也可以,当使用dport时,一定要使用-p指定协议,-I和-A的区别时i为insetr插入的意思,A为ADD增加的意思,使用i的时候规则会在最上面
3.删除一条规则
iptables -D INPUT -p tcp --dport 80 -j DROP
使用-D即可删除
4. 使用编号删除规则
当我们查看规则的时候可以加上 --line-number ,这样可以显示规则的编号,红色加粗的部分就编号
[root@localhost ~]#
iptables -nvL --line-numbe
r
Chain INPUT (policy ACCEPT 289 packets, 21662 bytes)
num
pkts bytes target prot opt in out source destination
1
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2
0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 215 packets, 19040 bytes)
num pkts bytes target prot opt in out source destination
那我们删除规则的时候就可以使用ID号
[root@localhost ~]#
iptables -D INPUT 1
# 指定INPUT链,删除ID号为1的规则
3. 当我们不设置规则的时候,那么就会走默认的策略,默认的策略一般为
ACCEPT,
Chain INPUT (policy ACCEPT 45 packets, 3120 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80
Chain FORWARD (
policy ACCEPT
0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (
policy ACCEPT
30 packets, 2464 bytes)
pkts bytes target prot opt in out source destination
那我们是可以更改默认规则的
[root@localhost ~]# iptables -P OUTPUT DROP
更改OUTPUT链的默认规则为DROP,但是最好不要更改,因为OUTPUT全部DROP后就不能联网了