How to change default interface for internet? - linux

I have a PC with Ubuntu server 18.04 installed on it and I'm trying to use this PC as a server. There are 2 interfaces involved here:
To provide It's internet, I am using an android smartphone that has access to internet via it's Data and it will be sharing internet with my PC (server) via USB Tethering. This will create an interface called 'enp0s29f7u8'. This interface will get an IP automatically (DHCP?), mostly '192.168.42.249'.
There is another interface called 'enp2s0' which is a Huawei internet modem and it's connected to my PC with a LAN cable. This 'enp2s0' will serve as an Access-Point so I can SSH to my PC While I'm close. I installed 'ifupdown' on server so I can assign an Static IP to my Access-Point, namely '192.168.1.10'.
$ cat /etc/network/interfaces
auto enp2s0
iface enp2s0 inet static
address 192.168.1.10
netmask 255.255.255.0
network 192.268.0.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1
Here is the problem: I cannot access internet with this setups. It's like Ubuntu is trying to connect to Internet via 'enp2s0', which is only an AP with no access to internet.
So i tried
sudo ifconfig enp2s0 down
and there it is, i have internet. Also, when I do
sudo ifconfig enp2s0 up
after that, i still have access to internet.
How can I config my PC so that it will always use 'enp0s29f7u8' to access internet and use 'enp2s0' only as an AP?
p.s. 1: I really don't understand network stuff. I tried changing default gateway (I don't know why) but it didn't helped(at least the way i did).
p.s. 2: I'm not a native English speaker. Hope that I could talk my mind.

To modify the routing table you have to use the command route.
If you run the command as super user it should show how the traffic is routed from your host. You have to change the default to enp0s29f7u8. In order to change it you can simply remove the default
# route del default
and add again
# route add default dev enp0s29f7u8
You can route only some address to the other interface. Have a look at the man for more option of the route command

Related

Multicast Routing over Wlan and Eth0 interface

I want to make my Raspberry PI model 3 as a router that is as follows -
Computer will be connected to RPI via Ethernet (eth0) interface
Phone will be connected to RPI over Wifi (wlan0) interface.
I was able to setup a interfaces with the help of the following links -
https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/
But I am not able to do the multicast routing from wlan0 to eth0 (or) vice versa Can you please help me with it. I need to route bonjour packets over the interfaces.
Note:
both the interfaces wlan and eth are supporting MULTICAST ( verified with ifconfig command ) I was able to get UP BROADCAST RUNNING MULTICAST for both the interfaces.
I have tried using the following commands for multicasting -
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
route add -net 224.0.0.0 netmask 240.0.0.0 dev wlan0
You can use avahi-daemon to reflect mDNS queries between interfaces. Some basic configuration like this in avahi-daemon.conf should be enough:
[server]
use-ipv4=yes
use-ipv6=yes
enable-dbus=no
allow-interfaces=eth0,wlan0
[publish]
disable-publishing=yes
[reflector]
enable-reflector=yes
This is quite an old discussion but I tried the given answer and it broke LAN DNS, so don't do it. Instead, edit file:
nano /etc/avahi/avahi-daemon.conf
change this one line to ‘yes’:
enable-reflector=yes
restart the service:
service avahi-daemon restart
check to see if the ports are detected automatically:
systemctl status avahi-daemon.service
Takes a minute for new services to be discovered and appear. Works on Buster. The app Discovery (formerly Bonjour Browser) is helpful to verify mDNS ZEROCONF multicast packets are getting across. Also there's an iOS app.

