switch raspberry pi from ap to client - raspberry-pi

I have a raspi with hostapd configured. what i want to do is run a nodejs server so if there is internet via eth0 it makes a hotspot and when there is no internet via eth0 it connects to known wifi networks. my problem is in my interfaces file which looks like this
# 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 static
address 172.24.1.1
netmask 255.255.255.0
network 172.24.1.0
broadcast 172.24.1.255
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
when i comment out
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
i can use hostapd to make an AP. but not connect to wifi as a client. if i comment out
iface wlan0 inet static
address 172.24.1.1
netmask 255.255.255.0
network 172.24.1.0
broadcast 172.24.1.255
i can connect to a wifi network but hostapd wont let me make a AP. if i leave both as they are nothing seems to work.
Thank you all in advance.

Related

Able to receive ping only from one direction between two subnets Rpi 4 buster

I have a typical configuration where Rpi acts as an router between two subnets. One subnet is connected to Rpi eth0 and there is now laptop connected directly (one day there will be an ip-camera). Rpi connects via wlan0 to to wireless router to get an internet connection.
Picture of configuration
There is several instruction on the web to get this working and it almost works. I can ping devices connected to my wireless router (but not my wireless router!?), from laptop connected to Rpi. I have internet connection on laptop connected to Rpi. Problem is though that I can't ping my laptop (that is connected to Rpi) from subnet of my wireless router. Yes, it has got something to do with my iptables confuration.
Here is some data:
I have set up net.ipv4.ip_forward=1 in my sysctl.conf file
I have done these settings:
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT
IPtables look like this:
pi#raspberrypi:~ $ sudo iptables -L -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
296 191K ACCEPT all -- wlan0 eth0 anywhere anywhere state RELATED,ESTABLISHED
37 14316 ACCEPT all -- eth0 wlan0 anywhere anywhere
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Routing tabe looks like this:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.3.1 0.0.0.0 UG 100 0 0 wlan0
default 192.168.3.1 0.0.0.0 UG 202 0 0 eth0
10.10.0.0 0.0.0.0 255.255.255.0 U 202 0 0 eth0
192.168.3.0 0.0.0.0 255.255.255.0 U 100 0 0 wlan0
192.168.3.1 0.0.0.0 255.255.255.255 UH 202 0 0 eth0
Any other information that could be useful?
In your wireless router routing table you should forward traffic going to 10.10.0.0/24 to the interface connected to the second subnet, the traffic going to 192.3.0.0/24 to the interface connected to the Rpi and other traffic to the interface connected to internet.
You also should have a routing table to the Subnet-1 with your Rpi as a gateway like this:
Destination Gateway Genmask
192.168.3.0 Rpi #IP 255.255.255.0

ifup eth0 already configured

I'm building an operating system for my device with buildroot-2017.08. The operating system that I got started successfully on my device except one big problem:I have no ipv4 address for eth0.
I have an initial script that configures all interfaces that I have. When started it runs the code:
ifup -a.
I've checked that the script really runs at the start of the system and checked the output of ifup. And I get the message:"Interface eth0 already configured". Just after the boot... Who might have configured this interface? I'm stuck. So it doesn't assign the ip address cause it's already configured...
This is my /etc/network/interfaces file
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
# The loopback interface
auto lo
iface lo inet loopback
# Wired or wireless interfaces
auto eth0
# iface eth0 inet dhcp
iface eth0 inet static
address 192.168.0.136
netmask 255.255.255.0
gateway 192.168.0.2
auto can0
iface can0 inet manual
#pre-up ip link set $IFACE type can bitrate 125000 listen-only off
pre-up ip link set $IFACE type can bitrate 125000 triple-sampling on
up ifconfig $IFACE up
down ifconfig $IFACE down
Can somebody give me a clue of what is going on? Maybe I missed some important settings in make-menuconfig or anywhere else. Thank you in advance!

How to get Raspberry Pi‘s wlan1 interface work after poweroff?

