Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ubuntu系统双网卡的配置 #11

Open
ttop5 opened this issue Oct 30, 2016 · 2 comments
Open

Ubuntu系统双网卡的配置 #11

ttop5 opened this issue Oct 30, 2016 · 2 comments

Comments

@ttop5
Copy link
Owner

ttop5 commented Oct 30, 2016

环境:

  • ubuntu 14.04 server
  • 拥有四块网卡的服务器(在下面的操作中只需要用到两块)
  • 外网IP: 210.44.185.75 外网网关:210.44.185.10
  • 内网IP: 10.6.0.248 内网网关:10.6.0.254

需求:

  • 服务器能通过网卡1来连接外网的某台主机;
  • 同时要求局域网内网段为 10.6.1.* , 10.6.4.* , 10.6.15.* 的三个网段要能通过网卡2连接服务器。

明确:

一台双网卡电脑拥有两个网关是不可能的,因为默认网关(default gateway)只能是一个!

解决方案:

1.配置网络

我们将其中一块网卡(比如外网网卡 eth0)设置成默认网关,另外一块网卡eth1设置网关。

sudo vim /etc/network/interfaces

配置如下:

auto eth0
iface eth0 inet static
address 210.44.185.75
netmask 255.255.255.0
gateway 210.44.185.10

auto eth1
iface eth1 inet static
address 10.6.0.248
netmask 255.255.255.0

2. 重启网络

sudo /etc/init.d/networking restart

3.设置路由

这时我们的第一条需求已经实现了,但是由于没有设置内网网关,第二条需求还实现不了,我们需要分别给这三个网段设置路由。

注意:一块网卡只能设置一个网关,多个网关会发生冲突而无法成功配置。

操作如下:

sudo route add -net 10.6.0.0/24 gw 10.6.0.254 dev eth1
sudo route add -net 10.6.1.0/24 dev eth1
sudo route add -net 10.6.4.0/24 dev eth1
sudo route add -net 10.6.15.0/24 dev eth1

最后使用 ip route 查看路由设置情况:

ttop5@ubuntu:~$ ip route
default via 210.44.185.10 dev eth0  metric 100
10.6.0.0/24 dev eth1  proto kernel  scope link  src 10.6.0.248
10.6.1.0/24 dev eth1  scope link
10.6.4.0/24 dev eth1  scope link
10.6.15.0/24 dev eth1  scope link
210.44.185.0/24 dev eth0  proto kernel  scope link  src 210.44.185.75

如有多余的配置,可使用下面的命令进行删除,祝你好运!:grinning:

sudo route del -net *.*.*.*/* dev eth*
@ttop5 ttop5 added this to the 旧wp博客迁移 milestone Oct 30, 2016
@ttop5 ttop5 self-assigned this Oct 30, 2016
@ttop5 ttop5 changed the title UBUNTU系统双网卡的配置 Ubuntu系统双网卡的配置 Oct 30, 2016
@eden-wang
Copy link

感谢分享

@ttop5
Copy link
Owner Author

ttop5 commented Dec 29, 2017

@eden-wang 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants