Shell-Script

無法使用 NetworkManager ‘up’ 腳本啟動 OpenVPN

  • September 3, 2017

當我的乙太網連接啟動時,我正在嘗試自動啟動 OpenVPN(它將連接到 Private Internet Access VPN)。

我有 /etc/NetworkManager/disaptcher.d/99-custom_script_handler

#!/bin/bash
if [ "$1" = "eth0" ] && [ "$2" = "up" ]
then
 echo "$(date): Starting tunnel script" >> /root/tunnel.log
 /bin/bash /root/start_tunnel_script
else
 echo No action for this combination
fi

這可以正常執行並啟動“start_tunnel_script”

/root/start_tunnel_script

#!/bin/bash
echo "$(date): Running tunnel script as $(whoami)" >> /root/tunnel.log
LOCATION="Netherlands"
openvpn --echo --daemon --cd /etc/openvpn/client/pia_ovpn/ --config "$LOCATION.ovpn" --auth-user-pass /root/.pia_credentials --script-security 2 --syslog --up /root/bootstrap

但是 OpenVPN 報告它找不到配置文件

Sep  3 15:12:39 test openvpn[4056]: Options error: In [CMD-LINE]:1: Error opening configuration file: Netherlands.ovpn
Sep  3 15:12:39 test openvpn[4056]: Use --help for more information.
Sep  3 15:12:39 test nm-dispatcher: req:4 'up' [eth0], "/etc/NetworkManager/dispatcher.d/99-custom_script_handler": complete: failed with Script '/etc/NetworkManager/dispatcher.d/99-custom_script_handler' exited with error status 1.
Sep  3 15:12:39 test NetworkManager[645]: <warn>  [1504465959.2123] dispatcher: (40) 99-custom_script_handler failed (failed): Script '/etc/NetworkManager/dispatcher.d/99-custom_script_handler' exited with error status 1.
Sep  3 15:12:40 test NetworkManager[645]: <info>  [1504465960.9892] policy: set 'eth0' (eth0) as default for IPv6 routing and DNS

但是配置文件明顯存在:

# ls -al /etc/openvpn/client/pia_ovpn/
total 160
drwxr-xr-x. 2 root root 4096 Sep  2 14:37 .
drwxr-x---. 3 root root   22 Sep  2 14:37 ..
-rw-r--r--. 1 root root  295 Feb  6  2017 AU Melbourne.ovpn
-rw-r--r--. 1 root root  285 Feb  6  2017 AU Sydney.ovpn
-rw-r--r--. 1 root root  288 Feb  6  2017 Brazil.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 CA Montreal.ovpn
-rw-r--r--. 1 root root 2025 Feb  6  2017 ca.rsa.2048.crt
-rw-r--r--. 1 root root  292 Feb  6  2017 CA Toronto.ovpn
-rw-r--r--. 1 root root  869 Feb  6  2017 crl.rsa.2048.pem
-rw-r--r--. 1 root root  289 Feb  6  2017 Denmark.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 Finland.ovpn
-rw-r--r--. 1 root root  288 Feb  6  2017 France.ovpn
-rw-r--r--. 1 root root  289 Feb  6  2017 Germany.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 Hong Kong.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 India.ovpn
-rw-r--r--. 1 root root  289 Feb  6  2017 Ireland.ovpn
-rw-r--r--. 1 root root  288 Feb  6  2017 Israel.ovpn
-rw-r--r--. 1 root root  287 Feb  6  2017 Italy.ovpn
-rw-r--r--. 1 root root  287 Feb  6  2017 Japan.ovpn
-rw-r--r--. 1 root root  288 Feb  6  2017 Mexico.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 Netherlands.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 New Zealand.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 Norway.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 Romania.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 Singapore.ovpn
-rw-r--r--. 1 root root  284 Feb  6  2017 South Korea.ovpn
-rw-r--r--. 1 root root  288 Feb  6  2017 Sweden.ovpn
-rw-r--r--. 1 root root  287 Feb  6  2017 Switzerland.ovpn
-rw-r--r--. 1 root root  288 Feb  6  2017 Turkey.ovpn
-rw-r--r--. 1 root root  291 Feb  6  2017 UK London.ovpn
-rw-r--r--. 1 root root  296 Feb  6  2017 UK Southampton.ovpn
-rw-r--r--. 1 root root  295 Feb  6  2017 US California.ovpn
-rw-r--r--. 1 root root  292 Feb  6  2017 US Chicago.ovpn
-rw-r--r--. 1 root root  289 Feb  6  2017 US East.ovpn
-rw-r--r--. 1 root root  292 Feb  6  2017 US Florida.ovpn
-rw-r--r--. 1 root root  292 Feb  6  2017 US Midwest.ovpn
-rw-r--r--. 1 root root  296 Feb  6  2017 US New York City.ovpn
-rw-r--r--. 1 root root  292 Feb  6  2017 US Seattle.ovpn
-rw-r--r--. 1 root root  298 Feb  6  2017 US Silicon Valley.ovpn
-rw-r--r--. 1 root root  290 Feb  6  2017 US Texas.ovpn
-rw-r--r--. 1 root root  289 Feb  6  2017 US West.ovpn

如果我以 root 身份從 shell 執行“start_tunnel_script”,它可以正常工作——這就是關鍵。問題在於 start_tunnel_script 是如何啟動的,但我無法——在我的一生中——弄清楚有什麼不同。

SELinux 上下文在“pia_ovpn”文件夾中設置不正確

restorecon -R /etc/openvpn/client

引用自:https://unix.stackexchange.com/questions/390126