I plugged an additional wifi connector in my Raspberry Pi 3B+. I can‘t see the interface of the additional connector after plugging my Raspberry Pi out of power or use sudo poweroff for save shutdown. But after sudo reboot the wifi connector is visible after typing sudo iwconfig in the terminal.
My /etc/network/interfaces contains:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
post-up iw dev $IFACE set power_save off
auto wlan1
allow-hotplug wlan1
iface wlan1 inet manual
post-up iw dev $IFACE set power_save off
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
My /etc/modules contains:
i2c-dev
bcm2835_wdt
# r8712u # this driver makes problems
r92su
The internal WiFi adapter in my Raspberry Pi 3 b+ is a broadcom and my external WiFi adapter is a Realtek RTL8191SU.
I tried already to shut down the power save mode of the wlan adapter:
post-up iw dev wlan1 set power_save off
EDIT:
Networking.service:
systemctl status networking.service
Returns:
Failed to initialize control interface 'DIR=/var/run/wpa_supplicant GROUP=netdev'. You may have another wpa_supplicant process already running or the file was left by an unclean termination of wpa_supplicant in which case you will need to manually remove this file before.
EDIT 2:
My WLAN connected after
sudo killall wpa_supplicant
sudo poweroff
Shutdown the stream and start Raspberry Pi and it works nice, but if I do this and remove a usb mouse and keyboard it won‘t connect with the wifi. It looks like there is a start problem with usb connectors of the Pi.
EDIT 3:
There seems to be a bug in the system with the USB connection and the current distribution.
Every time I remove the usb mouse and keyboard, the wlan usb adapter is no longer activated.
EDIT 4:
It could also be a driver problem for rtl8191su and therefor r8712u.
My post on the raspberry pi forum:
https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=230193&p=1410456&hilit=wlan1#p1410456
EDIT 5:
I tried already
https://www.raspberrypi.org/forums/viewtopic.php?t=191844
, but after i did this my system won‘t boot correctly and the broadcom internal wlan adapter won‘t work correctly. After this i got wlan0 and wlan1 but both were the same network adapters connected to the same wlan (strange).
So the problem is not fixed until now!
How to get Raspberry Pi‘s wlan1 interface work after shut down the stream and start the Raspberry Pi?
I looked into this issue and here is a fix that I found:
1.In the terminal type sudo nano /etc/network/interfaces
2.Edit your interfaces config file by making sure the text looks like the following...
auto lo
auto wlan0
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
4.Save your work by pressing Ctrl X and confirming save. Turn the pi off and on
5.When your Pi has restarted bring up the GUI again. Now double click the WiFi Config icon to bring up the wpa_gui properties box. If all was successful you should now see wlan0 show up in the Adapter section. From here you should scan for you SSID and set up the connection.
Hope this helps.
First suppress the loading of the modules by blacklisting and load them in a specific order later on:
Type this to the terminal:
$ sudo vi /etc/modprobe.d/wlan-blacklist.conf
This creates a new file. Make sure to end it with ".conf". Insert your modules here:
blacklist r8712u
blacklist 8192cu
Save and close the file. Now run this:
$ sudo depmod -ae
$ sudo update-initramfs -u
$ sudo vi /etc/modules
At the end of the file you add your modules in the order that they are supposed to show up (i.e. wlan0 before wlan1, etc.):
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
8192cu
r8712u
Then try turning it off and on
I got the solution for this problem:
First load the module (driver) for your usb wlan adapter after the internal chip adapter. In this example case, I got the RTL8191SU with the sudo apt-get install firmware-realtek and therefor r8712u driver.
sudo nano /etc/modprobe.d/wlan-blacklist.conf
Edit wlan-blacklist.conf like this:
blacklist r8712u
After this run:
sudo depmod -ae
sudo update-initramfs -u
sudo nano /etc/modules
Edit modules like this:
i2c-dev
brcmfmac
r8712u
Now load the r8712u module a little bit later after the internal wlan chip of the Raspberry Pi 3b+:
sudo nano /etc/crontab
Add line to crontab:
#reboot root (sleep 20; modprobe r8712u) &
Now have a right interfaces file:
sudo nano /etc/network/interfaces
wlan1 is the realtek wlan adapter and wlan0 the internal broadcom wlan chip:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
post-up iw dev wlan0 set power_save off
auto wlan1
allow-hotplug wlan1
iface wlan1 inet dhcp
post-up iw dev wlan1 set power_save off
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet manual
It is really important, that the default interface is manual!
sudo reboot
or
sudo poweroff
Both of these commands will let the wifi configured correctly. Even if I turn off the raspberry pi and then boot again.
Enjoy your second wlan adapter!

