10.11 Linux网络相关html
10.12 firewalld和netfilterlinux
10.13 netfilter5表5链介绍shell
10.14 iptables语法vim
(拓展:末尾有端口映射,jpg)windows
'centos
10.11 Linux网络相关:服务器
~1. ifconfig 查看网卡ip (yum install net-tools)网络
centos7默认没有ifconfig这个命令,须要安装。ifconfig查看起来更加清爽dom
可是能够用ip addr 来查看tcp
ifconfig -a 当你的网卡down掉的时候,或者没有ip的时候,ifconfig是不显示的。加上-a就能够显示
~2.ifup ens33/ifdown ens33
ifdown ens33 ens33这个网卡断掉(也叫down掉)
ifup ens33 down掉以后,ifup从新链接
用在增长一个网卡或者更改一个网关,但不想重启所有的网络服务,就能够用ifdown ifup指定一个来用。远程链接的时候不要ifdown掉,可是能够这样用ifdown ens33 && ifup ens33
~3. 设定虚拟网卡ens33:1
后面lvs keepalived的时候,会用到这个虚拟网卡
操做步骤详见实例3.
~4.mii-tool ens33 查看网卡是否链接
咱们链接了网线,不在机房(可看灯有没有亮),远程链接的时候,检查是否网卡有没有插入网线
mii-tool ens33 结果是否是link ok
~5. ethtool ens33 也能够查看网卡是否链接
若是没有mii-tool能够用 ethtool ens33 ,检查最后一行是否是Link detected: yes
~6.更改主机名 hostnamectl set-hostname aminglinux
centos7使用的
更改以后,他的配置文件在 /etc/hostname
~7. DNS配置文件 /etc/resolv.conf
若是咱们更改DNS,就更改网卡的配置文件就能够了。由于/etc/resolv.conf也是网卡的配置文件(/etc/sysconfig/network-scripts/ens33)定义的。若是在/etc/resolv.conf更改DNS,固然也能够vim,可是重启以后会被网卡的配置文件所覆盖掉,适合临时更改。这个须要注意
~8. /etc/hosts文件
这个文件是linux和windows都有的。后面的lamp作实验,访问一个自定义的域名的时候就用到这个文件,可是在windows上更改的
假如,咱们ping www.qq.123.com,他是一个公网IP。可是我想让他在咱们的本机不要访问那个公网IP,要让他访问到192.168.159.150来,能够直接vim /etc/hosts,在最下面一行直接写 192.168.159.150 www.qq123.com。咱们在ping就会到咱们设定这个IP来,当即生效,但只在本机生效。可支持一个IP多个域名。同一个域名设置了多个IP,以最后一次设置的IP为准。以行为单位,左边IP,右边域名,以空格分割
实例:
3.
[root@axinlinux-01 ~]# cd /etc/sysconfig/network-scripts/ 咱们先cd进网卡的配置文件
[root@axinlinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:1 cp ens33重命名为ens:1。要脱意一下冒号
[root@axinlinux-01 network-scripts]# vim ifcfg-ens33:1 修改一下ens:1的配置
vim ifcfg-ens33\:0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33:0 找到NAME这一行名字改成ens:1(配置文件里不须要脱意)
UUID=d116f724-9bfb-4573-9f5b-7dace5640272
DEVICE=ens33 找到DEVICE这一行名字改成ens:1(配置文件里不须要脱意)
ONBOOT=yes:o
IPADDR=192.168.159.150
NETMASK=255.255.255.0
GATEWAY=192.168.159.2 将网关删掉
DNS33=159.29.29.29 将DNS33删掉,由于已经有一个DNS33了
[root@axinlinux-01 network-scripts]# ifdown ens33:1 && ifup ens33:1 指定重启ens33:1这个网卡
[root@axinlinux-01 network-scripts]# ifconfig 咱们在ifconfig一下
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.159.128 netmask 255.255.255.0 broadcast 192.168.159.255
inet6 fe80::20c:29ff:fe87:4224 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:87:42:24 txqueuelen 1000 (Ethernet)
RX packets 1446 bytes 152000 (148.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1168 bytes 161981 (158.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 就发现多了ens33:1了
inet 192.168.159.150 netmask 255.255.255.0 broadcast 192.168.159.255 并且也是能够pin通的
ether 00:0c:29:87:42:24 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 40 bytes 3168 (3.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 40 bytes 3168 (3.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4.5.
[root@axinlinux-01 network-scripts]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
[root@axinlinux-01 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
6.
[root@axinlinux-01 network-scripts]# cat /etc/hostname
axinlinux-01
8.
[root@axinlinux-01 network-scripts]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@axinlinux-01 network-scripts]# vim !$
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.150 www.qq123.com
[root@axinlinux-01 network-scripts]# ping www.qq123.com
PING www.qq123.com (192.168.159.150) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.159.150): icmp_seq=1 ttl=64 time=0.133 ms
[root@axinlinux-01 network-scripts]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.150 www.qq123.com www.12.com www.wangxin.com
192.168.159.128 www.wangxin.com
[root@axinlinux-01 network-scripts]# ping www.wangxin.com
PING www.qq123.com (192.168.159.128) 56(84) bytes of data.
64 bytes from www.wangxin.com (192.168.159.128): icmp_seq=1 ttl=64 time=0.108 ms
----------------------------------------------------------------------------------------------------------------------------------------------------
10.12 firewalld和netfilter:
linux防火墙-netfilter
~1. selinux临时关闭 setenforce o
咱们以前密钥认证的时候就要临时关闭
~2. selinux永久关闭 vi /etc/selinux/config
vi以后,将 SELINUX=enforcing 改成 disabled。getenforce一下显示enforcing。这个时候再setenforce o,再去getenforce一下,发现已经为permissive
permissive表示,虽然selinux已经开启了。可是仅仅是,遇到须要发生阻断的时候,他不须要真正的去阻断,仅仅是有一个提醒。咱们不会从屏幕上看到,可是有个地方会去记录
~3. centos7以前使用netfilter防火墙
都是能够用iptables来配置端口的
~4. centos7开始使用firewalld防火墙
都是能够用iptables来配置端口的
咱们能够在7上将firewalld关闭,将netfiter打开。是能够在7上使用netfilter的。方法以下:
先执行~6.(把firewalld停掉,不让他开机启动) > 在执行~5.(把这个服务关掉) > 而后再 执行~7. 把netfilter开启(实际上就是安装一下) > 再执行~8. (产生这个服务) > 最后执行~9. (开始这个服务)
iptables -nvL能够查看他的规则
iptables是netfilter的工具,而名字是叫netfilter
~5. systemctl stop firewalld
~6. systemctl disable firewallded
~7. yum install -y iptables-services
~8. systemctl enable iptables
~9. systemctl start iptables
实例:
2.
[root@axinlinux-01 network-scripts]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled 改成disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@axinlinux-01 network-scripts]# getenforce
Enforcing
[root@axinlinux-01 network-scripts]# setenforce 0
[root@axinlinux-01 network-scripts]# getenforce
Permissive
4.(将firewalld关闭,开启netfilter)
[root@axinlinux-01 network-scripts]# systemctl disable firewallded
Failed to execute operation: No such file or directory
[root@axinlinux-01 network-scripts]# systemctl stop firewalld
[root@axinlinux-01 network-scripts]# yum install -y iptables-services
[root@axinlinux-01 network-scripts]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@axinlinux-01 network-scripts]# systemctl start iptables
----------------------------------------------------------------------------------------------------------------------------------------------------
10.13 netfilter5表5链介绍:
linux防火墙—netfilter
~1. netfilter的5个表
~2. filter表用于过滤包,最经常使用的表,有INPUT、PORWARD、OUTPUT三个链
~3. nat表用于网络地址转换,有PREROUTING、OUTPUT、POSTROUTING三个链
~4. managle表用于给数据包表标记,几乎用不到
~5. raw表能够实现不追踪某些数据包,阿明历来不用
~6.security表在centos6中并无,用于强制访问控制(MAC)的网络规则,阿铭没用过
~7. 参考文章 http:www.cnblogs.com/metoy/4320813.html
linux防火墙-netfilter
~1. 数据包流向与netfilter的5个链
~2. PREROUTING:数据包进入路由表以前
~3. INPUT:经过路由表后目的地为本机
~4. FORWARD:经过路由表后,目的地不为本机
~5. OUTPUT:由本机产生,向外发出
~6. POSTROUTING:发送到网卡接口以前
----------------------------------------------------------------------------------------------------------------------------------------------------
10.14 iptables语法:
linux防火墙—netfilter
iptables相关的命令与用法:
~1. 查看iptables规则:iptables -nvL
service iptables restart 重启iptables
/etc/sysconfig/iptables 规则保存位置
详见实例1.
~2. iptables -F 清空规则
感受规则不合适的时候可所有清除。但需注意便是清除了规则,他的文件里(/etc/sysconfig/iptables)仍是有的
~3. service iptables save 保存规则
把当前的规则保存到文件里面去
可是咱们刚刚已经删除了,清空规则以后,咱们在保存规则保存的仅仅是空规则。咱们能够重启他的服务,让他从新加载回来(service iptables restart),
咱们写完的规则仅仅只是在内存中生效的,想让他重启后依然生效,就要service iptables save
也就是说,重启服务器或者是重启iptables,他都会从新加载配置文件里的规则
~4. iptables -t nat -t 指定表(不加-t就是默认filter表)
以上1 2 3 针对的都是filter表
写法为 iptables -t nat -nvL
固然net表里面没有规则
~5. iptables -Z 能够把计数器清零
iptables -nvL后,第二行是有数字的。pkts表示的是有多少包,byts表示的是数据量,数据大小(字节)
-Z以后数据清零,再次查看时,又会产生一些数据,是由于每时每刻都是在产生的
~6. iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
增长DROP的规则,来源IP是某某某IP的,访问咱们某一个端口的。或者来源端口是什么的。咱们给他DROP(扔掉
)掉
没有加-t,就是默认的filter表
-A 增长一条规则。后面的INPUT就是增长的链是INPUT链。
-s指定一个来源IP
-p指定协议。是tcp仍是udp或者icmp
sport 1234 来源的端口是1234
-d目标的IP
dport 80 目标的端口是80
最后操做 -j DROP ,DROP就是把数据直接给扔掉。还有一个 REJECT拒绝(来时会告诉他被拒绝了,比较有礼貌。DORP是看都不看直接扔掉)DORP用的比较多。扔掉和拒绝的效果都是同样的,都是让这个数据过不来。至关于把这个IP给封掉了
~7. iptables -I/-A/-D INPUT -s 1.1.1.1 -j DROP
iptables -I INPUT -p tcp --dport 80 -j DORP (在用-dport或-sport的时候,前面要指定哪一个协议)
根据~6. -A为增长一条规则,即排在后面。-I为插入一条规则,即为排在最前面(置顶)。排在最前面,咱们在匹配的时候回优先匹配最前面的规则,而后才会一条条的往下执行。若是你的这个规则,这个数据包已经匹配了第一条规则,好比咱们访问80端口的而且知足下面的一条规则,这两条规则同时知足。那么同时知足他会匹配第一条,一旦匹配了第一条这个数包就DORP掉了,就不会往下走了。一旦匹配直接执行
-D 删除一条规则,好比iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP(删除~6.那条规则)
~8. iptables -IINPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT
指定(-i)网卡为etho的
~9. iptables -nvL --line-numbers
假如,咱们好久之间写了一条很长的规则,就像~6.。咱们想把它删了,可是又想不起来这条规则是怎么写的。沃恩能够执行这条命令,在前面加上序号,用序号把他删掉。写法为 iptables -D INPUT 7
~10. iptables -D INPUT 1
利用序号,把规则删掉
~11. iptables -P INPUT DROP
-P指定链的默认的规则。像OUTPUT没有任何的规则。那么他就有默认的策略(ACCEPT)来决定。默认的策略就是ACCEPT。全部的数据包,只要是没有具体的规则来匹配,那么就走默认的策略。那么这个默认的策略也能够改,也能够改为DORP。写法为:iptables -P OUTPUT DROP。可是不要这么改默认的规则。一旦这么改了,就会连不上服务器,由于DORP掉了,数据回传不过来,xshell只能掉了。就只能回到服务器在改回来 iptables -P OUTPUT ACCEPT
默认的规则不要改,保持默认就能够了
实例:
1.
[root@axinlinux-01 ~]# iptables -nvL 红色部分是他的规则
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
61 7858 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
1 60 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
1 60 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
1 60 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
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
--
-
2.
[root@axinlinux-01 ~]# iptables -F 清除规则
[root@axinlinux-01 ~]# iptables -nvL 会发现就没有规则了
Chain INPUT (policy ACCEPT 23 packets, 1924 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 15 packets, 1608 bytes)
pkts bytes target prot opt in out source destination
3.
[root@axinlinux-01 ~]# service iptables restart 重启iptables 在查看规则
Redirecting to /bin/systemctl restart iptables.service 发现已加载回来
[root@axinlinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
22 1872 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
4.
[root@axinlinux-01 ~]# iptables -t nat -nvL 空规则
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
6.
[root@axinlinux-01 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@axinlinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
197 16288 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
2 458 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80
以上-选项相对应的。 即为新增长的DORP规则
扩展:
图为 端口映射: