0

For a client project, a part of the application requires me to disconnect the Raspberry Pi from the WiFi. I'm doing this operation using OS commands. To disconnect from the wifi, currently I'm calling the OS command sudo ifconfig wlan0 down which disables the network interface and the Pi is disconnected.

However, I require the network interface to be active (for certain other activities). Is there a way to do this ? I have access to the wpa_supplicant files, is there a way where I can somehow manipulate this file to disconnect without deleting the user's saved SSID and passwords?

I need to do this from the terminal itself, as the end user will have a different application running in kiosk mode, so he won't be able to access the GUI tools for networks.

2
  • 1
    Try using sudo rfkill block 0
    – Dougie
    Aug 29 at 11:53
  • Do you really need to disconnect, and why? Can't you just kill the process which uses the network? Aug 30 at 12:51
2

Preface:

This "answer" is in two parts. The first part is my assessment of your question, the second part is a suggestion intended to help you move toward a clearer understanding of your network management toolset, which will hopefully allow you to formulate a clearer question.

Part 1:

N.B. Networking questions can be difficult questions to formulate.
To put it bluntly, your question strikes me as ambiguous and unclear. You speak of "connected" and "disconnected", then you say you put the interface down (disabled) yet require it to be "active" (whatever that is) for "certain other activities". Think for a moment about what exactly each of those terms mean. I'm sure you'll appreciate that network questions are difficult - for several reasons - when they are clearly stated, but may be near impossible otherwise.

A good and clear network question can be difficult to compose. Stack Exchange recognizes this difficulty; this is why an upvote for a question and and upvote for an answer have the same point rewards. Also, a good and clear question is much more likely to net you a good and clear answer. These are the incentives for investing a bit more time and effort in your question.

Part 2:

As distributed, RPi OS uses wpa_supplicant and wpa-cli as its default "WiFi manager". wpa_cli is said to be a "complete tool" for managing a wi-fi connection set up with wpa_supplicant.

Read man wpa_cli - or wpa_cli -help to learn its options. As stated above, I'm unclear on what you mean when you said, "I require the network interface to be active (for certain other activities)", but perhaps perusing the available documentation will help you revise your question such that it is less ambiguous. For example, see the parameters enable_network and disable_network.

1

If there's no other network interfaces active, just stop dhcpcd:

sudo systemctl stop dhcpcd

This may leave the interface in a DOWN state but you can put it up again:

sudo ip l set wlan0 up

Whether or not that allows for your "certain other activities" I can't say since I don't know what they are.

-1

I manipulated the wpa_supplicant.conf file in the /etc/wpa_supplicant folder:

  1. Copy the contents of the file to a temporary file. Use some basic string manipulations to extract only the top 3 header lines.
  2. Overwrite the original wpa_supplicant.conf file with just the headers
  3. Execute wpa_cli -i wlan0 reconfigure
  4. This makes the Pi temporarily forget all networks, and therefore disconnects from the wifi leaving the interface up.
  5. When the user is ready to connect again, simply replace the contents of the wpa_supplicant.conf with temporary file created earlier and run step 3 again

Note: Always keep a backup of the original wpa_supplicant.conf somewhere safe so if things go wrong, you can undo the changes

This method also doesn't touch the wlan0 interface itself, and so is safe as long as the wpa_supplicant.conf contains the headers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.