Double wifi connection (lan / internet) Ubuntu Server

I got Ubuntu Server 16.04.1 with two wireless USB adapters.
I would like to connect to two different wifi:
to my own router with wlan0; to the lan only, just to ssh into the server
to an enterprise network (802.1x/AES/AES) with wlan1 for internet access
I can establish both connections simultaneously but I don't know how to tell wlan0 to connect to the lan only (and not to the internet).
This is my /etc/network/interfaces. With this configuration, both wlan get connected but wlan0 has got internet access and wlan1 shows no internet traffic.
Is there a way to connect wlan0 to my router, with no internet access?
# auto wlan0 → for lan connection only (?)
auto wlan0
iface wlan0 inet static
address 192.168.1.xx
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
wireless-essid <my_essid>
wireless-mode managed
wpa-driver wext
wpa-conf /home/<my_username>/<my_essid>.psk.conf
# auto wlan1 → for internet connection
auto wlan1
iface wlan1 inet dhcp
wireless-mode managed
wpa-ssid WOW FI - FASTWEB
wpa-driver wext
wpa-ap-scan 1
wpa-proto RSN
wpa-pairwise CCMP TKIP
wpa-eap PEAP
wpa-key-mgmt WPA-EAP
wpa-identity <my_enterprise_id>
wpa-password <my_enterprise_pw>
wpa-phase2 auth=MSCHAPV2
Found the answer: delete the gateway address so that wlan0 config is
# auto wlan0 → for lan connection only (?)
auto wlan0
iface wlan0 inet static
address 192.168.1.xx
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
wireless-essid <my_essid>
wireless-mode managed
wpa-driver wext
wpa-conf /home/<my_username>/<my_essid>.psk.conf

How does the OS know which network interface to use for the internet?

I have a PC with two network interfaces: eth0 and eth1.
eth0 - Has an ip of 192.168.11.X/24.
eth1 - Has an ip of 192.168.130.X/24. eth1 has internet connectivity.
How does my OS know which interface to use when I try connecting to the internet? Does it iterate over all default gateways? Does it have any cache of what each interface provides? Is there any difference in the behaivior between Windows and Linux?
I'm going to answer for the Linux side of the house (at least for Debian-based systems, such as Ubuntu, since it's more common for users at this point):
Type the following into a command line:
route -n
You should see your "routing table" appear, with something like the following:
Destination Gateway Genmask ... Iface
0.0.0.0 192.168.11.254 0.0.0.0 ... eth0
169.254.0.0 0.0.0.0 255.255.0.0 ... eth0
192.168.11.0 0.0.0.0 255.255.255.0 ... eth0
192.168.130.0 0.0.0.0 255.255.255.0 ... eth1
I omitted a couple columns, but basically, the line that says "0.0.0.0" under "Destination" is the line that determines where your default route is. In other words, where all of the traffic goes that isn't destined for any of the other subnets in the other lines (google.com, facebook.com, whatever).
If it's not right (such as in the above table, where "eth1" is the card you want with Internet access), you should change the default route:
sudo route del default
sudo route add default gw 192.168.130.254 netmask 255.255.255.0
That will fix it for now. To make it permanent, edit your interfaces file:
sudo gedit /etc/network/interfaces
Edit it to look something like the following (change as necessary to your specific situation):
auto eth0
iface eth0 inet dhcp
up route del default
auto eth1
iface eth1 inet dhcp
up route add default gw 192.168.130.254 netmask 255.255.255.0
Then restart networking to see if that did the trick:
sudo /etc/init.d/networking restart
The feature you're asking about is a routing table, a list of destinations known to the host.
When the OS needs to forward a packet it checks this list and chooses the most appropriate one (from specific destinations to general ones). For example:
192.0.2.0/28 - 192.0.2.1 via eth1
198.51.100.0/27 - 198.51.100.1 via eth0
0.0.0.0/0 - 203.0.113.1 via eth0
Note the last destination: it will match any IPv4 address.