Linux网络管理入门

2023-06-12,,

根据自己的需要来设置Linux的一些属性

网络状态查看

在终端输入ifconfig可以查看网络状态

# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.4.16 netmask 255.255.252.0 broadcast 10.0.7.255
inet6 fe80::5054:ff:fee5:a88b prefixlen 64 scopeid 0x20<link>
ether 52:54:00:e5:a8:8b txqueuelen 1000 (Ethernet)
RX packets 24244336 bytes 4246851961 (3.9 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 24130040 bytes 4921464849 (4.5 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 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 1000 (Local Loopback)
RX packets 7038 bytes 896518 (875.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7038 bytes 896518 (875.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0表示第一块网卡,inet表示IPv4地址,netmask表示子网掩码,ether表示网卡的MAC地址,下面的RX和TX和收发数据包的大小有关

lo表示本地回环,IP地址永远是127.0.0.1,常常用于本地测试

网络配置

通过以下命令可以修改IP地址:

ifconfig <接口> <IP地址> [netmask 子网掩码]
ifup <接口>
ifdown <接口>

例如使用ifconfig eth0 10.10.10.10就可以直接将IP修改为10.10.10.10

输入ifup eth0启动网卡,ifdown eth0关闭网卡

输入route -n 查看网关,这里输出的是路由表

# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.4.1 0.0.0.0 UG 100 0 0 eth0
10.0.4.0 0.0.0.0 255.255.252.0 U 100 0 0 eth0

如下是对这些字段的解释:

输出项 说明
Destination 目标网段或者主机
Gateway 网关地址,”*” 表示目标是本主机所属的网络,不需要路由
Genmask 网络掩码
Flags 标记。一些可能的标记如下:
U — 路由是活动的
H — 目标是一个主机
G — 路由指向网关
R — 恢复动态路由产生的表项
D — 由路由的后台程序动态地安装
M — 由路由的后台程序修改
! — 拒绝路由
Metric 路由距离,到达指定网络所需的中转数(linux 内核中没有使用)
Ref 路由项引用次数(linux 内核中没有使用)
Use 此路由项被路由软件查找的次数
Iface 该路由表项对应的输出接口

路由表是保存在 RAM 中的数据文件,其中存储了与直连网络以及远程网络相关的信息。路由表包含网络与下一跳的关联信息。这些关联告知路由器:要以最佳方式到达某一目的地,可以将数据包发送到特定路由器(即在到达最终目的地的途中的“下一跳”)。下一跳也可以关联到通向最终目的地的外发或送出接口。

使用route del可删除网关地址,使用route add可以添加网关地址

# route add -host 10.0.0.1 gw 10.0.4.17
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.4.1 0.0.0.0 UG 100 0 0 eth0
10.0.0.1 10.0.4.17 255.255.255.255 UGH 0 0 0 eth0
10.0.4.0 0.0.0.0 255.255.252.0 U 100 0 0 eth0

如上就直接向路由表添加了一行

# route del -host 10.0.0.1 gw 10.0.4.17

如上是删除

也可以向网段添加一条路由

# route add -net 192.168.0.0 netmask 255.255.255.0 gw 10.0.4.14
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.4.1 0.0.0.0 UG 100 0 0 eth0
10.0.4.0 0.0.0.0 255.255.252.0 U 100 0 0 eth0
192.168.0.0 10.0.4.14 255.255.255.0 UG 0 0 0 eth0

如上显示添加成功

网络故障排除

可以借助一些命令进行网络故障的排除

ping

检测与对方主机是否能连通

# ping www.baidu.com
PING www.a.shifen.com (110.242.68.4) 56(84) bytes of data.
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=1 ttl=51 time=25.5 ms
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=2 ttl=51 time=25.5 ms
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=3 ttl=51 time=25.5 ms
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=4 ttl=51 time=25.5 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 25.509/25.512/25.517/0.003 ms

ping命令后接上主机名,发起ping请求,完成后下方会显示发送数据包的数量和丢包率以及延时

traceroute

该命令能输出网络数据包的路由路径

# traceroute -w 1 www.baidu.com
traceroute to www.baidu.com (112.80.248.76), 30 hops max, 60 byte packets
1 9.245.218.129 (9.245.218.129) 10.315 ms 10.525 ms 10.688 ms
2 * * *
3 10.196.39.233 (10.196.39.233) 0.610 ms 0.807 ms *
4 220.196.197.161 (220.196.197.161) 1.966 ms 10.196.95.33 (10.196.95.33) 1.683 ms 220.196.197.161 (220.196.197.161) 2.074 ms
5 * * *
6 220.196.196.13 (220.196.196.13) 3.170 ms 220.196.197.161 (220.196.197.161) 1.958 ms 220.196.196.13 (220.196.196.13) 3.148 ms
7 153.3.228.74 (153.3.228.74) 12.334 ms * 153.3.228.150 (153.3.228.150) 10.950 ms
8 * 219.158.104.218 (219.158.104.218) 5.527 ms 219.158.104.222 (219.158.104.222) 9.296 ms
9 182.61.216.0 (182.61.216.0) 8.884 ms 153.3.228.150 (153.3.228.150) 8.259 ms 153.3.228.142 (153.3.228.142) 8.873 ms
10 153.3.226.18 (153.3.226.18) 11.230 ms 153.37.96.134 (153.37.96.134) 12.598 ms *
11 * 182.61.216.0 (182.61.216.0) 10.106 ms *
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * *
30 * * *

记录按序列号从1开始,每个纪录就是一跳 ,每跳表示一个网关,我们看到每行有三个时间,单位是 ms。有时traceroute 一台主机时,会看到有一些行是以星号表示的。出现这样的情况,可能是防火墙封掉了ICMP的返回信息,所以我们得不到什么相关的数据包返回数据。

nslookup

该命令能够将域名解析为IP地址

# nslookup www.baidu.com
Server: 183.60.83.19
Address: 183.60.83.19#53 Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 112.80.248.75
Name: www.a.shifen.com
Address: 112.80.248.76

使用该命令检测域名是否能解析为IP

telnet

# telnet www.baidu.com 80
Trying 112.80.248.75...
Connected to www.baidu.com.
Escape character is '^]'.
^]
telnet> quit
Connection closed.

尝试连接目标主机的某个端口

tcpdump

流量抓包工具

# tcpdump -i any -n port 80
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked v1), capture size 262144 bytes
....

使用-i可以制定网卡 -n port可以指定端口号

# tcpdump -i any -n host 10.0.0.1
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked v1), capture size 262144 bytes

使用-n host可以指定主机IP

# tcpdump -i any -n host 10.0.0.1 and port 80
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked v1), capture size 262144 bytes

