Category Archives: RaspberryPi

Right way to connect to a wireless network and assign a static IP address in Raspbian

Connect to a wireless network

Add these code to /etc/wpa_supplicant/wpa_supplicant.conf

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

Then ifdown and then ifup your wireless interface.
Raspbian should connect to the network and get an IP address by dhcpcd automatically.

Set static IP address for an interface

NEVER modify /etc/network/interfaces again.

Add these to /etc/dhcpcd.conf

interface eth0
static ip_address=192.168.0.200/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

Replace eth0 and the addresses as what you want.

ref: 1 2

Raspbian GNU/Linux 7 配置WIFI

在树梅派原厂的,基于Debian Wheezy的系统中配置WIFI连接,让它开机自动连热点…这样就可以无头使用了…

偷懒,问度娘。不过度娘说的大都都用不了。想偷懒还是不行滴…

查Debian手册,找到了这个:WiFi/HowToUse#wpasupplicant

树梅派系统默认带了wpa_supplicant,所以只要wlan0能识别出来就可以(可以用ip link命令确认)。

执行wpa_passphrase,获得psk。假设WIFI名字(SSID)是myssid,密码是my_very_secret_passphrase。

wpa_passphrase myssid my_very_secret_passphrase

得到以下输出:

network={
        ssid="myssid"
        #psk="my_very_secret_passphrase"
        psk=ccb290fd4fe6b22935cbae31449e050edd02ad44627b16ce0151668f5f53c01b
}

复制psk=ccb290fd4fe6b22935cbae31449e050edd02ad44627b16ce0151668f5f53c01b备用。

用一个你喜欢的编辑器打开/etc/network/interfaces

如果你想要dhcp,修改wlan0的部分为:

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
    wpa-ssid myssid
    wpa-psk  ccb290fd4fe6b22935cbae31449e050edd02ad44627b16ce0151668f5f53c01b

如果你想要静态IP,假设IP为192.168.0.2,子网掩码255.255.255.0,网关为192.168.0.1,修改wlan0的部分:

allow-hotplug wlan0
auto wlan0
iface wlan0 inet static
    wpa-ssid myssid
    wpa-psk  ccb290fd4fe6b22935cbae31449e050edd02ad44627b16ce0151668f5f53c01b
    address  192.168.0.2
    netmask  255.255.255.0
    gateway  192.168.0.1
    # dns-nameservers 192.168.0.1 # not work

dns-nameservers在这里不起作用。事实上,resolvconf并没有默认安装。
所以,dns服务器需要直接修改/etc/resolv.conf,例如将dns改为192.168.0.1:

nameserver 192.168.0.1

关于resolvconf和dns-nameservers的细节,戳这里