Search code examples
tunnelngrok

How to keep ngrok running even when signing off of a server


I have ngrok running on a server I remote into.

I start it by using the obvious, ngrok.exe http 80. The problem is that when I sign off on that particular server, ngrok will close out and I will lose my tunnel. Is there a way I can keep the ngrok tunnel running even when I have signed off the machine? I understand if the machine is shut down there is nothing I can do to keep the tunnel running, that is obvious. Any ideas?

Thanks in advance.


Solution

  • As you've said If the machine is shutdown there will be no way keep the process running. There are a number of methods to do this. In each of these methods I'm assuming you already have the following config file:

    config.yml

    authtoken: <your-auth-token>
    tunnels:
        default:
            proto: http
            addr: 80
    

    Ngrok Link (Windows/Mac OS/Linux, Commercial)

    With ngrok link simply run the following commands:

    ngrok service install -config /path/to/your/config.yml
    ngrok service start
    

    You should then be able to manage ngrok as you would any other service running on your given operating system.

    Nohup (Maco OS/Linux)

    The nohup command normally comes installed by default on mac os and linux. To run the command as such:

    nohup ngrok start --all --config="path/to/config.yml" &
    

    Running in a screen should also achieve the same effect here.

    Creating a Windows Service (Windows)

    To create the service you will need to download a program for creating services from non service executables. Here I'm going to how to do this with NSSM (Non-Sucking Service Manager).

    1. Download the executable
    2. Open CMD and cd into the same directory as the nssm.exe
    3. Run to following command:

      nssm.exe install ngrok
      
    4. select the ngrok executable in the window that appears and add the following to the arguments, then press 'Install service'.

      start --all --config="C:\path\to\my\config.yml"
      
    5. The service can now be managed from service manager. To start it open an admin terminal and run the following:

      sc start ngrok
      

    Creating a systemd service (Linux - systemd only)

    Requires root.

    1. cd into /etc/systemd/system/

    2. Create the following file:

      ngrok.service

      [Unit]
      Description=Ngrok
      After=network.service
      
      [Service]
      type=simple
      User=<your_user_name>
      WorkingDirectory=/home/<your_user_name>
      ExecStart=/usr/bin/ngrok start --all --config="/path/to/config.yml"
      Restart=on-failure
      
      [Install]
      WantedBy=multi-user.target
      
    3. Then run the following command to start and enable the service

      systemctl enable ngrok.service && systemctl start ngrok.service
      

    sources:

    https://ngrok.com/docs/ngrok-link#service

    https://www.freedesktop.org/software/systemd/man/systemd.unit.html

    https://nssm.cc/commands