Search code examples
sshraspberry-pi

Raspberry pi - Toggle SSH from terminal (without raspi-config)


I want to have a physical toggle switch near my Raspberry pi 4, in order to turn on and off an SSH access, for security reasons. The Raspberry does not have a screen connected to it most of the times, so this is only for the situations I need to service it.

Currently the SSH is on by default, so I need to find a solution how to toggle it from my program in python (= command/sequence of commands)

I only found how to do it with raspi-config, but that has too many user interactions required, which is not ideal.


Solution

  • If everything you want to do is to stop and start ssh you can use systemctl:

    sudo systemctl start ssh
    sudo systemctl stop ssh
    

    If you don't want it to start at boot:

    sudo systemctl disable ssh