Network configuration for static IP and automatic wifi connection

5,065

I have a similar configuration: static ip on eth0 and dynamic on wlan0.

This is the content of my /etc/network/interfaces file:

# 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

Where I configure the dhcp or static addresses is in /etc/dhcpcd.conf

interface eth0
static ip_address=192.168.20.40
static routers=192.168.20.1
nogateway

interface wlan0
static routers=192.168.1.1
static domain_name_servers=8.8.8.8 8.8.4.4

(I adapted the file to your configuration)

You also have to configure the Wifi (SSID, password, etc) in /etc/wpa_supplicant/wpa_supplicant.conf (as I supose you already did)

NOTE: I'm using raspbian jessie. I think in previous versions (wheezy) network configuration is done different

EDIT: Added configuration for DNS

Share:
5,065

Related videos on Youtube

mozcelikors
Author by

mozcelikors

Updated on September 18, 2022

Comments

  • mozcelikors
    mozcelikors over 1 year

    I would like to set up my Raspberry Pi as static IP device, which communicates over Ethernet (through an IP like 192.168.20.X), and at the same time it should be connected to the internet through WLAN (192.168.1.1). However when I allow 'auto eth0' then it does not configure the wlan0. I would like both of them to work. My current configuration is given below:

    source-directory /etc/network/interfaces.d
    auto lo
    
    iface lo inet loopback
    
    auto eth0  
    iface eth0 inet static
     address 192.168.20.40
     netmask 255.255.255.0
     network 192.168.20.1
     gateway 192.168.20.1
    
    
    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
    

    What am I missing? Any help would be appreciated. Thanks in advance.

    SOLVED:

    source-directory /etc/network/interfaces.d
    auto lo
    
    iface lo inet loopback
    
    auto eth0  
    iface eth0 inet static
     address 192.168.20.45
     netmask 255.255.255.0
     network 192.168.20.1
     gateway 192.168.1.1    # Changing this worked
     dns-nameservers 8.8.8.8 8.8.4.4
    
    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
    
  • migrc
    migrc over 7 years
    This other question maybe can help: dhcpcd-vs-etc-network-interfaces
  • mozcelikors
    mozcelikors over 7 years
    I'm using jessie and I'm afraid it cant find no wireless interfaces now. eth0: Link is down, wlan0:Not associated. Also, the wpa psk stuff is correctly configured as I connected to it before. I simply added interfaces below /etc/dhcpcd.conf. Maybe I need to edit some stuff there?
  • GAD3R
    GAD3R over 7 years
    It is the same answer proposed by @migrc , you should accept it as the best/working answer .
  • migrc
    migrc over 7 years
    Not exactly @GAD3R. He set the configuration in /etc/network/interfaces while I suggested him to configure the connection using the file /etc/dhcpcd.conf (and he said it didn't work for him). Thanks anyway.