树莓派3 设置静态ip dhcpcd vs /etc/network/interfaces

网上查找了下如何设置树莓派静态ip 有两种方案

1 修改/etc/dhcpcd.conf

2修改/etc/network/interfaces

具体有什么不同。我在如下网站发现也有人问,并且也有人给出答案。感觉可以我在此引用

question:

I have this configuration in /etc/network/interfaces:

auto lo
iface lo inet loopback

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

allow-hotplug wlan0
#iface wlan0 inet dhcp
        iface wlan0 inet static
        address 192.168.0.110
        netmask 255.255.255.0
        network 192.168.0.1
        gateway 192.168.0.1
        wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp


iface eth0 inet static
    address 192.168.0.115
    netmask 255.255.255.0
    network 192.168.0.1
    gateway 192.168.0.1

The wireless static IP worked, but the eth0 didn't.

So I tried to do the config in /etc/dhcpcd.conf:

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

And it worked. I am confused and here are several questions:

  1. When to use which file?

  2. Why the wifi worked with /etc/network/interfaces but the eth0 didn't?

  3. Does dhcpcd has somehow priority over /etc/network/interface?

  4. How to check which service has priority or someting? And whichservice uses /etc/network/interface?

answer:

There are good points in all the answers to this question, but I think that there is some scope for addressing your specific questions directly.

  1. When to use which file?

I'm going to make the assumption that you are suffering from a general degree of confusion that is occurring at the moment (in the wider Raspberry Pi community) with the introduction of Debian 'Jessie' in place of Debian 'Wheezy'. This change has made a large number of tutorials at worst obsolete and at best confusing.

The answer to the question is that when using Wheezy it was normal to make changes to the /etc/network/interfaces file for the purposes of setting up network interfaces (go figure). When using Jessie, it will be necessary to make changes to '/etc/dhcpcd.conf'. However, if making changes to a wireless connection (wlan0) you will also need to make changes to /etc/wpa_supplicant/wpa_supplicant.conf which is where you add the network ssid and password.

  1. Why the wifi worked with /etc/network/interfaces but the eth0 didn't?

I'm not sure how the wifi connection could have worked since there is some information missing from your files (ssid at the least). As janos pointed out, the priority of the etho connection details coming after the wlan0 details probably made them operative (since they would have been seen last by the process reading the file).

  1. Does dhcpcd has somehow priority over /etc/network/interface?

No, they're different and are designed to suit different purposes under Jessie. For the purposes of using Jessie in a fairly simple way you can pretty much ignore the interfaces file and work with dhcpcd.conf and wpa_supplicant.conf.

  1. How to check which service has priority or someting? And which service uses /etc/network/interface?

Again I'm going to make the assumption that the question is more of a 'Which file do I use and if I have to use one which takes priority?' question. The answer is that with the change from Wheezy to Jessie (and in a broader sense with the adoption of systemd) the configuration of `dhcpcd.conf' and 'wpa_supplicant.conf' will be the norm and the 'interfaces' file will be left to it's own devices.

What does this mean for you?

Well (again) making an assumption that you're trying to set up a hard wired (eth0) and wireless (wlan0) connections with static IP addresses, you would want your interfaces file to be the default as it was initially installed;

# 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

You would want your dhcpcd.conf file to include the connection details at the end of the file for both interfaces and the additional entries would probably look a bit like this;

# Custom static IP address for eth0.
interface eth0
static ip_address=192.168.0.115/24  //自己得ip
static routers=192.168.0.1         //无线路由ip
static domain_name_servers=192.168.0.1  //DNS 设成跟路由ip一样,网关忽略可以不要。

# Custom static IP address for wlan0.
interface wlan0
static ip_address=192.168.0.115/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

Lastly you would want to edit your wpa_supplicant.conf file so that it includes the ssid for the wifi network and the password. It should probably look a bit like this;

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

network={
    ssid="homenetwork"
    psk="h0mepassw0rd"
}

I hope that covers it off. There is the very real possibility that my assumptions are incorrect, but since I recently went through a similar learning curve I'm hoping the data is useful.

回答的作者说啦。这主要是由于版本差异 Debian 'Jessie' in place of Debian 'Wheezy'.

我的是jessie选择修改的是 dhcpcd.conf这个。其中作者也说了如何设置usb无线的配置文件。

设置好后 sudo reboot就可以啦。

原文的网址:https://raspberrypi.stackexchange.com/questions/39785/dhcpcd-vs-etc-network-interfaces




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值