是这样的,我有一台远程的Linux服务器(Debian系统),上面有两块网卡,同样的IP。
我现在需要选择第二块网卡,因为这块网卡连上了CMCC的专线,可以进行一些CMCC的业务操作。但是默认的使用的是第一块网卡。
不知道如何实现选择不同的网卡进行网络访问。
谢谢
谢谢各位的回答,总结了各位的答案,我成功的解决了问题。
一开始两个网卡同个IP是服务器那边配置出了问题,没有配置第二个网卡的内网IP,现在配置了之后两个网卡分别有两个IP地址。
首先运行nmcli查看两个网卡的网关:
sudo nmcli dev list
其中的IP4的GW就是网关的,可以直接这样查看:
sudo nmcli dev list | grep gw
然后记着那张需要的网卡的gw地址xxx.xxx.xxx.xx,然后使用下面这条命令:
sudo route add -host xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xx
其中第一个是需要制定访问eth1网卡的Host地址,第二个是前面查到的网关的内容。
再次感谢大家的热心帮助。
"
I encountered this when I was doing my graduation project. The needs at that time were similar to yours:
I will share the notes I took at that time.
To add, it is not surprising that the IPs of the two network cards are the same, and there is nothing to consider. It is important to investigate the IP or IP segment of the other server of the CMCC business dedicated line. This method only uses the resolved IP address of the other party to determine which network card the data will go to.
For Python programs, just put
socket.connect
beforesocket.bind(('需要用的本地网卡IP', 0))
.This can be done using policy routing under Linux
1. Edit the routing table and add the CMCC dedicated line entry
2. For example, the network card IP of eth2 is 124.0.0.1, and the network segment of the CMCC dedicated line is 124.127.101.0/24. It can be configured as follows