WHAT'S NEW?
Loading...

How to connect your Raspberry Pi to your internet router with a standard Ethernet cable and setup a static IP

Suppose we have a Raspberry Pi connected to the TV with an HDMI cable and that uses the internet connection via Wifi. In this example, the operating system is Ubuntu Mate. What we want to get is to connect the Raspberry Pi directly to the home router using an Ethernet cable and set up a static IP address so that we can connect it later using SSH.

First of all, while we are connected to Wifi, we enable Raspberry to get a dynamic IP from the Ethernet cable. To enable this feature we will have to edit the file

/etc/network/interfaces

and add these 2 lines if they are not present

auto eth0
iface eth0 inet dhcp

the result file should be similar to this

# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp

Connect the Raspberry Pi to your router with the Ethernet cable and do a reboot.

From command line do: ifconfig



You should see a result similar to the one up. To find your Ethernet IP you have to look at the interface Eth0 and then look at the inet (IPv4) section, in the example is : 192.168.1.48

Use the route -n command and mark the values you get

raspberry@raspberry:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    1024   0        0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

My example values are

Gateway 192.168.1.1
Newtwork 192.168.1.0
NetMask 255.255.255.0

Now we have all informations we need to proceed

sudo nano /etc/network/interfaces


Comment the line about dynamic IP address and Eth0 interface by using #
add the line to obtain the static IP and the lines of your configuration as the example. This sets your static IP, and the appropriate settings for your resolv.conf. You'll notice it uses your local DNS, a Google DNS (8.8.8.8 and 8.8.4.4)  the final result must be similar to the follow.

# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
#iface eth0 inet dhcp
allow-hotplug eth0
iface eth0 inet static
address 192.168.1.48
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 192.168.1.1 8.8.8.8 8.8.4.4

All done! The Raspberry static IP via Ethernet cable will be 192.168.1.48 in our exmple. Last thing to do is to reboot the system.

sudo reboot

2 comments: Leave Your Comments

  1. good post, however only good if you have a 100Mb network devices as the Pi* is sadly not gigabit!

    ReplyDelete
    Replies
    1. oops wrong post, please edit out " however only good if you have a 100Mb network devices as the Pi* is sadly not gigabit!" or remove, thanks

      Delete