0

I wrote simple server-client OpenVPN config files:

server.conf

dev tun0
ifconfig 10.8.0.1 10.8.0.2

client.conf

dev tun0
ifconfig 10.8.0.2 10.8.0.1
remote 192.168.0.123

which basically is a cleartext OpenVPN tunnel - for now on its ok to me, I just learn it from basics, the point is - yay, it works! :)

I also wrote a tiny simple bash script to automatically run client:

#!/bin/bash
openvpn --config client.conf

and saved as run.sh. Then, wanted to add it to cron to allow client connect to vpn server at the time I want.

I did:

crontab -e
11 5 * * * /home/mirx/run.sh

but I dont see any connected client on server site - does my cron entry work or not? On client site I dont see any new tun interface when I check it with ifconfig. Any ideas?

mirx
  • 159
  • 2
  • 9

1 Answers1

2

Update

Your script must use full path to configuration file, as cron daemon does not run as your user (nor from your home directory).

#!/bin/bash
openvpn --config /home/mirx/client.conf

Also make sure you are editing root's cronjob as you need root to connect to openvpn network

Update 2

Also it seems you might have your corn syntax mixed, have this header to help you set it up :

#minute (0-59),
#|      hour (0-23),
#|      |       day of the month (1-31),
#|      |       |       month of the year (1-12),
#|      |       |       |       day of the week (0-6 with 0=Sunday).
#|      |       |       |       |       commands

Old Answer

I recommend placing ampersand (&) at the end to make sure it runs in the background:

11 5 * * * /home/mirx/run.sh &

This way you can check if it is running or kill it if needed with ps ax | grep 'run.sh'

Also, if you are using any variables like $HOME or relative paths in your run.sh script it will not work, as cron is ran as a separate user which usually does not have access to most of your environment variables.

phoops
  • 2,073
  • 4
  • 18
  • 23
  • Hmm, did everything you wrote and still dont have any incoming connections in my server – mirx Mar 19 '14 at 21:59
  • are you editing crontab of root user? As openvpn requires root privileges to run properly. – phoops Mar 19 '14 at 22:01
  • yes, checkes as root - same result = no connection – mirx Mar 19 '14 at 22:13
  • can you try setting it to `/home/mirx/run.sh &> /tmp/cron.log` and then check if you get any output in /tmp/cron.log file. If you do not, then either cron daemon is not running or you have your time mixed up. I have added an example header you can use in your crontab file to help you setup times. Please let me know if it helps. – phoops Mar 19 '14 at 22:17
  • my cron.log is empty – mirx Mar 19 '14 at 22:34
  • but cron is running, checked with `ps -ef | grep cron | grep -v grep` – mirx Mar 19 '14 at 22:36
  • but you did not create the file manually? If it was created by cron process then it means the script is being run. Can you also use full path to openvpn binary (you can find where it is by this command: `which openvpn`). Also the problem might be that cron user does not have sufficient privileges to access a tun/tap device. – phoops Mar 19 '14 at 23:36
  • Ok, tried it once again. I did everything as root: 1) wrote client config: http://pastie.org/private/r0kyhqrmyeqtgrgcvy00pq 2) wrote client run script: http://pastie.org/private/qvcltjyfcjpaujxdtaeffq 3) did `chmod u+x run.sh` 4) added line to crontab: `1 4 * * * /tmp/run.sh &> /tmp/cron.log` ... and still nothing. Cron is working (checked again with `ps -ef | grep cron | grep -v grep`) and file `cron.log` is empty (I didnt create it it was created by cron) - finally, connected to server, many, many thanks :D but why `cron.log` is empty btw? – mirx Mar 20 '14 at 21:36