18

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.

Yusha
  • 1,496
  • 2
  • 14
  • 30

1 Answers1

43

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

Hobbid Hobbin
  • 534
  • 5
  • 6
  • 4
    Unfortunately Ngrok Link is a separate product and cost money, so this won't work unless you pay. – Yusha Jun 12 '18 at 16:50
  • 2
    I've updated my answer to show this and included instructions on how to set up ngrok as a service without needing ngrok link. – Hobbid Hobbin Jun 14 '18 at 00:12
  • Where do we put the config file? – Kavi Vaidya Dec 19 '18 at 02:40
  • 1
    The config file can be placed anywhere as long as you supply the path to the file as in the examples. If no config param is passed it will look in the default file location as specified by the documentation here: https://ngrok.com/docs#default-config-location the $HOME in $HOME/.ngrok2/ngrok.yml is the home directory of the user running the process (or the system32 folder when running a windows service through nssm). – Hobbid Hobbin Dec 20 '18 at 03:57
  • I installed ngrok using Snapd, and had this line on the ngrok.service - ExecStart=/var/lib/snapd/snap/bin/ngrok start --all --config="/snap/ngrok/config.yml". I didn't include user and working directory, as i just wanna keep ngrok running for Jenkins. I tried to run the command that you gave but it threw me error (code=exited, status=1/FAILURE). Any idea on what went wrong? – Fred A Jan 20 '20 at 04:27
  • For the systemd service, are you sure you didn't mean `After=network.target`? – NReilingh May 31 '20 at 19:09
  • how can i achive this with the free version of ngrok? This solution is only for ngrok-link a version for which i have to pay – Opa114 Dec 27 '20 at 17:32
  • how to make it run on startup using nohup? I can make it work using the terminal at the moment – jash101 Feb 09 '21 at 04:41
  • In Windows, after installing nssm with 'choco install nssm', this worked for me: nssm install YourServiceName cmd.exe /K ngrok http --config="C:\path\to\your\config\.ngrok2\ngrok.yml" --region=us --hostname=my.speficic.hostname.com 443 – Rodolfo G. Oct 06 '21 at 07:13
  • does restarting service change the ngrok address to connect to? – Ro. Nov 16 '21 at 00:12