Raspbian : Force a wlan0 static ip [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 months ago.
Improve this question
I would like to configure a wlan0 static ip and connect through it by ssh.
Here my /etc/network/interfaces :
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
adress 192.168.0.2
netmask 255.255.255.0
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.100.102
netmask 255.255.255.0
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
When I do ifconfig my wlan0 and eth0 interfaces are correctly configured :
However, when I want to ssh my raspberry by wifi, my Wifi router give to my raspberry the ip : 192.168.100.116.
How can I force my Wifi router to give 192.168.100.102 to my rapsberry pi ?
Moreover, My Wifi router give to my raspberry sometimes 192.168.100.102 and sometimes 192.168.100.116
Don't know why
I experienced similar issues when I wanted to connect my Rasperry Pi 2 with Raspbian Jessie Lite in a headless configuration for access using SSH from my Windows 10 PC with PuTTY.
In my case it turned out that the Ahavi daemon (an implementation of zeroconf) conflicted with the DHCP and name server options of my router.
Raspbian Jessie and Raspbian Jessie Lite comes pre-configured with an Avahi and a DHCP client deamon for automated network configuration. In most situation this will probably work fine, but if not, things take a turn for the worse with unpredictable side-effects.
Instead of going too deep into any details (its really a long story) I will just summarize here what I did and why. There are too many possible issues and I dont want to push you into the wrong direction. Try it out. If it fails, look for another approach or leave a comment for clarification.
Now let's get your feet wet:
Try it first on a non-productive SD card with a fresh copy of Raspbian Jessie (for headless servers I prefer Raspbian Jessie Lite).
Start the Raspberry Pi with a keyboard and a monitor connected and login with user pi.
Start raspi-config with sudo raspi-config and make sure to turn on the SSH server (menue 7 Advanced Options -> A4 SSH).
Get your current IP address for eth0 with ifconfig.
Try to connect to this address over ethernet using ssh. In case you cant connect, resolve this issue first. You cant connect over WLAN at this stage.
In case your connection succeeds, you can do the rest of your configuration using this connection over ssh.
Purge the avahi-daemon
$ sudo apt-get purge avahi-daemon
Dont worry about the error message 'rmdir: failed to remove ‘/var/run/avahi-daemon’: Directory not empty'. This is related to a an empty socket and a pid file, that currently cannot be removed. You can ignore this message or remove the directory manually, but it wont hurt in case you just leave it.
$ sudo rm -r /var/run/avahi-daemon
Configure /etc/network/interfaces
It's important to know, that we need only one single change in this file. Open /etc/network/interfaces for changes with the editor of your choice (note: if you followed this steps 1:1 extended vim is not yet installed).
$ sudo nano /etc/network/interfaces
Add the line auto eth0 just before the existing line iface eth0 inet manual. That's all we have to do here is to here. After the changes the file should look like this:
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Note: make sure to read and to understand the comments on top of the file!
Configure the DHCP client daemon
Next, as stated in the comments of the previously edited file, add the static ip configuration at the end of the configuration file for the DHCP client daemon.
It might sound silly to use a DHCP client if we are going to use static IP addresses anyway. However, when I tried to remove the DHCP client daemon I experienced new configuration and/or dependency issues. Finally I decided to keep as close to the standard configuration as possible and leave it as is.
The configuration is done at the very end in the file /etc/dhcpcd.conf (watch proper spelling). The last line in the file should read: 'nohook lookup-hostname'. So let's open the the file in the editor and scroll down to the bottom of the file.
$ sudo nano /etc/dhcpcd.conf
Add there the static ip definition. In my case the definitions at the of /etc/dhcpcd.conf look like this:
nohook lookup-hostname
# static ip configuration for eth0 and wlan0
interface eth0
static ip_address=192.168.1.61/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8
interface wlan0
static ip_address=192.168.1.62/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8
Note that the syntax is different from the syntax used in /etc/network/interfaces. For a full class C network the netmask must be provided in form of a /24 at the end of the IP address. For further details consult man dhcpcd.conf.
Reboot and test your configuration with eth0
After the next reboot you should have a reliable network configuration with static ip addresses. But remember, we are not yet finished and only eth0 will work. At this point even no IPv4 address will be shown for wlan0 if ifconfig is issued
In case it fails and you didn't do anything wrong so far (carefully check for typos), then you have another issue that must get fixed first; in this case this solution is probably not for you.
Configure WLAN
To configure WLAN we have to edit /etc/wpa_supplicant/wpa_supplicant.conf and add a network configuration to it.
In my case I'm using WPA/PSK for secure access and after modifying the country code and adding the network entry, the content of the file looks something like this:
country=CH
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
# home network; allow all valid ciphers
network={
ssid="home"
scan_ssid=1
key_mgmt=WPA-PSK
psk="very secret passphrase"
}
ssid="home" is the WLAN network name (SSID) of the WLAN router or access point.
scan_ssid=1 scans for hidden SSID's. This is usual practice but can slowdown the scan process.
key_mgmt=WPA-PSK defines that WPA pre-shared key management protocol will be used.
psk="very secret passphrase" defines your passphrase to access the router.
Replace the values assigned for ssid and psk with the real values for your secure WLAN access. There are several supported protocols, some requiring other and more options.
Consult man wpa_supplicant.conf for additional and more detailled information.
Reboot and test your configuration with wlan0
Now it's time to reboot and test wlan0. The ip address configured for wlan0 should now appear if ifconfig is issued.
If this is the case you should be able to access the Rasperry Pi using ssh with the IP address configured for wlan0.
I hope you are happy and everything works fine for you so far. If this is the case, I could stop writing here and leave you alone. But wait, there is more!
Two network connections, does that make sense?
If you connect to the Raspberry Pi from the same network over both, eth0 and wlan0 and issue an ifconfig to take a look at the transmitted and received packets or bytes, you will recognize, that even you connected to the IP for wlan0 most traffic is going through eth0.
I cant explain why this is the case, but obviously somehow the packets are routed along the faster path which sounds good so far.
However, I've seen rare situations where this behaviour was undesirable and slowed my connections down. Since I'm using my Raspberry Pi either near my router using a wired connection to eth0 or more distant from the router with no access to a cable using wlan0, I decided to stop wlan0 if a cable with an active connection to the router is attached to eth0.
To do this I created the script /home/pi/ifdown-wlan-if-not-needed.sh
#!/bin/sh
#### Shutdown wlan0
# Test eth0 for a cable attached and
# shutdown wlan0 if this is the case.
###
sleep 30
cable=$(cat /sys/class/net/eth0/carrier)
echo $cable
if [ "$cable" = "1" ]; then
sudo ifdown wlan0
fi
exit 0
Then I've set the script to executable
$ chmod +x /home/pi/ifdown-wlan-if-not-needed.sh
And added it for execution after reboot with crontab -e to the crontab of user pi.
#reboot /home/pi/ifdown-wlan-if-not-needed.sh
Name to IP address resolution
So far I have not yet addressed the name to IP address resolution problem. How this can be done best, depends on your network equipment and use case.
My router provides the ability to bind names to MAC addresses, hostnames and IPv4 adresses. However, for some reason none of these options really work. Some times it works by simply using the given name, other times the suffix .home is needed. Sometimes the names disappear from the list or no name resolution takes place even everything looks ok.
I finally defined the adresses in the C:\Windows\System32\drivers\etc\hosts file of my Windows PC and my Notebook. This is certainly not state of the art but works reliable and without hassle.
However, I cant guarantee that what I did works for you too, but I hope it brings you on the right track to get your problem solved. I've been using my configuration since the release of Raspbian Jessie about one year ago. It survived any updates without hassle so far.
Good luck!
Login on your router management interface and (if it has this feature, most do have it) reserve a DHCP address on the router for your selected client, so that the specific client always has the same IP address. See this article for more information.

Locally hosted sites not accessible over WiFi

MacBook connected on university network through Ethernet with the following settings.
Configure IPv4: Using DHCP
IP Address: 172.17.15.146
Subnet Mask: 255.255.255.0
Router: 172.17.15.254
DNS Server: 172.24.2.71
Shared my Ethernet connection over WiFi for my android phone.
IP address: 192.168.2.2
IP settings: Using DHCP
Googling MyIP through both devices give the following.
Your public IP address: 202.78.172.162
Local sites like library/ and news/ only accessible over 1st device.
Is there a way to access these sites on the 2nd?
A proxy server on your MacBook will probably let you do what you want. It's a little more involved than file sharing, but things like http://squidman.net/squidman/ can make it a little easier.
After setting up the proxy server, you'll want to set your Android up to use it. You'll want to use the local NAT address of your MacBook (likely 192.168.2.1).

Multiple NIC card with different subnet

Am using Cent OS 6.2 (64bit), I have 4 NIC interface, in that am trying to connect two NIC with different subnet,
em1 with 10.30.2.x series
em4 with 10.30.4.x series
Also I added route with /sbin/route add -net 10.30.4.0 netmask 255.255.255.0 dev em4
When I make the network device up "ifup em4" am not able to ping both the interfaces.
There is no IPtables running and selinux also disabled.
The same setup is working in one more DELL server, in that server reverse IP and IP forwarding is not enabled, even then its working.
Reverse IP & IP Forwarding
cat /proc/sys/net/ipv4/conf/em2/rp_filter
1
cat /proc/sys/net/ipv4/ip_forward
0
Any comments would be appreciated.
Thanks in advance.
If you are sure that ip addresses are actually setted on the interfaces, everything should work out, i would suggest to check network equipment on the way.
easiest way to test this is to use tcpdump -i any icmp and see if you actually receive the packets, this will also show you if your pong is going on the wrong interface.
hope that helps

How to route TCP/IP responses through a different interface?

I have two machines each with two valid network interfaces, an Ethernet interface eth0 and a tun/tap interface gr0. The goal is to start a TCP connection on machine A using interface gr0 but then have the responses (ACKs, etc) from machine B come back over the Ethernet interface, eth0. So, machine A sends out a SYN on gr0 and machine B receives the SYN on its own gr0 but then sends its SYN/ACK back through eth0. The tun/tap device is a GNU Radio wireless link and we just want the responses to come through the Ethernet.
What's the easiest way to accomplish this? I need to research more on TCP/IP, but I was initially thinking that source-spoofing outgoing packets would tell the receiver to respond to the spoofed address (which should get routed to eth0). This would involve routing the IPs from the tun/tap interfaces through gr0 and leave the other traffic to eth0.
We are using Linux and a Python solution would be preferable.
Thanks for looking!
You could add an additional address to the lo interface on each system and use these new addresses as the TCP connection endpoints. You can then use static routes to direct which path each machine takes to get to the other machine's lo address.
For example:
Machine A:
ip addr add 1.1.1.1/32 dev lo
ip route add 2.2.2.2/32 dev eth0 via <eth0 default gateway>
Machine B:
ip addr add 2.2.2.2/32 dev lo
ip route add 1.1.1.1/32 dev gr0
Then bind to 1.1.1.1 on machine A and connect to 2.2.2.2.
You may be interested in enabling logging of martian packets net.ipv4.conf.all.log_martians, and disable reverse path filtering net.ipv4.conf.<interface>.rp_filter on the affected interfaces.
This sysctl vars are accesible via the sysctl utility and/or the /proc filesystem.

Resources