Day35-Linux网络管理5

1. 网卡配置

接口命名方式:
CentOS6以前eth[0,1,2,…]
CentOS7开始ens[33,34,35,…]

#调整ens33为eth0方式
安装系统时:grep菜单内核后:biosdevname=0,net.ifnames=0
安装系统后:修改内核文件,然后重新加载内核文件。

启停网卡设备:

ifdown eth0                ##停止网卡设备
ifup eth0                  ##启动网卡设备

ifconfig eth0 down         #停止网卡设备
ifconfig eth0 up           #启动网卡设备

ip link set eth0 up        #启动网卡设备
ip link set eth0 down      #启动网卡设备

systemctl restart network  ##重启网络 /etc/init.d/network restart

C6以前常规的启动方式:

/etc/init.d/network restart   #和service network restart等价 #C6其他服务也是这么起。

扩展作业:如何绑定网卡bond?

配置文件:

/etc/sysconfig/network-scripts/ifcfg-eth0

[root@oldboy ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE="Ethernet"   *****
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"  *****
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="eth0"   *****
UUID="a8f8de70-844c-4571-a705-be74bd8089b6" *****
DEVICE="eth0" *****
ONBOOT="yes"  *****

网卡配置方式:

  • 静态地址:适合服务器。
    1)配置文件 vim /etc/sysconfig/network-scripts/ifcfg-eth0
    2)ifconfig/ip 可以临时配置。
    3)nmtui ##图形配置,依赖NetworkManager,C6:setup
    4)nmcli #复杂不好用。

  • 动态分配:适合办公室员工PC,手机。
    DHCP: Dynamic Host Configuration Protocol
    扩展作业:DHCP工作原理。

2. DNS客户端域名解析配置

配置文件/etc/resolv.conf

root@oldboy ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 223.5.5.5
nameserver 202.106.0.20

#一定是2个。将来有机会自己配置DNS服务器(bind,powerdns),几乎没有人配置。

大规模网站【局域网】DNS,替代hosts。公网DNS,阿里,DNSPOD 提供商。
意义:
1.面试。
2.临时改一下。

在网卡配置文件配置了,覆盖/etc/resolv.conf

[root@oldboy ~]# grep -i dns /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=202.106.0.20
DNS2=223.5.5.5

DNS重点:DNS解析原理。
hosts和DNS区别。
host通过分发文件到主机实现域名解析。小规模环境(所有客户机主机能控制)
dns通过配个客户端地址,访问的时候,去请求DNS解析。大规模环境
dns功能不仅限于此。

#hosts本地主机名和IP地址映射
getent hosts查看/etc/hosts内容

默认优先于使用DNS客户端配置
#修改/etc/hosts和DNS客户端优先级
/etc/nsswitch.conf
hosts: files dns

[root@oldboy ~]# cat /etc/nsswitch.conf
...
#hosts:     db files nisplus nis dns
hosts:      files dns myhostname
...

3. 给网卡配多个IP

别名或辅助IP

将多个IP地址绑定到一个网卡上
每个IP绑定到独立逻辑网卡,即网络别名,
命名格式:如:eth0:0、eth0:1、eth0:2

4. ip地址查看和设置

4.1 ifconfig命令

配置别名:ifconfig ###(yum install net-tools -y)

来自net-tools包,很好用,C6前的工具,C7多用ip。
ifconfig,查看配置别名ip等。

别名IP:VIP虚拟IP

ifconfig eth0:1 10.0.0.25/24 up  #配置别名IP,VIP
ifconfig eth0:1                  #查看
ifconfig eth0:1 down             #停掉别名IP

例:

ifconfig eth1:0 10.0.0.10 netmask 255.255.255.0 up
ifconfig eth1:0 10.0.0.11/24 up

heartbeat高可用高可用软件(VIP)早期用的别名IP,就是ifconfig起的IP地址。
用途:做集群的VIP,当主服务器宕机,在从服务器漂移VIP,实现访问。
企业案例:更换局域网路由器的时候, 即使配置好所有配置和IP和老的一样,切换也会遇到问题。
要记得做ARP缓存清理通知,更换路由器,负载均衡、防火墙。

Usage: arping [-fqbDUAV] [-c count] [-w timeout] [-I device] [-s source] destination

  -f : quit on first reply
  -q : be quiet
  -b : keep broadcasting, don't go unicast
  -D : duplicate address detection mode
  -U : Unsolicited ARP mode, update your neighbours
  -A : ARP answer mode, update your neighbours
  -V : print version and exit
  -c count : how many packets to send        #包数量
  -w timeout : how long to wait for a reply  #超时
  -I device : which ethernet device to use   #设备
  -s source : source ip address
[root@oldboy ~]# arping -I eth0 -c 5 -s 10.0.0.129 10.0.0.254
ARPING 10.0.0.254 from 10.0.0.129 eth0
Unicast reply from 10.0.0.254 [00:50:56:E0:DD:A3]  0.673ms
Unicast reply from 10.0.0.254 [00:50:56:E0:DD:A3]  1.719ms

