美文网首页
Linux 网卡配置

Linux 网卡配置

作者: whuangxm | 来源:发表于2020-01-08 23:39 被阅读0次

Linux 网卡配置有两种方法,一是临时配置、二是永久配置。

1. 临时配置

临时配置网卡(以网卡名为eth0为例)的IP地址和子网掩码,在重启后失效。
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

其中,网卡lo为本地环回网卡。

使用route命令配置网关,命令如下:

route add default gw 192.168.33.31

若网卡 eth0 未启用,可使用命令ifconfig eth0 up启用;用命令ifconfig eth0 down禁用网卡。

2. 永久配置

编辑/etc/network/interfaces文件。命令如下:

vi /etc/network/interfaces

按下按键io进入插入模式,编辑结束后使用Esc退出编辑,分别按键:wq退出。

例如:该interfaces的内容可能为:

# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
# Generated by debian-installer.

# The loopback interface
auto lo
iface lo inet loopback

或者设置静态IP地址。

auto eth0
iface eth0 int static
address 192.168.33.16
netmask 255.255.255.0
gateway 192.168.33.31

或者默认通过DHCP模式获得网络配置。

auto eth0
iface eth0 int dhcp

配置后用命令行/etc/init.d/networking restart重启网络服务或重启计算机生效。

相关文章

网友评论

      本文标题:Linux 网卡配置

      本文链接:https://www.haomeiwen.com/subject/stseactx.html