search

Home  >  Q&A  >  body text

Linux下如何选择不同的网卡进行网络访问?Python或者其他

是这样的,我有一台远程的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地址,第二个是前面查到的网关的内容。

再次感谢大家的热心帮助。
"

ringa_leeringa_lee2767 days ago493

reply all(4)I'll reply

  • 高洛峰

    高洛峰2017-04-17 13:13:05

    I encountered this when I was doing my graduation project. The needs at that time were similar to yours:

    • FC18 system
    • Dual network cards
    • One connected to a standard router: ADSL broadband Internet access, fast access to domestic and foreign websites, but cannot access exclusive resources on campus
    • One connected to the campus network: access the external network and pop up the Web authentication interface, but you can access exclusive resources within the school without authentication and enjoy LAN speed
    • The default behavior of the system at that time seemed to be messed up...

    I will share the notes I took at that time.

    Beihang Campus Network Features

    • The speed is unstable and is obviously affected by the number of users on the campus network.
    • Intranet transmission is fast, and the computers in the campus network are all in the same LAN.
    • Domestic Internet access speed is average.
    • Internet speed abroad is poor.
    • Check online that the network segment of Beihang University is: 202.112.128.0/202.122.143.255, and it is confirmed that the financial department, campus network certification, network center and other school organization websites are within this range.

    Fedora routing configuration

    Because my dormitory computer uses Fedora, I will use Fedora as an example. It's not very clear how to configure it under Windows. First, use dual network cards to connect the campus network port and the router for ADSL broadband at the same time.

    Edit the network properties of the adapter p17p1 connected to the Beihang intranet:

    • IPv4 Settings: Automatic (DHCP)
    • IPv4 Settings-Routing: Address 202.112.128.0 Subnet Mask 255.255.240.0 Gateway 192.168.11.33 Metric
    • [V] Ignore automatically acquired routes
    • [V] Only use this connection for resources on the corresponding network

    For wlan0 connected to the ADSL public network, there is no need to edit the network properties, keep it automatic.

    Effect

    • Be able to access Beihang campus websites such as the Finance Office and Network Center that cannot be accessed through the public network. At this time, the data goes through the campus network.
    • Get stable speeds when accessing any other website. At this time, the data goes through ADSL broadband.

    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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:13:05

    route add -net CMCC的网段 netmask xxx dev 第二块网卡的名字
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:13:05

    For Python programs, just put socket.connect before socket.bind(('需要用的本地网卡IP', 0)).

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:13:05

    This can be done using policy routing under Linux
    1. Edit the routing table and add the CMCC dedicated line entry

    vim /etc/iproute2/rt_tables
    200      cmcc
    

    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

    ETH2=eth2
    IP2=124.0.0.1
    SEGMENT2=124.127.101.0/24
    
    TABLE2=cmcc
    
    ip route add $SEGMENT2 dev $ETH2 src $IP2 table $TABLE2
    ip route add default via $IP2 table $TABLE2
    
    ip rule add from $IP2 table $TABLE2
    

    reply
    0
  • Cancelreply