ip地址查看和设置:

4.2 ip命令

用ip的方式配多个IP,被称作辅助IP

例:

#添加IP
ip addr add 10.0.0.15/24  dev eth1 label eth1:2     #推荐

#删除IP
ip addr del 10.0.0.15/24 dev eth1

#删除IP
ifconfig eth1:2 down
ip add #ip a

辅助IP形式:keepalived高可用软件一直都是用的辅助IP做VIP。
ip的方式未来趋势。

网卡重启不失效:
写成配置文件(/etc/sysconfig/network-scripts/ifcfg-eth0:1)。

ip命令功能:

  1. ip地址 #ifconfig
  2. 设置路由。#route
[root@oldboy ~]# ip
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | addr | route | }
       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -h[uman-readable] | -iec |
                    -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |
                    -4 | -6 | -I | -D | -B | -0 |
                    -l[oops] { maximum-addr-flush-attempts } | -br[ief] |
                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
                    -rc[vbuf] [size] | -n[etns] name | -a[ll] |?-c[olor]}

ip addr flush dev eth0 label eth0:0

4.3 ip命令:查看和设置网络配置

在这里插入图片描述

来自iproute包,可代替ifconfig

man ip

ip [ OPTIONS ] OBJECT { COMMAND | help }

OPTIONS := { -V[ersion] | -h[uman-readable] | -s[tatistics] | -d[etails] | -r[esolve] | -iec | -f[amily] {
inet | inet6 | ipx | dnet | link } | -4 | -6 | -I | -D | -B | -0 | -l[oops] { maximum-addr-flush-
attempts } | -o[neline] | -rc[vbuf] [size] | -t[imestamp] | -ts[hort] | -n[etns] name | -a[ll] |  -c[olor] }

OBJECT := {link |address(addr) |route |neigh | }
[root@oldboy ~]# rpm -ql net-tools|grep bin
/bin/netstat *****
/sbin/arp *****
/sbin/ether-wake
/sbin/ifconfig *****
/sbin/ipmaddr  *****
/sbin/iptunnel *****
/sbin/mii-diag
/sbin/mii-tool *****
/sbin/nameif
/sbin/plipconfig
/sbin/route    *****
/sbin/slattach

4.4 ip命令帮助

ip addr help
ip route help
[root@oldboy ~]# rpm -ql iproute|grep bin
/usr/sbin/cbq  #限速
/usr/sbin/ip   #大全命令
/usr/sbin/ss   #类似netstat
/usr/sbin/tc   #限速

ip link - network device configuration
set dev IFACE,可设置属性:up and down:激活或关闭

例:

#禁用网卡
ip link set eth0 down #禁用指定接口,相当于ifdown

#启用网卡
ip link set eth0 up   #启用指定接口,相当于ifup

#网卡辅助IP
ip addr add 10.0.0.15/24  dev eth1 label eth1:2
ip addr del 10.0.0.15/24 dev eth1
ip add #ip a

#清除网络地址
ip addr flush dev eth0 

5. 路由

路由:为去某一个地方选择一个指定路径
路由功能:路由器,工作在网络层

5.1 路由功能分类:

  • 主机路由:(不重要)
  • 网络路由:
    –静态路由:route、ip route *****
    –动态路由:ospf、rip (了解)
  • 默认路由:默认网关

优先级:精度越高,优先级越高

重点静态路由:面试必考还是工作都很重要

5.2 查看路由:

