Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 9:37 am

Is it possible to somehow share the OpenVPN connection I have on my Windows laptop to an RPi4 sitting on the same home LAN as the laptop?
The reason is that I am programming for the RPi on the RPi itself (using FreePascal) and I want to have the code managed through subversion at our company office. The SVN server is not externally connected so whenever I use it on the laptop I connect an OpenVPN tunnel to the company and then I can use the SVN.

If I work on a Linux virtual machine hosted by VMWare on my laptop I get direct access from Linux to the company SVN server since the virtual machine is using NAT on the laptop host for networking and thus sees the same network as the laptop does.
This has worked great for all Linux (Intel based CPU) coding, but I cannot use it for RPi coding since the RPi cannot be virtualized via VMWare...

So I have to figure out some other way to be able to use subversion from the RPi...

I have in the past tried to install openvpn client on the RPi and connect a tunnel to the company directly from it, but unlike OpenVPN client on Windows I have failed to find a way on the RPi to connect the tunnel only when needed and pull it down when no longer needed.
Maybe it can be done, but in that case I would be grateful for advice on how...

Any ideas?
Bo Berglund
Sweden

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 10:26 am

I have failed to find a way on the RPi to connect the tunnel only when needed and pull it down when no longer needed.
This sounds like the most sensible solution. I have never tried OpenVPN but I would think that it runs as a service that can be started/stopped.
( Which version or Raspbian are you running ? )

Code: Select all

systemctl status | grep vpn
If you find the correct name (my guess openvpn-client) then, you can run

Code: Select all

sudo systemctl stop name.service
sudo systemctl start name.service
sudo systemctl disable name.service
The "disable" command should prevent it to start automatically on every boot if you dont want that.

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 10:47 am

topguy wrote:
Wed Oct 30, 2019 10:26 am
I have failed to find a way on the RPi to connect the tunnel only when needed and pull it down when no longer needed.
This sounds like the most sensible solution. I have never tried OpenVPN but I would think that it runs as a service that can be started/stopped.
( Which version or Raspbian are you running ? )
September 2019 version of Raspbian Buster fully updated/upgraded

Code: Select all

systemctl status | grep vpn
If you find the correct name (my guess openvpn-client) then, you can run

Code: Select all

sudo systemctl stop name.service
sudo systemctl start name.service
sudo systemctl disable name.service
The "disable" command should prevent it to start automatically on every boot if you dont want that.
I have not yet installed OpenVPN on my dev RPi, only on about a half dozen RPi:s but then running as servers to connect into the local network from outside.
Only once did I try to use it on RPi as a client and that failed because of the lack of client control, I think I had to start the client in a separate command window if I remember correctly because it would not return without a Ctrl-C, which killed it.
But it was a fair number of years ago so I might not remember the correct details. Anyway I gave up since it was such a hassle.
I might need to start asking at the OpenVPN forum...

EDIT:
I found this webpage which sets it up to always connect, which is not what I want....
Bo Berglund
Sweden

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 1:59 pm

I found this webpage which sets it up to always connect, which is not what I want....
I took a look at the files installed by the openvpn package on Buster: https://packages.debian.org/buster/armh ... n/filelist
And you can see the systemd service files there

Code: Select all

/lib/systemd/system/openvpn-client@.service
/lib/systemd/system/openvpn-server@.service
/lib/systemd/system/openvpn.service
/lib/systemd/system/openvpn@.service
So I think that my instructions on how to disable the automatic start should work. If not you can just uninstall the whole package.

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 2:35 pm

Code: Select all

systemctl status | grep vpn
If you find the correct name (my guess openvpn-client) then, you can run

Code: Select all

sudo systemctl stop name.service
sudo systemctl start name.service
sudo systemctl disable name.service
The "disable" command should prevent it to start automatically on every boot if you dont want that.


Well, I did this:

Code: Select all

pi@rpi4-gui:~ $ sudo apt install openvpn
.......... installation display.....

pi@rpi4-gui:~ $ systemctl status | grep vpn
           │   │ └─28342 grep --color=auto vpn
