This is quite simple but can seem complicated if doing it for the first time. We are just going to edit a file, but we do need to get some information from our Raspberry Pi to use in this file.
Lets first just take a take a look at the file we are going to edit. Connect to your Raspberry Pi and in the terminal type
You should see something like below, we will come back to this file soon, I just wanted to show you what we are going to edit.
An example of the text we are going add on to the end of this file is below. We are going to have to replace this example below with the information we get out of the Raspberry Pi.
Example
interface eth0
static ip_address=192.168.0.1/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface wlan0
static ip_address=192.168.0.1/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
You can download this example directly to your Raspberry Pi using the terminal command below
This will get saved to your current working directory under the file name dC79x2FQ
Notice that the example is the same lines of text just written twice, the only difference being interface eth0 and interface wlan0. These are the wired and wireless network interfaces of the Raspberry Pi, we will not need to to change these lines of text.
Type in the terminal
See in the output we are been shown 3 different records?
eth0
eth0 - inet addr:192.168.0.10
wlan0 - inet addr:192.168.0.11
These are the current IP address for each network interface and for my example I am just going to use the IP address already being used by my Raspberry Pi.
You can choose your own by changing the last digit to what you want, but bare in mind that if another device on your network uses the same IP as you choose here you could have issues. It it is for this reason I am going to use the IP address each network interface already has, as my static IP`s.
So we are almost done now need to change the first line of each interface and we just chose our static IP`s. Just 2 more lines to sort out for each interface(highlighted below), both requiring the same information.
interface eth0
static ip_address=192.168.0.10
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface wlan0
static ip_address=192.168.0.11
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
To get the static routers part, type this in to terminal
See the Gateway? In my example it is 192.168.0.1, this is the router on my network.
Then for the static domain name server, type this in to the terminal
The address shown will probably match the static router part you just got? Either way that is the information you want.
And we are done, I ended up with.
interface eth0
static ip_address=192.168.0.10
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface wlan0
static ip_address=192.168.0.11
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
Now we need to add this on to the end of the file we used the cat command on at the start of this post.
In the terminal type
This will open the file in nano, use the arrow keys here to move the cursor to the end of the file.
Paste your information here (right shift + insert or right click the mouse)
Press Ctrl+x to exit and confirm you want to save your changes by pressing y and then Enter.
All done, reboot your Raspberry Pi and then then SSH in to the two newly set up static IP`s to test them, the IP address of each network interface is now Static!
Lets first just take a take a look at the file we are going to edit. Connect to your Raspberry Pi and in the terminal type
cat /etc/dhcpcd.conf

You should see something like below, we will come back to this file soon, I just wanted to show you what we are going to edit.
An example of the text we are going add on to the end of this file is below. We are going to have to replace this example below with the information we get out of the Raspberry Pi.
Example
interface eth0
static ip_address=192.168.0.1/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface wlan0
static ip_address=192.168.0.1/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
You can download this example directly to your Raspberry Pi using the terminal command below
wget http://pastebin.com/download/dC79x2FQ
This will get saved to your current working directory under the file name dC79x2FQ
Notice that the example is the same lines of text just written twice, the only difference being interface eth0 and interface wlan0. These are the wired and wireless network interfaces of the Raspberry Pi, we will not need to to change these lines of text.
Type in the terminal
ifconfig
See in the output we are been shown 3 different records?
eth0
lo
wlan0
Take a look at the line for the eth0 section and wlan0 section where it says
wlan0
Take a look at the line for the eth0 section and wlan0 section where it says
inet addr:
Mine shows
eth0 - inet addr:192.168.0.10
wlan0 - inet addr:192.168.0.11
These are the current IP address for each network interface and for my example I am just going to use the IP address already being used by my Raspberry Pi.
You can choose your own by changing the last digit to what you want, but bare in mind that if another device on your network uses the same IP as you choose here you could have issues. It it is for this reason I am going to use the IP address each network interface already has, as my static IP`s.
I will change the static ip_address to 192.168.0.10 for interface eth0
And change the static ip_address to 192.168.0.11 for interface wlan0
Just the same as in the ifconfig output.
So we are almost done now need to change the first line of each interface and we just chose our static IP`s. Just 2 more lines to sort out for each interface(highlighted below), both requiring the same information.
interface eth0
static ip_address=192.168.0.10
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface wlan0
static ip_address=192.168.0.11
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
To get the static routers part, type this in to terminal
route -n
See the Gateway? In my example it is 192.168.0.1, this is the router on my network.
Then for the static domain name server, type this in to the terminal
cat /etc/resolv.conf
The address shown will probably match the static router part you just got? Either way that is the information you want.
And we are done, I ended up with.
interface eth0
static ip_address=192.168.0.10
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface wlan0
static ip_address=192.168.0.11
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
Now we need to add this on to the end of the file we used the cat command on at the start of this post.
In the terminal type
sudo nano /etc/dhcpcd.conf
This will open the file in nano, use the arrow keys here to move the cursor to the end of the file.
Paste your information here (right shift + insert or right click the mouse)
Press Ctrl+x to exit and confirm you want to save your changes by pressing y and then Enter.
All done, reboot your Raspberry Pi and then then SSH in to the two newly set up static IP`s to test them, the IP address of each network interface is now Static!
This is essential for when we open the Raspberry Pi up to the internet in the future.







No comments:
Post a Comment