After connecting to PPPoE, Linux cannot retain the original physical network card IP address as Windows does

Posted by SamDOD on Fri, 17 Dec 2021 19:59:35 +0100

1. Come straight to the point and explain the phenomenon

(1) It is known that there is a PC of Linux system and a PC of Windows system in the network environment. Before connecting PPPoE, the IP addresses of relevant physical network cards are as follows:


(2) If a PPPoE server has been set up in the current network environment, Linux and Windows can be connected to PPPoE respectively, as shown in the following figure:


(3) It can be seen that after Linux system and Windows system are connected to PPPoE respectively, Linux does not retain the address of the original physical network card, but Windows retains it. It should be noted that the situation of not retaining the IP address after successfully connecting to PPPoE exists on all Linux distributions based on NetworkManager service, which is not unique to certain Linux distributions.

2. The limitation and impact caused by not retaining the physical network card after Linux is connected to PPPoE


As shown in the figure above, after Linux and Windows systems are connected to PPPoE respectively, Because the IP address of the original physical network card is cleared, Linux cannot connect with the original LAN in the same network segment (local area network) for network communication. Because the IP address of the original physical network card is reserved, Windows can still maintain normal network communication with the LAN of the same network segment. If the original LAN has cross network segments, because the default route is changed after connecting PPPoE, it needs to add a route manually in the PC to communicate.

3. How to keep the IP address to maintain communication with the original LAN

After testing, the nmcli or nmtui in the NetworkManager service of Linux cannot retain the original IP address. From the command line, the original physical network card and PPP virtual network card can only keep one network card active with * sign at the same time, as shown in the following figure:


Although the service cannot permanently retain the IP address of the physical network card, we can still reconfigure the original IP address by "ifconfig < physical network card name > < IP address >" to achieve the retention effect. The original IP address is 192.168 Take 100.70 as an example, as shown in the figure below:

The final effect is as follows:

For users familiar with Linux command line, this method can solve the problem of original LAN network communication interruption caused by non reservation of IP address.

4. Improve PPPoE experience on deepin or UOS through Shell scripts

On operating systems such as deepin and UOS with graphics, which are one of the Linux distributions, if you restart while maintaining the PPPoE connection, the system will automatically connect to the PPPoE after the restart, resulting in the original physical network card having no time to obtain the IP address, so that users do not know which address to configure to the physical network card, Moreover, it is too cumbersome for users to configure the IP address through the command line every time. At this time, you can consider solving this problem through the following Shell script: (for other Linux distributions, such as Ubuntu and Ukylin, you can actually modify the template according to this script)

#!/bin/bash
username=`cat /etc/passwd | grep 1000:1000 | awk -F: '{print $1}'`
defaultInterfaceName=`route -n | sed -n '3p' | awk '{print $8}'`
defaultInterfaceIPaddr=`ifconfig ${defaultInterfaceName} | grep broadcast | awk '{print $2}'`

#In / etc / udev / rule D Directory - 82 PPPoE saveipaddr Rules to implement the script ifconfigipaddr when the PPP virtual network card is started sh
function createPPPoEUdevRule(){
	touch /etc/udev/rules.d/82-pppoe-saveIPaddr.rules
	touch /etc/ppp/ifconfigIPaddr.sh
	echo "SUBSYSTEM==\"net\",ACTION==\"add\",KERNEL==\"ppp*\" RUN+=\"/etc/ppp/ifconfigIPaddr.sh\"" > /home/${username}/Desktop/82-pppoe-saveIPaddr.rules
	mv /home/${username}/Desktop/82-pppoe-saveIPaddr.rules /etc/udev/rules.d/
	chmod 644 /etc/udev/rules.d/82-pppoe-saveIPaddr.rules
}

#Write udev rule 82 PPPoE saveipaddr The shell script ifconfigipaddr. Needs to be executed in rules SH and the text file defaultrouteinterfaceandipaddr that stores the default network card name and IP address txt
function createIfconfigIPaddr_sh(){
	echo $defaultInterfaceName > /home/${username}/.defaultRouteInterfaceAndIPaddr.txt
	echo $defaultInterfaceIPaddr >> /home/${username}/.defaultRouteInterfaceAndIPaddr.txt
	chown ${username} /home/${username}/.defaultRouteInterfaceAndIPaddr.txt
	chgrp ${username} /home/${username}/.defaultRouteInterfaceAndIPaddr.txt
	cat << EOF > /etc/ppp/ifconfigIPaddr.sh
#!/bin/bash
username=\`cat /etc/passwd | grep 1000:1000 | awk -F: '{print \$1}'\`
ifconfig \`sed -n '1p' /home/\${username}/.defaultRouteInterfaceAndIPaddr.txt\` \`sed -n '2p' /home/\${username}/.defaultRouteInterfaceAndIPaddr.txt\`
EOF
	chmod a+x /etc/ppp/ifconfigIPaddr.sh
}

#Measures for Linux to lose the IP address of the physical network card again when connecting to PPPoE by default after restarting the system
function afterRestartSystem(){
	cat << EOF > /home/${username}/.findIPaddrAndInterfaceName.sh
#!/bin/bash
username=\`cat /etc/passwd | grep 1000:1000 | awk -F: '{print \$1}'\`
defaultInterfaceName=\`route -n | sed -n '3p' | awk '{print \$8}'\`
defaultInterfaceIPaddr=\`ifconfig \${defaultInterfaceName} | grep broadcast | awk '{print \$2}'\`
echo \$defaultInterfaceName > /home/\${username}/.defaultRouteInterfaceAndIPaddr.txt
echo \$defaultInterfaceIPaddr >> /home/\${username}/.defaultRouteInterfaceAndIPaddr.txt
EOF
	chmod a+x /home/${username}/.findIPaddrAndInterfaceName.sh
	echo "nmcli connection down \`nmcli connection show --active | grep pppoe | awk '{print \$(NF-2)}'\`" >> /etc/profile
	echo "sleep 4" >> /etc/profile
	echo "bash ~/.findIPaddrAndInterfaceName.sh" >> /etc/profile
}

#function call
createPPPoEUdevRule
createIfconfigIPaddr_sh
afterRestartSystem

[note] this script can optimize the PPPoE operation experience on deepin and UOS systems, but it still can't cope with some scenarios - for example, the IP address is changed during use without restarting the computer (for example, the IP address is changed due to the probability of DHCP lease expiration).

Topics: Linux Windows TCP/IP