How does the OS know which network interface to use for the internet? - sockets

I have a PC with two network interfaces: eth0 and eth1.
eth0 - Has an ip of 192.168.11.X/24.
eth1 - Has an ip of 192.168.130.X/24. eth1 has internet connectivity.
How does my OS know which interface to use when I try connecting to the internet? Does it iterate over all default gateways? Does it have any cache of what each interface provides? Is there any difference in the behaivior between Windows and Linux?

I'm going to answer for the Linux side of the house (at least for Debian-based systems, such as Ubuntu, since it's more common for users at this point):
Type the following into a command line:
route -n
You should see your "routing table" appear, with something like the following:
Destination Gateway Genmask ... Iface
0.0.0.0 192.168.11.254 0.0.0.0 ... eth0
169.254.0.0 0.0.0.0 255.255.0.0 ... eth0
192.168.11.0 0.0.0.0 255.255.255.0 ... eth0
192.168.130.0 0.0.0.0 255.255.255.0 ... eth1
I omitted a couple columns, but basically, the line that says "0.0.0.0" under "Destination" is the line that determines where your default route is. In other words, where all of the traffic goes that isn't destined for any of the other subnets in the other lines (google.com, facebook.com, whatever).
If it's not right (such as in the above table, where "eth1" is the card you want with Internet access), you should change the default route:
sudo route del default
sudo route add default gw 192.168.130.254 netmask 255.255.255.0
That will fix it for now. To make it permanent, edit your interfaces file:
sudo gedit /etc/network/interfaces
Edit it to look something like the following (change as necessary to your specific situation):
auto eth0
iface eth0 inet dhcp
up route del default
auto eth1
iface eth1 inet dhcp
up route add default gw 192.168.130.254 netmask 255.255.255.0
Then restart networking to see if that did the trick:
sudo /etc/init.d/networking restart

The feature you're asking about is a routing table, a list of destinations known to the host.
When the OS needs to forward a packet it checks this list and chooses the most appropriate one (from specific destinations to general ones). For example:
192.0.2.0/28 - 192.0.2.1 via eth1
198.51.100.0/27 - 198.51.100.1 via eth0
0.0.0.0/0 - 203.0.113.1 via eth0
Note the last destination: it will match any IPv4 address.

Related

How to emulate QEMU to connect server sockets

I have set up a QEMU virtual machine (VM) trying to emulate an ARM Cortex-A9 cpu on my lubuntu VM (on VirtualBox). Using the kernel, initrd and image of this article, I start QEMU like this:
qemu-system-arm -M vexpress-a9 -cpu cortex-a9
-m 512
-kernel vmlinuz-3.2.0-4-vexpress
-initrd initrd.img-3.2.0-4-vexpress
-drive if=sd,file=debian_wheezy_armhf_standard.qcow2
-append "root=/dev/mmcblk0p2"
-nic user,hostfwd=tcp::5555-:22
After it boots up, I have configurated an static IP on the guest. Modifying the path /etc/network/interfaces.
auto eth0
iface eth0 inet static
address 192.168.0.102
netmask 255.255.255.0
gateway 192.168.0.11
The VM lubuntu, is on the same IP range (192.168.0.1) and my goal is to be able to stablish a connection from QEMU VM to three server sockets which are on port 12000, 13000 and 14000 on the lubuntu VM.
Which parameters do i have to modify/add? I've been reading the network documentation but any test i do, is not working.
UPDATE 1
Following #Peter Maydell advice i changed the network configuration to run a dhcp client instead of an static IP.
# /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
So when running # ip a i get the IP 10.0.2.15/24. Anyways, now I'm being able to reach every net from QEMU but, to be honest, i would prefer to set up a static IP.
You can't just pick an IP address at random for your guest. It has to match the fake network that user-mode networking creates, which is (as the wiki page you link to mentions) on 10.0.2.x by default. It will not (and should not be) on the same IP range as the host or this other VM. The simplest thing is to have the guest run a DHCP client, which will then be able to automatically pick up the right network config from the fake DHCP server that the user-mode networking sets up.
Cross-VM communication as you want should be doable. First check that you can connect from the host to those ports on the lubuntu VM. If you can't do that then you need to fix that VM's config first. Once that is working, then it should also work to connect from the in-QEMU guest to the lubuntu VM on the same IP address/port. This is because outbound connections from the QEMU user-mode-networked guest to either the host or to the outside world require no special configuration. It's only inbound connections to the QEMU guest that need hostfwd setup.