使用and连接,将两者都指定

netstat

输出网络连接状态

# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:36585 0.0.0.0:* LISTEN 401826/vim
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3151/sshd
tcp6 0 0 :::80 :::* LISTEN 440587/httpd

如上命令能够查看正在监听的服务,并且查看对应的进程,如sshd,该进程提供了远程控制服务,监听在22端口

网络配置文件

输入以下命令进入网络接口配置文件所在的目录

# cd /etc/sysconfig/network-scripts/

查看网卡接口对应的配置文件

# ls ifcfg-*
ifcfg-eth0 ifcfg-lo

查看文件内容

# cat ifcfg-eth0
BOOTPROTO=dhcp
DEVICE=eth0
DHCPV6C=yes
DHCPV6C_OPTIONS=-S
HWADDR=52:54:00:e5:a8:8b
IPV6INIT=yes
IPV6_AUTOCONF=yes
ONBOOT=yes
PERSISTENT_DHCLIENT=yes
TYPE=Ethernet
USERCTL=no

BOOTPROTO为dhcp表明IP地址是动态分配的,配置为static则为静态分配

ONBOOT为yes,表明开机时网卡会被启用,如果为no则要在开机后使用ifup启动网卡

Linux网络管理入门的相关教程结束。

《Linux网络管理入门.doc》

下载本文的Word格式文档,以方便收藏与打印。