Raspbian_How do I set up networking/WiFi/Static IP

via: http://raspberrypi.stackexchange.com/questions/37920/how-do-i-set-up-networking-wifi-static-ip

 

The Raspbian released in May 2015 changed the way networking (and particularly WiFi) works.

This applies to the current Foundation releases of Raspbian Jessie and the last Raspbian Wheezy 2015-05-05 (no longer available).

This makes all the existing tutorials obsolete. (Note the tutorials can still be used if you want to use the older style of manual configuration, but you need to disable dhcpcd.)

sudo systemctl stop dhcpcd.service will stop (deactivate) dhcpcd.

The newer configuration (based on dhcpcd) has many advantages (and a few minor drawbacks) and is recommended for new users.

How to setup Raspbian Networking

If you are using an Ethernet connection with a router there should be no configuration required and it should work out of the box.

If you are using WiFi and the GUI set up by following the Foundation Guidelineshttps://www.raspberrypi.org/documentation/configuration/wireless/README.md

If you are using WiFi from the Command Line set up /etc/wpa_supplicant/wpa_supplicant.conf by following https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md This is the only way to set up a network which does not broadcast SSID.

This can also be used to setup a 'Headless' system e.g. using a serial console cable, but it MUCH easier if you can borrow a monitor and keyboard for setup. See below for another possible method using an Ethernet cable to a PC.

Networking Files

If you are running a recent Raspbian /etc/network/interfaces should be as below. If you have changed it PUT IT BACK.

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

If the Foundation had setup dhcpcd as the author intended /etc/network/interfaces would not be needed. The 10-wpa_supplicant hook automatically launches WPA supplicant on wireless interfaces and can be installed if desired.

The file /etc/wpa_supplicant/wpa_supplicant.conf will be created/modified by the recommended setup methods, but can be setup by hand. It should contain something like the following:-

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="ESSID"
    psk="Your_wifi_password"
}

If you need to connect to a private network (i.e. no broadcast SSID) include the line scan_ssid=1inside network={⋯}NOTE there are many other options which can be used see man wpa_supplicant.conf.

Setup a Static IP Address

Questions about setting Static IP Address are among the most common on this site. There are very many tutorials (many wrong, obsolete or incomplete).

Disclaimer

Before proceeding I feel obliged to state that setting up a static address is NOT recommended. Telecommunications Engineers do not do this. Static IP Addresses can be the bane of a Network Administrator's life. There are situations where Static IP Addresses are necessary e.g. if you are running a DHCP server, or running on an isolated network with no DHCP server.

If you are determined to proceed anyway you should make sure you get it right. Adapted fromFoundation Network Tutorial

Find the Settings of your local Network

This is most easily done with the Pi itself, using DHCP, but can be done on any computer on your network, although the commands may differ on other systems.

Run either of the following (depending on whether you are using Ethernet or WiFi):

ip -4 addr show dev eth0 | grep inet
ip -4 addr show dev wlan0 | grep inet

which should give an output like:

inet 10.1.1.30/24 brd 10.1.1.255 scope global eth0
inet 10.1.1.31/24 brd 10.1.1.255 scope global wlan0

The first address is the IP address of your Pi on the network, and the part after the slash is the network size. It is highly likely that yours will be a /24.

The second address is the brd (broadcast) address of the network.

Find the address of your router (or gateway)

ip route | grep default | awk '{print $3}'

10.1.1.1

Finally note down the address of your DNS server, which is often the same as your gateway.

cat /etc/resolv.conf

# Generated by resolvconf
nameserver 10.1.1.1

Then follow ONE of the following options. (There are other methods not documented here. These are the most common on Raspbian.)

Network Interfaces method

Configure a static network adddress on your Pi in /etc/network/interfaces

auto eth0
iface eth0 inet static
        address 10.1.1.30
        netmask 255.255.255.0
        gateway 10.1.1.1

allow-hotplug wlan0
iface wlan0 inet static
        address 10.1.1.31
        netmask 255.255.255.0
        gateway 10.1.1.1
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

address is the address from the command above (or another unused address on the same network),
netmask 255.255.255.0 corresponds to network size/24.
gateway is the address of your router (or gateway).

You can also specify dns-nameservers, but this is generally not necessary.
The broadcast is automatically derived from address and netmask and need not be specified.
For more detail see https://wiki.debian.org/NetworkConfiguration

You can set either or both eth0 or wlan0

Then disable the DHCP client daemon and switch to standard Debian networking:

sudo systemctl disable dhcpcd
sudo systemctl enable networking

Reboot for the changes to take effect:

sudo reboot

dhcpcd method

Leave /etc/network/interfaces at its default (as above).

Edit /etc/dhcpcd.conf as follows:-

 Here is an example which configures a static address, routes and dns.
       interface eth0
       static ip_address=10.1.1.30/24
       static routers=10.1.1.1
       static domain_name_servers=10.1.1.1

       interface wlan0
       static ip_address=10.1.1.31/24
       static routers=10.1.1.1
       static domain_name_servers=10.1.1.1

ip_address is the address and size from the command above (or another unused address on the same network),
routers is the address of your router (or gateway).
domain_name_servers is the DNS address(es) from /etc/resolv.conf.
(see man dhcpcd.conf)

There is a good article on dhcpcd at https://wiki.archlinux.org/index.php/dhcpcd The Fallback profile is an alternative to static IP

Connecting a Computer to the Pi

Recent versions of Raspbian (which use dhcpcd) allow ssh to work over a link-local address andavahi (which is a zeroconf implementation) enables programs to discover hosts running on a local network.

This means you can plug the Pi into a Computer (with an Ethernet cable) or a local network router and connect without knowing the IP address.

You can easily connect from Linux and OS X with ssh pi@hostname.local (the default hostname is raspberrypi) This should work with popular GUI ssh programs. This is sometimes problematic with some versions of Windows and networks which use .local in a non-standard way. (Seehttps://en.wikipedia.org/wiki/.local)

NOTE .local resolution does not always work e.g. in rsync. The following should resolve IP (and can be included in bash scripts)
RemotePi=$(getent hosts hostname.local | awk '{ print $1 }')

If you have multiple Pi you need to make sure each Pi has a unique hostname.

You can use a crossover cable, but you don't need one (most modern interfaces automatically detect).

One drawback of direct connection is that the Pi will have no Internet access and the date will not be set. You can copy the date from the host by running ssh pi@hostname.local sudo date -s$(date -Ins) before connection.

Scope:

This tutorial is about setting up a normal Raspbian installation to access the internet using the included software. It also covers connections to other computers on the same network.

It is primarily aimed at helping new users struggling to get their Pi (especially WiFi) working .

It does NOT cover:-

  • Other Network Managers.
  • Running advanced networking on the Pi (e.g. DHCP servers, Tunnelling, VPN, Access Point).
  • Use of ipv6 (although if you have an ipv6 network this should work).
  • Alternate networking setups. (There are many different ways of setting up networking.)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值