ifup eth0 already configured

I'm building an operating system for my device with buildroot-2017.08. The operating system that I got started successfully on my device except one big problem:I have no ipv4 address for eth0.
I have an initial script that configures all interfaces that I have. When started it runs the code:
ifup -a.
I've checked that the script really runs at the start of the system and checked the output of ifup. And I get the message:"Interface eth0 already configured". Just after the boot... Who might have configured this interface? I'm stuck. So it doesn't assign the ip address cause it's already configured...
This is my /etc/network/interfaces file
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
# The loopback interface
auto lo
iface lo inet loopback
# Wired or wireless interfaces
auto eth0
# iface eth0 inet dhcp
iface eth0 inet static
address 192.168.0.136
netmask 255.255.255.0
gateway 192.168.0.2
auto can0
iface can0 inet manual
#pre-up ip link set $IFACE type can bitrate 125000 listen-only off
pre-up ip link set $IFACE type can bitrate 125000 triple-sampling on
up ifconfig $IFACE up
down ifconfig $IFACE down
Can somebody give me a clue of what is going on? Maybe I missed some important settings in make-menuconfig or anywhere else. Thank you in advance!

ipadm to set ip address of an interfce as 0.0.0.0

Is there any command or option in ipadm command to set the ip addres as 0.0.0.0
in solaris 11.
when I tried to create ip address using the command ipadm, getting the below error.
ipadm create-addr -T static -a 0.0.0.0/0 net0/v4
ipadm: cannot create address: Invalid address
The address 0.0.0.0 is an IPv4 reserved address, so you may not assign it to an interface. Please see https://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses as well as https://stackoverflow.com/a/3655736/2299087 for more details.
Why do you want to create an interface with this address, anyway?

Add Additional Ips to my new Dedicated Server

I'am not really into Commands and Dedciated server but i had to buy one recently with 5 usuable Ips and got the informations Bellow :
Usable IP Range: 142.54.190.234 - 142.54.190.238
Gateway: 142.54.190.233
Subnet Mask:255.255.255.248
How can add those Ips to the Main one via SSH ?? since i can connect to only the first one which is 142.54.190.234 knowing that im planing to Install Powermta on that server Btw the machine is runing centos 6
Assuming your default interface is eth0 then you'll be able to add the other ip address as aliases to the default interface (eth0) or whatever interface name you have there.
ifconfig eth0:0 142.54.190.235 netmask 255.255.255.248
ifconfig eth0:1 142.54.190.236 netmask 255.255.255.248
ifconfig eth0:2 142.54.190.237 netmask 255.255.255.248
ifconfig eth0:3 142.54.190.238 netmask 255.255.255.248
If you can add these in /etc/rc.local at the end just to be sure that in case of a restart/reboot of the server, these ips are added after boot.
There are other methods as well but this would be the easiest one and it does exactly what you need.

ssh issues with zeroconf client on ubuntu

I have an embedded device which when connected via usb, it gets a IP assigned. I should be able to telnet to this device using the assigned IP. I can see this in the ifconfig of my ubuntu machine.
Problem:
When I try to telnet, it does not connect and waits endlessly.
Workaround:
Disconnect the usb interface on my ubuntu system and run command $sudo dhclient enp0s20f0u9 manually. This refreshes the usb interface IP (getting the same ip address reassigned) but this time I can successfully telnet to the device.
This I need to do every time I plug in the device using usb. Very annoying. Any idea how to fix this permanently?
Another workaround solution is via updating the routing table.
The routing table at first was:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.8.1 0.0.0.0 UG 100 0 0 enp0s31f6
169.254.0.0 0.0.0.0 255.255.255.252 U 100 0 0 enp0s20f0u9
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 docker0
169.254.0.1 169.254.0.2 255.255.255.255 UGH 100 0 0 enp0s20f0u9
172.16.8.0 0.0.0.0 255.255.254.0 U 100 0 0 enp0s31f6
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
As we see, there are two entries for the interface enp0s20f0u9. Deleting the below route also solved the issue.
sudo route del -net 169.254.0.1 gw 169.254.0.2 netmask 255.255.255.255 dev enp0s20f0u9
Apparently this problem exists after Ubuntu 14.04. This post provides a solution but I am not keen on trying this if it will break something else.