管理Linux网络
linux设备识别
以太网卡 eth# eth0 eth1
无线网卡 wifi# wifi0 wifi1
拨号连接 ppp# ppp0 ppp1
本地环回网卡 lo
临时更改IP地址设置
立即生效,重启系统、重启网络服务后失效
ifconfig
查看网络连接的设置
查看所有工作的网络连接
[root@learn ~]# ifconfig
查看eth1网卡的设置
[root@learn ~]# ifconfig eth1
禁用eth1网卡
[root@learn ~]# ifdown eth1
[root@learn ~]# ifconfig eth1 down
启用eth1网卡
[root@learn ~]# ifup eth1
[root@learn ~]# ifconfig eth1 up
能够看到所有网卡包括禁用的和启用的
[root@learn ~]# ifconfig -a
更改IP地址和网关
[root@learn ~]# ifconfig eth1 192.168.1.100 netmask 255.255.255.0
[root@learn ~]# ifconfig eth1 192.168.1.100/24
不指定netmask就是默认子网掩码
[root@learn ~]# ifconfig eth1 192.168.1.100
更改网卡的MAC地址
[root@learn ~]# ifconfig eth1 hw ether 00:0c:29:41:FA:1A
route
route -n 查看路由表
route add -net 到某一个网段怎么走
route add -host 到某个主机怎么走
route add default gw 192.168.80.1添加默认路由
route add -net 0.0.0.0 gw 192.168.80.1 添加默认路由
添加默认网关
[root@learn ~]# route add default gw 192.168.80.1
添加到192.168.60.0/24网段的路由
[root@learn ~]# route add -net 192.168.60.0/24 gw 192.168.80.2
添加到192.168.50.0 255.255.255.0 网段的路由
[root@learn ~]# route add -net 192.168.50.0 netmask 255.255.255.0 gw 192.168.80.2
添加到主机192.168.50.3的路由
[root@learn ~]# route add -host 192.168.50.3 gw 192.168.80.2
查看路由表
[root@learn ~]# route -n
删除到某主机或某网段的路由
[root@learn ~]# route del -net 192.168.50.0/24
[root@learn ~]# route del -net 0.0.0.0
[root@learn ~]# route del -host 192.168.50.3
永久更改IP地址设置
修改网卡配置文件(永久性更改),非立即生效需要重启系统或重启服务
永久更改linux服务器名称
[root@learn ~]# vim /etc/sysconfig/network
网络服务是否禁用
网关
[root@learn ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=learn #服务器名称
GATEWAY=192.168.80.1
更改ip地址
[root@learn ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 #网卡名
HWADDR=00:0C:29:41:FA:11 #MAC地址
TYPE=Ethernet #以太网
UUID=a316e175-f00b-4c7c-8ad2-7494256deba5
ONBOOT=yes #开机是否启用该网卡
NM_CONTROLLED=yes #守护网络(改变配置文件立即生效,建议关掉)
BOOTPROTO=dhcp #none static dhcp bootp
IPV6INIT=no
USERCTL=no
重启网卡服务
[root@111 ~]# service network restart
路由配置文件
默认是没有的,需要单独创建,和网卡绑定
[root@learn ~]# vim /etc/sysconfig/network-scripts/route-eth0
192.168.20.0/24 via 192.168.80.2
192.168.40.0/24 via 192.168.80.3
192.168.40.5/32 via 192.168.80.4
网段 到 路由
[root@learn ~]# service network restart
更改DNS
如果网卡设置中没有指定DNS时 /etc/resolv.conf 文件的设置生效,如果是dhcp或网卡中有设置应用网卡设置。
[root@learn ~]# vim /etc/resolv.conf
[root@learn ~]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search lan
nameserver 192.168.99.1 #DNS
nameserver 61.139.2.69
更改hosts
计算机优先解析本地dns文件 /etc/hosts
应用顺序 etc/hosts --> /etc/resolv.conf -->
[root@learn ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
222.213.2.100 www.baidu.com #例
查看网络服务状态
service NetworkManager status
service network status
网卡服务级别
chkconfig --list #应用启动级别列表
chkconfig --lecel 2345 NetworkManager off #关闭网络应用2345启动级别
网络相关的命令
ping
icmp协议
-c 指定发送几个数据包
-i 指定等待几秒钟发一个ping包
-w 指定ping命令执行多少秒
-t 指定数据包 ttl
-s 指定发送数据包大小
arping
arping -I eth0 192.168.0.22 #检查子网内有无IP冲突
traceroute
[root@learn ~]# traceroute 61.139.2.69
traceroute to 61.139.2.69 (61.139.2.69), 30 hops max, 60 byte packets
1 xyun.co (192.168.99.1) 2.728 ms 7.534 ms 6.247 ms
2 100.64.0.1 (100.64.0.1) 22.862 ms 22.681 ms 22.574 ms
3 61.157.151.205 (61.157.151.205) 8.269 ms 8.234 ms 8.173 ms
4 171.208.198.153 (171.208.198.153) 9.648 ms 8.771 ms 8.700 ms
5 * * 171.208.199.74 (171.208.199.74) 9.300 ms
6 61.139.113.54 (61.139.113.54) 8.313 ms 10.821 ms 10.843 ms
7 ns.sc.cninfo.net (61.139.2.69) 12.120 ms 10.483 ms 10.385 ms
[root@learn ~]# traceroute 192.168.99.1
traceroute to 192.168.99.1 (192.168.99.1), 30 hops max, 60 byte packets
1 xyun.co (192.168.99.1) 1.381 ms 3.234 ms 3.196 ms
nslookup
[root@learn ~]# nslookup baidu.com
Server: 192.168.99.1
Address: 192.168.99.1#53
Non-authoritative answer:
Name: baidu.com
Address: 111.13.101.208
Name: baidu.com
Address: 123.125.114.144
Name: baidu.com
Address: 180.149.132.47
Name: baidu.com
Address: 220.181.57.217
[root@learn ~]# nslookup
> qq.com
Server: 192.168.99.1
Address: 192.168.99.1#53
> set type=mx #解析邮件服务器
> qq.com
Server: 192.168.99.1
Address: 192.168.99.1#53
Non-authoritative answer:
qq.com mail exchanger = 30 mx1.qq.com.
qq.com mail exchanger = 10 mx3.qq.com.
qq.com mail exchanger = 20 mx2.qq.com.
Authoritative answers can be found from:
mx2.qq.com internet address = 59.37.97.124
mx2.qq.com internet address = 14.17.41.170
mx2.qq.com has AAAA address 240e:ff:f040:28::f
mx1.qq.com internet address = 183.57.48.35
mx1.qq.com has AAAA address 240e:ff:f040:28::f
mx3.qq.com internet address = 183.57.48.35
mx3.qq.com has AAAA address 240e:ff:f040:28::f
> set type=a #解析网站
Server: 192.168.99.1
Address: 192.168.99.1#53
** server can't find set: NXDOMAIN
netstat
查看Linux路由表
[root@learn ~]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.99.0 * 255.255.255.0 U 0 0 0 eth0
192.168.10.0 * 255.255.255.0 U 0 0 0 eth1
link-local * 255.255.0.0 U 0 0 0 eth1
default xyun.co 0.0.0.0 UG 0 0 0 eth0
查看侦听的端口
netstat -l
netstat -n
[root@learn ~]# netstat -ln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
查看什么服务侦听的什么端口
netstat -lnp|grep "tcp"
查看建立的链接
netstat -n |grep "tcp"
查看所有协议统计信息
netstat -s
查看tcp协议统计信息
netstat -s |grep -A 10 "Tcp:" #-A选项找到关键字及以下10行
高级网络设置
给网卡绑定多个ip地址
临时给网卡增加ip地址
[root@learn ~]# ifconfig eth0:0 192.168.99.166/24
[root@learn ~]# ifdown eth0:0 down #虚拟网卡down后启不来
通过配置文件给一个网络增加多个ip地址
[root@learn ~]# cd /etc/sysconfig/network-scripts/
[root@learn network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
[root@learn network-scripts]# vim ifcfg-eth0:0 #编辑配置文件
DEVICE=eth0:0 #更改
HWADDR=00:0C:29:41:FA:11 #不需要更改
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none #更改
IPADDR=192.168.99.166 #更改
PREFIX=24 #掩码位数
将多个网卡绑定成一个网卡
负载均衡
网卡冗余(备用网卡)
模式0 负载均衡
模式1 网卡冗余 主备模式
模式3 多块网卡同时发送相同的数据
1.创建绑定网卡配置文件
[root@WebServer ~]# vim /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
IPADDR=192.168.80.240
PREFIX=24
GATEWAY=192.168.80.1
ONBOOT=yes
USERCTL=no
BOOTPROTO=none
BONDING_OPTS="mode=0 miimon=50"
2.修改eth0和eth1网卡配置文件
[root@WebServer ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no
[root@WebServer ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no
3.添加驱动支持bond0
[root@WebServer ~]# vi /etc/modprobe.d/bonding.conf
alias bond0 bonding
4.重启network服务
[root@WebServer ~]# service network restart
arp绑定mac地址和ip地址
arp 绑定
[root@WebS1 ~]# arp -s 192.168.80.111 00:0c:29:7b:54:d1
[root@WebS1 ~]# arp -d 192.168.80.111
[root@WebS1 ~]# cat /proc/net/arp
IP address HW type Flags HW address Mask Device
192.168.80.1 0x1 0x2 00:50:56:e1:89:93 * eth0
192.168.80.199 0x1 0x2 00:50:56:c0:00:08 * eth0
192.168.80.111 0x1 0x6 00:0c:29:7b:54:d1 *
将静态映射添加到配置文件
[root@WebS1 ~]# vi /etc/ethers
# see man ethers for syntax
192.168.80.111 00:0c:29:7b:54:d2