Linux 基础 - 网络管理
Linux 网络管理
网络接口配置
使用 ifconfig 检查和配置网卡
如果没有 ifconfig 命令,则需要安装 net-tools,也可能是 network-tools,不同发行版的名不同
[root@localhost home]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.213.203 netmask 255.255.255.240 broadcast 192.168.213.207
inet6 fe80::3aa2:8560:48d2:fb80 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:9f:3f:03 txqueuelen 1000 (Ethernet)
RX packets 2386 bytes 363424 (354.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1268 bytes 173267 (169.2 KiB)
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 36 bytes 2944 (2.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 36 bytes 2944 (2.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0 代表第一块网卡,lo 表示主机的环回地址,这个地址是用于本地通讯的
ifconfig 可以有参数
[root@localhost home]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.213.203 netmask 255.255.255.240 broadcast 192.168.213.207
inet6 fe80::3aa2:8560:48d2:fb80 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:9f:3f:03 txqueuelen 1000 (Ethernet)
RX packets 2462 bytes 390399 (381.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1288 bytes 176615 (172.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig 可以设置 IP 地址
[root@localhost home]# ifconfig eth0 192.168.159.130 netmask 255.255.255.0
也可以简写
[root@localhost home]# ifconfig eth0 192.168.159.130/24
/24 代表子网掩码前 24 位是 1,即 255.255.255.0
系统能通过 IP 地址和子网掩码自动算出网关,也可以指定网关
[root@localhost home]# ifconfig eth0 192.168.159.130 broadcast 192.168.159.255 netmask 255.255.255.0
关闭网卡
[root@localhost home]# ifconfig eth0 down
[root@localhost home]# ifdown eth0
开启网卡
[root@localhost home]# ifconfig eth0
[root@localhost home]# ifup eth0
将 IP 配置信息写入配置文件
通过 ifconfig 配置的 IP 属于一种动态配置,一旦系统重启,将会失效
而网络配置文件在 /etc/sysconfig/network-scripts 中
[root@localhost network-scripts]# ls
ifcfg-eth0 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team init.ipv6-global
ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort network-functions
ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel network-functions-ipv6
ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless
ifcfg-eth0 就是 eth0 的配置文件
[root@localhost network-scripts]# cat 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="03e15ea4-de93-4c40-b360-224725c25b6f"
DEVICE="eth0"
ONBOOT="yes"
- TYPE 网络类型
- PROXY_METHOD
- BROWSER_ONLY
- BOOTPROTO [none|static|bootp|dhcp]
- IPADDR IP地址
- PREFIX 子网掩码长度
- NETMASK 子网掩码
- DNS1 DNS地址可设置多个不大于3
- DNS2 DNS地址可设置多个不大于3
- DNS3 DNS地址可设置多个不大于3
- GATEWAY 默认网关地址
- DEFROUTE 是否启用默认路由
- IPV4_FAILURE_FATAL 如果启用了IPv4和IPv6配置,其中有一个激活失败则认为是整体故障
- IPV6INIT 是否启用IPV6设置
- IPV6_AUTOCONF 是否使用IPV6地址的自动配置
- IPV6_DEFROUTE IPV6是否启用默认路由
- IPV6_FAILURE_FATAL 如果启用了IPv4和IPv6配置,其中有一个激活失败则认为是整体故障
- IPV6_ADDR_GEN_MODE
- NAME
- UUID 设备唯一标识UUID值
- DEVICE 网卡名称
- ONBOOT 是否开机
想要使设置立即生效可以关闭再开启网卡,或者重启网络服务
[root@localhost ~]# service network restart
Restarting network (via systemctl): [ 确定 ]
路由和网关设置
网关设置:route
添加默认网关
[root@localhost ~]# route add default gw 192.168.159.2
删除默认网关
[root@localhost ~]# route del default gw 192.168.159.2
# 简写
[root@localhost ~]# route del default
查看当前路由表
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.213.193 0.0.0.0 UG 100 0 0 eth0
192.168.213.192 0.0.0.0 255.255.255.240 U 100 0 0 eth0
DNS 客户端配置
host 文件:/etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
DNS 客户端配置文件:/etc/resolv.conf
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search mshome.net
nameserver 192.168.213.193
网络测试工具
ping
测试另一台主机是否可达
[root@localhost ~]# ping 220.181.38.150
PING 220.181.38.150 (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150: icmp_seq=1 ttl=50 time=17.3 ms
64 bytes from 220.181.38.150: icmp_seq=2 ttl=50 time=19.9 ms
64 bytes from 220.181.38.150: icmp_seq=3 ttl=50 time=18.4 ms
64 bytes from 220.181.38.150: icmp_seq=4 ttl=50 time=18.5 ms
^C
--- 220.181.38.150 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 17.353/18.580/19.991/0.943 ms
-c
ping 的次数-i
ping 包的发送间隔-w
指定超时时间
host
查询 DNS 记录
如果没有命令则需要安装 yum install bind-utils
[root@localhost ~]# host www.google.com
www.google.com has address 69.63.176.15
www.google.com has IPv6 address 2001::45ab:ed10
;; connection timed out; no servers could be reached
指定 DNS 服务器
[root@localhost ~]# host www.google.com 8.8.8.8
Using domain server:
Name: 8.8.8.8
Address: 8.8.8.8#53
Aliases:
www.google.com has address 203.111.254.117
www.google.com has IPv6 address 2001::42dc:9543
www.google.com has address 31.13.84.2