route -n      #推荐
[root@oldboy ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.254      0.0.0.0         UG    100    0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0

netstat -rn
[root@oldboy ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.0.0.254      0.0.0.0         UG        0 0          0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0

ip route show
[root@oldboy ~]# ip route show
default via 10.0.0.254 dev eth0 proto static metric 100
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.200 metric 100

route命令
路由表管理命令

5.3 路由表:

[root@oldgirl fd]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.254      0.0.0.0         UG    101    0        0 eth1
10.0.0.0        0.0.0.0         255.255.255.0   U     101    0        0 eth1

路由表主要构成:

  • Destination: 目标网络ID,表示可以到达的目标网络ID,0.0.0.0/0 表示所有未知网络,又称为默认路由,优先级最低
  • Gateway: 到达非直连的网络,将数据发送到临近(下一个)路由器的临近本主机的接口的IP地址,如果是直连网络,gateway是0.0.0.0
  • Genmask:目标网络对应的netmask
  • Metric: 值越小,路由记录的优先级最高
  • Iface: 到达对应网络,应该从当前主机哪个网卡发送出来

在这里插入图片描述

1.在网关上面加路由
2.主机上配置静态路由

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

5.4 网络路由说明:

即去往某一网络或网段的路由;一般多网段之间互相通信,希望建立一条优先路由,而不是通过默认网关时就可以配置网络路由。还是拿房子比喻,你现在不是要出门,而是卧室,卫生间,去卧室就要经过卧室的门,去卫生间也要经过卫生间的门,这里的卧室和卫生间的门就可以认为是去往某一网段的路由,而不是默认路由(即房子的门。)
实际工作中会有需求,两个不同的内部网络之间互访,而不是出网访问,就是上面例子的情况。

5.5 路由命令:

route
ip route
添加:route add

route add  [-net|-host|default]  target [netmask Nm] [gw GW] [[dev] If]
-host   主机路由(两台主机之间配置)
-net    网络路由
default 默认路由

-host 主机路由(两台主机之间配置)
高可用对服务器之间早期用双绞线直连线。
如果添加一个主机路由?
主机路由说明:
就是去往某个主机地址如何配置路由
解答本地问题方法:

/sbin/route add -host 192.168.2.13 dev eth2
/sbin/route add -host 202.81.11.91 dev lo

keepalived或heartbeat高可用服务器对之间的使用单独网卡接心跳线通信就会用到以上主机路由。

[root@oldboy ~]# route add -host 10.0.0.130 dev eth0
[root@oldboy ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth1
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.0.0.0        0.0.0.0         255.255.255.0   U     101    0        0 eth1
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0
10.0.0.130      0.0.0.0         255.255.255.255 UH    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

5.6 default 默认路由

添加删除默认网关:上网默认出口,所有路由都不匹配的情况下,最后才会选择默认路由。

route del default gw 10.0.0.254 dev eth1
route add default gw 10.0.0.254 dev eth1

添加/删除静态路由

route add -net 10.0.1.0/24 gw 10.0.0.254 dev eth1
route del -net 10.0.1.0/24

5.7 企业案例:老男孩培训-第七节节课课前考试题route命令总结答案

例:通过[配置静态路由实现不同网络互通]实践
#第一步环境

A :ip:10.0.0.129,gw:10.0.0.254
B :路由:eth0:10.0.0.2,eth1:192.168.1.1
   注意:不配GW
C :192.168.1.7,gw:192.168.1.1

注意:服务器不要有多余网卡开启,关闭其他网卡。
检查:在B上ping 10.0.0.129,ping 192.168.1.7 通畅。

#第二步配置路由
在A配置静态路由

route add -net 192.168.1.0/24 gw 10.0.0.2

#第三步开启路由转发功能
在B配置路由转发

vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
#注意:sysctl -p

#第四步验证
从A上ping C
ping 192.168.1.7 如果返回证明成功。
如果故障:tcpdump抓包

#配置动态路由
通过守护进程获取动态路由,安装quagga包,通过命令vtysh配置
支持多种路由协议:

RIP: Routing Information Protocol,路由信息协议
OSPF:Open Shortest Path First,开放式最短路径优先
BGP: Border Gateway Protocol,边界网关协议 
RIP、OSPF和BGP 

#管理路由
*ip route 用法

#添加路由:

ip route add TARGET via GW dev IFACE src SOURCE_IP        

TARGET:
主机路由:IP
网络路由:NETWORK/MASK

#添加网关:

ip route add default via GW dev IFACE

ip route add default via 10.0.0.253 dev eth1
ip route add default via 10.0.0.253

#删除路由:

ip route del TARGET 
ip route del default via 10.0.0.254

#显示路由:

ip route show|list

#清空路由表:

ip route flush  [dev IFACE]     [via PREFIX]

例:

ip route add 192.168.1.0/24 via 10.0.0.2 dev eth1
ip route add 192.168.1.0/24 via 10.0.0.2
ip route add default via 10.0.0.253

ip route flush dev eth0   #不轻易执行

默认网关直接在网卡配置里配置,网络路由、主机路由都可以放到/etc/rc.local

5.8 路由相关配置文件

/etc/sysconfig/network-scripts/route-IFACE

如果要是永久生效,有如下几种方法:
方法一:利用route-eth0文件

# vi /etc/sysconfig/network-scripts/route-eth0(C7)		<- 默认不存在此文件
# 加入如下内容:
192.168.1.0/24 via 10.0.0.2
提示:写到配置里,重启网络服务和重启系统都会生效!
route add -net 192.168.1.0/24 gw 10.0.0.2

方法二:利用static-routes文件(C6)

# vi /etc/sysconfig/static-routes					<- 默认不存在此文件
# 加入如下内容:
any net 172.16.1.0/24 gw 192.168.1.1
提示:写到配置里,重启网络服务和重启系统都会生效!

方法三:利用rc.local文件

# vi /etc/rc.local
# 加入如下内容:
route add -net 192.168.1.0/24 gw 10.0.0.2
提示:写到配置里,重启系统会生效!

例:

[root@oldboy ~]# cat /etc/sysconfig/network-scripts/route-eth1
192.168.1.0/24 via 10.0.0.2
[root@oldboy ~]# /etc/init.d/network restart
[root@oldboy ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth1
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
192.168.1.0     10.0.0.2        255.255.255.0   UG    0      0        0 eth1

例: 查看路由过程

[root@oldgirl ~]# ip route get 10.0.0.254
10.0.0.254 dev eth1 src 10.0.0.129 
    cache 
  • 31
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值