Linux 網(wǎng)卡配置有兩種方法,一是臨時(shí)配置、二是永久配置。
1. 臨時(shí)配置
臨時(shí)配置網(wǎng)卡(以網(wǎng)卡名為eth0為例)的IP地址和子網(wǎng)掩碼,在重啟后失效。
1.1使用ifconfig命令配置,命令如下:
ifconfig eth0 192.168.33.15 netmask 255.255.255.0
配置完成后,使用ifconfig命令查看是否配置成功,得到:
root@ubuntu:~# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.33.15 netmask 255.255.255.0 broadcast 192.168.33.255
inet6 fe80::fab1:56ff:feb5:8 prefixlen 64 scopeid 0x20<link>
ether f8:b1:56:b5:00:08 txqueuelen 1000 (Ethernet)
RX packets 988079 bytes 161803321 (161.8 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1849659 bytes 2584022279 (2.5 GB)
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 9474363 bytes 997142730 (997.1 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9474363 bytes 997142730 (997.1 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
其中,網(wǎng)卡lo為本地環(huán)回網(wǎng)卡。
使用route命令配置網(wǎng)關(guān),命令如下:
route add default gw 192.168.33.31
若網(wǎng)卡 eth0 未啟用,可使用命令ifconfig eth0 up啟用;用命令ifconfig eth0 down禁用網(wǎng)卡。
2. 永久配置
編輯/etc/network/interfaces文件。命令如下:
vi /etc/network/interfaces
按下按鍵i或o進(jìn)入插入模式,編輯結(jié)束后使用Esc退出編輯,分別按鍵:wq退出。
例如:該interfaces的內(nèi)容可能為:
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
# Generated by debian-installer.
# The loopback interface
auto lo
iface lo inet loopback
或者設(shè)置靜態(tài)IP地址。
auto eth0
iface eth0 int static
address 192.168.33.16
netmask 255.255.255.0
gateway 192.168.33.31
或者默認(rèn)通過DHCP模式獲得網(wǎng)絡(luò)配置。
auto eth0
iface eth0 int dhcp
配置后用命令行/etc/init.d/networking restart重啟網(wǎng)絡(luò)服務(wù)或重啟計(jì)算機(jī)生效。