After running my webserver via WiFi connection for the past 2 months, I've finally decided to set a proper connection using Ethernet cable. I was postponing it, as I needed a 10m cable to connect my Pi to the nearest router. But, at last, I've managed to buy the cable and am preparing for setting up a proper connection of my server.
As mentioned, I've bought a 10m High Speed CAT6 Gold Headed Network Cable and a bunch of 100 x 7mm round cable clips to keep the cable near the walls. And that's it. everything else is done on the Pi.
I've connected the cable to my router, clipped it against walls so it climbs nicely to my RasPi nest. Now comes the tricky part which is configuring static IP address on my eth0 interface.
Setting up static IP
NOTE: Before you attempt to do any changes to your network connection, make sure you do not connect with your Raspberry Pi via ssh. You may loose connection in the process. It's a good idea to connect your RPi to an external monitor and use keyboard, or connect to your PC via serial port.
Using a static IP is very useful when you want to easily access your RPi without need to get its IP address every time it boots or reconnects to the network. It simplifies the work with SSH, FTP, and, most of all, lets the RPi web server run smoothly without the need to update DNS records if you have your own domain name as I do.
To achieve this, you only need to modify the
/etc/network/interfaces file that holds your network interfaces configuration.
NOTE: Before you attempt to make any changes, especially with system files, it's a good idea to make backup of the file you want to edit. We need root privileges, to edit system files, so we use
sudobefore the command. Type:
sudo cp /etc/network/interfaces /etc/network/interfaces.old
If anything goes wrong, you can just overwrite the broken file with the old version:
sudo cp /etc/network/interfaces.old /etc/network/interfaces
You can edit the file with any text editor such as vim, joe, ed, nano, etc. Personally, I prefer to use nano.
sudo nano /etc/network/interfaces
In the file, look for a line such as:
iface eth0 inet dhcp
This helps to enable the DHCP client. We want to disable it. Put a hash at the beginning of the line to disable it or delete it:
#iface eth0 inet dhcp
In the file you must insert the following lines:
auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.xxx.xxx #your static IP gateway 192.168.1.1 #your gateway IP netmask 255.255.255.0 #your network address 'family' network 192.168.1.0 broadcast 192.168.1.255
Only the address and netmask data are strictly required.
Netmask usually is
255.255.255.0, and your gateway should end with 1:
xxx.xxx.xxx.1 (your router IP or the one given by your ISP).
If you are not sure, of any data, check your router LAN configuration.
Now, we need to restart the network:
sudo service networking restart
If that fails, try:
sudo service networking reload
And in very rare cases reboot your RPi:
sudo shutdown -r now
To check if the connection is set up properly, type ifconfig and you should see something like tihs:
eth0 Link encap:Ethernet HWaddr b8:27:eb:fb:c0:c8 inet addr:x.x.x.x Bcast:xxx.xxx.xxx.xxx Mask:255.255.255.0 inet6 addr: xxxxxxxxxxxxxxxx Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1958667 errors:0 dropped:43175 overruns:0 frame:0 TX packets:2711788 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000` RX bytes:432321252 (412.2 MiB) TX bytes:3512983066 (3.2 GiB)
for your wired connection.
If you still encounter any problems, such as no data flow even if the connection seems to be ok, and you are using any network managers, such as network-manager, or wicd, you may have to disable connection established with them. For wicd try:
- Access wicd menu:
- Hit [Shift] + P to enter preferences menu
- In General Settings under Network Interfaces tick with Spacebar '
Always show wired interface' and '
Always switch to wired connection when available' and make sure the Wires Interface shows
eth0. Hit [F10] to save.
- In the main menu, you should now see Wired Networks above Wireless Networks with the line:
0 wired-default vvvIf not, hit Q to quit and run
- Enter the
wired-defaultto configure it by pressing the Right Arrow on your keyboard.
- Tick Use Static IP and enter your connection details as used in
/etc/network/interfacesfile. Hit [F10] to save.
- Hit [Shift] + D to disconnect form existing connection, then highlight the
0 wired-default vvvwired interface and hit C to connect. After few seconds you should get your connection.
- To make sure everything works fine, restart your RPi with
sudo shutdown -r nowcommand.
Everything should work well now and you can enjoy your static wired connection.