Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux管理 >> Linux 網路

Linux 網絡配置入門:從接口命名到路由與解析

一、網絡接口

命名規則:Linux 內核按接口類型分配前綴,再從 0 開始編號。所有以太網接口以 eth 開頭,eth0、eth1、eth2 分別指第一、二、三張網卡。

查看配置:

ifconfig         只顯示活躍網卡
ifconfig -a      顯示所有接口
ip link          等價的另一種方式

二、驅動程序選擇

RedHat 把網卡驅動編譯為內核模塊,引導時依據 /etc/modprobe.conf 裝載,並在其中把接口名映射到模塊:

[root@Think ~]# grep 'alias eth' /etc/modprobe.conf
alias eth0 vmxnet
alias eth1 e1000
alias eth2 e1000

只有一張網卡時這樣就夠了。若多張網卡共用同一模塊(如 eth1 與 eth2 都用 e1000),必須用 HWADDR 加以區分,該變量寫在接口配置文件中:

[root@Think ~]# grep 'HWADDR' /etc/sysconfig/network-scripts/ifcfg-eth*
/etc/sysconfig/network-scripts/ifcfg-eth1:HWADDR=00:C2:89:fQ:b7:55
/etc/sysconfig/network-scripts/ifcfg-eth2:HWADDR=00:TC:Q6:CC:5F:F5

三、帶寬與雙工

缺省情況下接口為 autonegotiate,由網卡與交換機協商最佳參數,但有時並非最優,可手工指定。

臨時修改:

ifdown eth0
ethtool -s eth0 autoneg off speed 1000 duplex full
ifup eth0

永久修改:在 /etc/sysconfig/network-scripts/ifcfg-ethX 中加入:

ETHTOOL_OPTS="autoneg off speed 1000 duplex full"

四、動態 IPv4 配置

[root@Think ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

五、靜態 IPv4 配置

[root@Think ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
IPADDR=192.168.1.112
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes

兩種方式改完都需 ifdown ethX 再 ifup ethX 激活。

六、IP 别名

把多個 IP 綁到一張網卡上,如 eth0:1、eth0:2、eth0:3,每個别名會生成獨立的接口配置文件 ifcfg-eth0:1。

七、路由表

[root@Think ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref Use Iface
192.168.1.0     *               255.255.255.0   U     0      0   0   eth0
192.168.122.0   *               255.255.255.0   U     0      0   0   virbr0
169.254.0.0     *               255.255.0.0     U     0      0   0   eth0
default         192.168.1.1     0.0.0.0         UG    0      0   0   eth0

[root@Think ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags MSS Window irtt Iface
192.168.1.0     *               255.255.255.0   U     0   0      0    eth0
default         192.168.1.1     0.0.0.0         UG    0   0      0    eth0

[root@Think ~]# ip route
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.112
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
169.254.0.0/16 dev eth0 scope link
default via 192.168.1.1 dev eth0

八、默認網關

當路由表中沒有更具體的匹配時,由默認網關決定 IP 包的去向,通常用於局域網只有單一出口的場景。動態獲取靠 DHCP;靜態配置用 GATEWAY 變量,可寫在通用文件 /etc/sysconfig/network 或接口文件 ifcfg-ethX 中,兩者同時存在時接口配置優先級更高。

九、驗證連通性

ping 用於測量丟包與延遲,默認每秒發送一個 64 字節的 ICMP 包,-c 可指定次數。traceroute 顯示到達目的地的網絡路徑,流量經過多個路由器時,用它定位故障點很方便。

十、主機名

hostname              查看
hostname Think        臨時定義

永久定義在 /etc/sysconfig/network 中設置變量 HOSTNAME。

十一、本地解析

配置文件 /etc/hosts,優先級高於 DNS。其中必須包含 localhost 條目,最好也包含已在 network 文件中定義的主機名。不要改動 localhost 行本身,可在行尾追加別名:

[root@Think ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1       localhost.localdomain localhost
192.168.1.112   localhost.localdomain localhost
117.79.93.222   www.csdn.net www

十二、遠程解析

配置文件為 /etc/resolv.conf,用 nameserver 指定 DNS 服務器地址。

Copyright © Linux教程網 All Rights Reserved