How do you disconnect an OpenVPN tunnel on CentOS 7?



  • I established a tunnel using the command "openvpn --config xyz.ovpn" but I couldn't figure out a way to disconnect the tunnel without rebooting the server. What command would you use to disconnect it on CentOS 7?



  • @sn said in How do you disconnect an OpenVPN tunnel on CentOS 7?:

    @StrongBad I attempted to add the following line to the .bashrc

    alias openvpn-disconnect='kill -9 $( ps -e | grep openvpn | grep -v grep | awk '{ print $1 }' )'

    When sourced the .bashrc file, I received the following errors.

    -bash: alias: print: not found
    -bash: alias: } 😞 not found

    That's because you messed up your quotes. Look at them, you have this:

    kill -9 $( ps -e | grep openvpn | grep -v grep | awk
    

    Then you have this:

    { print $1 }
    

    Fix your quotes and it will likely work fine. If it doesn't run in a normal command line, it won't work in an alias command.



  • @sn said in How do you disconnect an OpenVPN tunnel on CentOS 7?:

    I established a tunnel using the command "openvpn --config xyz.ovpn" but I couldn't figure out a way to disconnect the tunnel without rebooting the server. What command would you use to disconnect it on CentOS 7?

    kill -9 $( ps -e | grep openvpn | grep -v grep | awk '{ print $1 }' )
    

    http://www.commandlinefu.com/commands/using/kill



  • @sn What is the virtual adapter called that openvpn is using for it's connection?

    ifdown virbr0


  • @thwr That was the perfect answer I was looking for! Thank you.

    One more question,
    I tried to create an alias for that command but it gave me a "-bash: alias: print: not found" error.
    I tried to replace awk '{ print $1 }' with cut -d' ' -f1 but still got similar error.
    Adding the command to a bash script also did not work.

    Am I doing anything wrong here or is there a better way to shorten the command?



  • @sn said in How do you disconnect an OpenVPN tunnel on CentOS 7?:

    @thwr That was the perfect answer I was looking for! Thank you.

    One more question,
    I tried to create an alias for that command but it gave me a "-bash: alias: print: not found" error.
    I tried to replace awk '{ print $1 }' with cut -d' ' -f1 but still got similar error.
    Adding the command to a bash script also did not work.

    Am I doing anything wrong here or is there a better way to shorten the command?

    What was the alias command that you attempted?



  • @sn said in How do you disconnect an OpenVPN tunnel on CentOS 7?:

    @thwr That was the perfect answer I was looking for! Thank you.

    One more question,
    I tried to create an alias for that command but it gave me a "-bash: alias: print: not found" error.
    I tried to replace awk '{ print $1 }' with cut -d' ' -f1 but still got similar error.
    Adding the command to a bash script also did not work.

    Am I doing anything wrong here or is there a better way to shorten the command?

    Sure, np. Stopping an OpenVPN tunnel is basically just stopping the process. Signal 9 may be a bit hard, try lower levels to let it gracefully stop.

    About the alias: see @StrongBad's answer



  • @StrongBad I attempted to add the following line to the .bashrc

    alias openvpn-disconnect='kill -9 $( ps -e | grep openvpn | grep -v grep | awk '{ print $1 }' )'

    When sourced the .bashrc file, I received the following errors.

    -bash: alias: print: not found
    -bash: alias: } 😞 not found



  • @sn said in How do you disconnect an OpenVPN tunnel on CentOS 7?:

    @StrongBad I attempted to add the following line to the .bashrc

    alias openvpn-disconnect='kill -9 $( ps -e | grep openvpn | grep -v grep | awk '{ print $1 }' )'

    When sourced the .bashrc file, I received the following errors.

    -bash: alias: print: not found
    -bash: alias: } 😞 not found

    That's because you messed up your quotes. Look at them, you have this:

    kill -9 $( ps -e | grep openvpn | grep -v grep | awk
    

    Then you have this:

    { print $1 }
    

    Fix your quotes and it will likely work fine. If it doesn't run in a normal command line, it won't work in an alias command.



  • @StrongBad said in How do you disconnect an OpenVPN tunnel on CentOS 7?:

    @sn said in How do you disconnect an OpenVPN tunnel on CentOS 7?:

    @StrongBad I attempted to add the following line to the .bashrc

    alias openvpn-disconnect='kill -9 $( ps -e | grep openvpn | grep -v grep | awk '{ print $1 }' )'

    When sourced the .bashrc file, I received the following errors.

    -bash: alias: print: not found
    -bash: alias: } 😞 not found

    That's because you messed up your quotes. Look at them, you have this:

    kill -9 $( ps -e | grep openvpn | grep -v grep | awk
    

    Then you have this:

    { print $1 }
    

    Fix your quotes and it will likely work fine. If it doesn't run in a normal command line, it won't work in an alias command.

    It does run in a normal command line without changing the quotes!



  • @sn are you sure? The quotes are definitely wrong. How do you run it normally? You put quotes around the whole thing?



  • So you are running it like this...

    openvpn-disconnect='kill -9 $( ps -e | grep openvpn | grep -v grep | awk '{ print $1 }' )'
    

    ANd the value of the variable is what you want?



  • @StrongBad Thank you!, I fixed the quote and my alias is now working!!

    Now let me explain.

    As you suggested, it was absolutely related to the messy quotes I was using. I changed the outer one to double quotes as below and the alias worked immediately.

    alias openvpn-disconnect="kill -9 $( ps -e | grep openvpn | grep -v grep | awk '{ print $1 }' )"

    When I said it was working in a normal command line, I was just entering the command without the outer quotes which means there was only one set of quote ( around { print $1} ) and hence it worked without any issues.

    @thwr Thank you again for "killer" command!



  • @sn NP.


Log in to reply