回答

收藏

[教程] 【一网打尽】Raspberry Pi Course Week 3 – Day 5 (Raspberry Pi Static I...

Raspberry Pi Raspberry Pi 1903 人阅读 | 0 人回复 | 2015-03-28

The Raspberry Pi is automatically set to obtain an IP address from your wired or wireless network.  Why does the Raspberry Pi need an IP address?  This address is needed so that any traffic destined for your Raspberry Pi will be able to find it on the network.

This is method of obtaining an IP address is called DHCP or Dynamic Host Configuration Protocol.  It is sometimes referred to as a dynamic IP.  Your router will normally distribute these IP addresses but it isn’t guaranteed that it will get the same IP address every time.  This can cause problems if you are trying to connect to your Raspberry Pi remotely.

So how do you set a static IP address?

Login and enter the following command

cat /etc/network/interfaces

The line below is referring to the eth0 interface and that is should use DHCP.

iface eth0 inet dhcp

To change this line to a static IP address, enter the following command to retrieve your existing IP address and network information.

sudo ifconfig

Make a note of your current IP address, the broadcast IP and the Mask.  This is represented by the

inet addr: 192.168.0.6 [CHANGE]
Bcast: 192.168.0.255 [CHANGE]
Mask: 255.255.255.0

One last thing that is required is the gateway.  This is in most cases your routers internal IP address.

Enter the following

sudo route -nee

Make a note of Gateway address.
Gateway: 192.168.0.1

Next you need to edit the interfaces file to set this static information.  Enter the following command.

sudo nano /etc/network/interfaces

Remove the line that reads

iface eth0 inet dhcp

Add the following

iface eth0 inet static
address 192.168.0.6
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1

Save the file by pressing CTRL-X and select Y to save the changes.

When the raspberry Pi reads this file when booting up, it will look at the interface eth0 and set it to a static IP address.  I will set the IP address to 192.168.0.6, the network to 192.168.0.0, the broadcast address and the gateway address so that it can find its way out of the network.


[color=rgb(51, 51, 51) !important][backcolor=rgb(255, 195, 53) !important]
Buy Raspberry Pi 2 Model B from Amazon




Reboot your Raspberry Pi with the following command

sudo reboot

Login back in and enter the following.

ping 192.168.0.1 -c5

This will ping your gateway address or router and will return :

64 bytes from 192.168.1.254: icmp_req=1 ttl=255 time=2.18
64 bytes from 192.168.1.254: icmp_req=2 ttl=255 time=2.43
64 bytes from 192.168.1.254: icmp_req=3 ttl=255 time=3.24
64 bytes from 192.168.1.254: icmp_req=4 ttl=255 time=2.20
64 bytes from 192.168.1.254: icmp_req=5 ttl=255 time=3.37

One last thing that needs to be modified is the /etc/resolv.conf file.  This file contains information of DNS name resolvers that allow your raspberry pi to resolve names to IP addresses.  For example, if you pingwww.suntimebox.com the raspberry pi will have to determine the IP address of www.suntimebox.com.

Enter the following command to edit the resolv.conf file.

sudo nano /etc/resolv.conf

Enter the follow Google public dns server IP address.

nameserver 8.8.8.8
name server 8.8.4.4

Press CTRL-X to exit but remember to save the file by accepting the changes.

If you prefer, you can always make a reservation on your router or server.  For this you will have to make a note of the hardware address when you enter the

sudo ifconfig

command.  The hardware address is represent as the HWaddr: 08:00:22:ef:65:bc.  Note that your hardware address will be different.

Please let me know if I have missed anything or you want to add something that will benefit someone or a comment saying thank you would be nice.  It makes it all worthwhile knowing that I helped someone and if I didn’t, let me know where I went wrong.


分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条