pi@rpi4-gui:~ $ sudo systemctl status | grep vpn
           │   │ ├─28345 grep --color=auto vpn
So there is really nothing there concerning openvpn as a service.

I will try to manually run openvpn with my ovpn file and see what happens...
But now I first have to go and fetch my grandchildrenb at school...
Bo Berglund
Sweden

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 3:04 pm

Its probably not running until you have configured it for a VPN server, it might try to start but fail.

Someone who actually run OpenVPN on buster should chime in if they want.

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 6:14 pm

So I have gotten it to work half way now.
This is how I did it:
- Installed openvpn via apt (sudo apt install openvpn)
- Copied my ovpn file to /etc/openvpn/client (sudo cp myname.ovpn /etc/openvpn/client
- Created a file (sudo nano /etc/openvpn/client/auth.txt) and entered the password as a single line
- Edited the ovpn file (sudo nano /etc/openvpn/client/myname.ovpn)
- Added the single line: askpass /etc/openvpn/auth.txt at the end

Now I can start openvpn client on the command line:

Code: Select all

openvpn --config /etc/openvpn/client/myname.ovpn --daemon
This command returns so I can give other commands afterwards.
If I exclude the final --daemon from the command then it will not exit back to the terminal but sit there running openvpn.
While this is going on the RPi is now connected to the remote location and I can reach the SVN server.

Question:
If I want to shut down the connection I have two options dependingh on how it started:
1) Started with --daemon
Here I need to find its pid via ps aux and then enter sudo kill <pid value>
This can be done in the same terminal as starded openvpn or in any other terminal.
2) Started without --daemon
In this case the terminal is blocked while running openvpn, so the simplest way is to just enter Ctrl-C in this terminal and openvpn closes and the prompt returns.

I would like to automate (script) this with two scripts, one to start the connection and the other to close it.
Should be possible to start from the Buster GUI.

So I think I need the --daemon in the command.
But how can I then kill openvpn inside a script?
Is there some other way than knowing the process pid?

Code: Select all

kill <pid>
killall openvpn
Bo Berglund
Sweden

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 7:38 pm

Does the "openvpn" return a lot of text when its running ?

You can also just add "&" at the end of the command instead of "--daemon". This will start the process in the background but all the output will still go into the terminal/shell you are using. Which is irritating if you try to do other CLI commands in the in the shell and the background process output text frequently.
When you want to stop a background process, you type "fg" ( foreground ) and press CTRL-C. ( or "killall openvpn" )

Its a general linux/shell trick that if you forget to add a "&" then you can press CTRL-Z to suspend the command and then write "bg" ( background ) to let the suspended task continue in the background.

There is also some shell trick that a lot of system scripts are using, you can get the PID of the last command by the variable "$!"

Code: Select all

#!/bin/bash
my-app &
echo $! >>/tmp/my-app.pid
And then this value can be read back and used to kill just that process.

Code: Select all

#!/bin/bash
kill $(cat /tmp/my-app.pid)

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Sharing my Windows OpenVPN connection to an RPi4?

Wed Oct 30, 2019 8:20 pm

topguy wrote:
Wed Oct 30, 2019 7:38 pm
There is also some shell trick that a lot of system scripts are using, you can get the PID of the last command by the variable "$!"

Code: Select all

#!/bin/bash
my-app &
echo $! >>/tmp/my-app.pid
And then this value can be read back and used to kill just that process.

Code: Select all

#!/bin/bash
kill $(cat /tmp/my-app.pid)
Shouldn't the start command be:

Code: Select all

#!/bin/bash
my-app &
echo $! > /tmp/my-app.pid
I.e. a single > so that the pid is not appended to an existing pid value in the file?

And the kill command:

Code: Select all

#!/bin/bash
kill $(cat /tmp/my-app.pid)
rm /tmp/my-app.pid
So that the pid file is removed after use...
Bo Berglund
Sweden

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Sharing my Windows OpenVPN connection to an RPi4?

Thu Oct 31, 2019 9:55 am

You are correct..

Return to “Raspberry Pi OS”