I am using Ngrok for having remote SSH access to my home computer without setting up port forwarding and stuff like that. After messing around with it for a little, I decided to set it up to start the client whenever the computer boots, just like the SSH client itself. It should be as easy as executing the following command:
/usr/bin/ngrok start --config=/opt/ngrok/ngrok-config.yml ssh &
However, I can't find a way to execute it before entering my username and password. I have tried using systemd services, I have tried using cronjobs and I have tried using autostart links in /usr/share/applications
and /etc/xdg/autostart
, but none of those options has been able to start the program before loging in.
Just to clarify, by "login in" I mean entering my username and password to log into my desktop (GNOME). What I want to do has to happen with just pressing the power button on my PC, without me having to interact with it, because I want to be able to remotely reboot the PC if needed and still have SSH access to it.
By researching on this same forum I have found out that the @reboot
tag for cronjobs is useless for the time being, or so I have read.
I am running Parrot Sec OS 4.11, with a GNOME desktop. Linux Kernel 5.7.0 amd64
The command that I want to execute starts Ngrok's client, and it never exits, so I should have to make it run in the background. How can I have it be executed on the system startup just like the SSH client does?
This is the unit file:
[Unit]
Description=ngrok
Requires=network.target
After=network.target
[Service]
ExecStart=/bin/bash /opt/ngrok/start.sh
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
IgnoreSIGPIPE=true
Restart=always
RestartSec=3
RuntimeDirectoryMode=0755
User=root
[Install]
WantedBy=multi-user.target
When I enable the service, this is printed out:
kolterdyx@kolterdyx-pc:~$ sudo systemctl enable ngrok.service
Created symlink /etc/systemd/system/multi-user.target.wants/ngrok.service → /etc/systemd/system/ngrok.service.
This are the contents of /opt/ngrok/start.sh
:
#!/bin/bash
/usr/bin/pkill ngrok #This line ensures that no other ngrok instances are running
/usr/bin/ngrok start --config=/opt/ngrok/ngrok-config.yml ssh
After executing sudo systemctl start ngrok.service
and then `sudo journalctl -xe´, this is what I get:
-- Journal begins at Tue 2021-04-13 01:15:26 CEST, ends at Wed
[...]
ago 04 13:13:18 kolterdyx-pc wpa_supplicant[747]: wlan0: Trying to associate with 44:fb:5a:b2:d6:b4 (SSID='Cachuflos' freq=2462 MHz)
ago 04 13:13:18 kolterdyx-pc NetworkManager[736]: <info> [1628075598.4562] device (wlan0): supplicant interface state: associating -> associated
ago 04 13:13:18 kolterdyx-pc wpa_supplicant[747]: wlan0: Associated with 44:fb:5a:b2:d6:b4
ago 04 13:13:18 kolterdyx-pc NetworkManager[736]: <info> [1628075598.4563] device (p2p-dev-wlan0): supplicant management interface state: associating -> associated
ago 04 13:13:18 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
ago 04 13:13:18 kolterdyx-pc NetworkManager[736]: <info> [1628075598.5465] device (wlan0): supplicant interface state: associated -> 4way_handshake
ago 04 13:13:18 kolterdyx-pc NetworkManager[736]: <info> [1628075598.5465] device (p2p-dev-wlan0): supplicant management interface state: associated -> 4way_handshake
ago 04 13:13:18 kolterdyx-pc wpa_supplicant[747]: wlan0: WPA: Key negotiation completed with 44:fb:5a:b2:d6:b4 [PTK=CCMP GTK=TKIP]
ago 04 13:13:18 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-CONNECTED - Connection to 44:fb:5a:b2:d6:b4 completed [id=0 id_str=]
ago 04 13:13:18 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-SIGNAL-CHANGE above=0 signal=-50 noise=9999 txrate=1000
ago 04 13:13:18 kolterdyx-pc NetworkManager[736]: <info> [1628075598.5563] device (wlan0): supplicant interface state: 4way_handshake -> completed
ago 04 13:13:18 kolterdyx-pc NetworkManager[736]: <info> [1628075598.5566] device (p2p-dev-wlan0): supplicant management interface state: 4way_handshake -> completed
ago 04 13:14:16 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-SIGNAL-CHANGE above=1 signal=-49 noise=9999 txrate=117000
ago 04 13:15:01 kolterdyx-pc CRON[108374]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
ago 04 13:15:01 kolterdyx-pc CRON[108375]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
ago 04 13:15:01 kolterdyx-pc CRON[108374]: pam_unix(cron:session): session closed for user root
ago 04 13:15:07 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-SIGNAL-CHANGE above=1 signal=-50 noise=9999 txrate=104000
ago 04 13:15:10 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-SIGNAL-CHANGE above=1 signal=-49 noise=9999 txrate=117000
ago 04 13:17:01 kolterdyx-pc CRON[108414]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
ago 04 13:17:01 kolterdyx-pc CRON[108415]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
ago 04 13:17:01 kolterdyx-pc CRON[108414]: pam_unix(cron:session): session closed for user root
ago 04 13:18:18 kolterdyx-pc hostapd-wpe[905]: wlan0: STA 44:fb:5a:b2:d6:b4 IEEE 802.11: deauthenticated due to inactivity (timer DEAUTH/REMOVE)
ago 04 13:18:18 kolterdyx-pc kernel: wlan0: deauthenticated from 44:fb:5a:b2:d6:b4 (Reason: 6=CLASS2_FRAME_FROM_NONAUTH_STA)
ago 04 13:18:18 kolterdyx-pc kernel: wlan0: authenticate with 44:fb:5a:b2:d6:b4
ago 04 13:18:18 kolterdyx-pc kernel: wlan0: send auth to 44:fb:5a:b2:d6:b4 (try 1/3)
ago 04 13:18:18 kolterdyx-pc kernel: wlan0: authenticated
ago 04 13:18:18 kolterdyx-pc kernel: wlan0: associate with 44:fb:5a:b2:d6:b4 (try 1/3)
ago 04 13:18:18 kolterdyx-pc kernel: wlan0: RX AssocResp from 44:fb:5a:b2:d6:b4 (capab=0x431 status=0 aid=3)
ago 04 13:18:18 kolterdyx-pc kernel: wlan0: associated
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.6182] device (wlan0): supplicant interface state: completed -> authenticating
ago 04 13:18:18 kolterdyx-pc hostapd-wpe[905]: wlan0: STA 44:fb:5a:b2:d6:b4 IEEE 802.11: disassociated
ago 04 13:18:18 kolterdyx-pc hostapd[905]: wlan0: STA 44:fb:5a:b2:d6:b4 IEEE 802.11: deauthenticated due to inactivity (timer DEAUTH/REMOVE)
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.6183] device (p2p-dev-wlan0): supplicant management interface state: completed -> authenticating
ago 04 13:18:18 kolterdyx-pc hostapd[905]: wlan0: STA 44:fb:5a:b2:d6:b4 IEEE 802.11: disassociated
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.7256] device (wlan0): supplicant interface state: authenticating -> associating
ago 04 13:18:18 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-DISCONNECTED bssid=44:fb:5a:b2:d6:b4 reason=6
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.7257] device (p2p-dev-wlan0): supplicant management interface state: authenticating -> associating
ago 04 13:18:18 kolterdyx-pc wpa_supplicant[747]: wlan0: SME: Trying to authenticate with 44:fb:5a:b2:d6:b4 (SSID='Cachuflos' freq=2462 MHz)
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.7426] device (wlan0): supplicant interface state: associating -> associated
ago 04 13:18:18 kolterdyx-pc wpa_supplicant[747]: wlan0: Trying to associate with 44:fb:5a:b2:d6:b4 (SSID='Cachuflos' freq=2462 MHz)
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.7427] device (p2p-dev-wlan0): supplicant management interface state: associating -> associated
ago 04 13:18:18 kolterdyx-pc wpa_supplicant[747]: wlan0: Associated with 44:fb:5a:b2:d6:b4
ago 04 13:18:18 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.8429] device (wlan0): supplicant interface state: associated -> 4way_handshake
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.8429] device (p2p-dev-wlan0): supplicant management interface state: associated -> 4way_handshake
ago 04 13:18:18 kolterdyx-pc wpa_supplicant[747]: wlan0: WPA: Key negotiation completed with 44:fb:5a:b2:d6:b4 [PTK=CCMP GTK=TKIP]
ago 04 13:18:18 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-CONNECTED - Connection to 44:fb:5a:b2:d6:b4 completed [id=0 id_str=]
ago 04 13:18:18 kolterdyx-pc wpa_supplicant[747]: wlan0: CTRL-EVENT-SIGNAL-CHANGE above=0 signal=-50 noise=9999 txrate=1000
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.8528] device (wlan0): supplicant interface state: 4way_handshake -> completed
ago 04 13:18:18 kolterdyx-pc NetworkManager[736]: <info> [1628075898.8533] device (p2p-dev-wlan0): supplicant management interface state: 4way_handshake -> completed
ago 04 13:21:20 kolterdyx-pc sudo[108537]: pam_unix(sudo:auth): authentication failure; logname= uid=1000 euid=0 tty=/dev/pts/0 ruser=kolterdyx rhost= user=kolterdyx
ago 04 13:21:25 kolterdyx-pc sudo[108537]: kolterdyx : TTY=pts/0 ; PWD=/home/kolterdyx ; USER=root ; COMMAND=/usr/bin/systemctl stop ngrok
ago 04 13:21:25 kolterdyx-pc sudo[108537]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
ago 04 13:21:25 kolterdyx-pc systemd[1]: Stopping ngrok...
░░ Subject: A stop job for unit ngrok.service has begun execution
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ A stop job for unit ngrok.service has begun execution.
░░
░░ The job identifier is 11536.
ago 04 13:21:25 kolterdyx-pc systemd[1]: ngrok.service: Succeeded.
░░ Subject: Unit succeeded
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ The unit ngrok.service has successfully entered the 'dead' state.
ago 04 13:21:25 kolterdyx-pc systemd[1]: ngrok.service: Unit process 940 (ngrok) remains running after unit stopped.
ago 04 13:21:25 kolterdyx-pc systemd[1]: Stopped ngrok.
░░ Subject: A stop job for unit ngrok.service has finished
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ A stop job for unit ngrok.service has finished.
░░
░░ The job identifier is 11536 and the job result is done.
ago 04 13:21:25 kolterdyx-pc systemd[1]: ngrok.service: Consumed 4min 7.504s CPU time.
░░ Subject: Resources consumed by unit runtime
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ The unit ngrok.service completed and consumed the indicated resources.
ago 04 13:21:25 kolterdyx-pc sudo[108537]: pam_unix(sudo:session): session closed for user root
ago 04 13:21:47 kolterdyx-pc sudo[108551]: kolterdyx : TTY=pts/0 ; PWD=/home/kolterdyx ; USER=root ; COMMAND=/usr/bin/systemctl start ngrok
ago 04 13:21:47 kolterdyx-pc sudo[108551]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
ago 04 13:21:47 kolterdyx-pc systemd[1]: ngrok.service: Found left-over process 940 (ngrok) in control group while starting unit. Ignoring.
ago 04 13:21:47 kolterdyx-pc systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
ago 04 13:21:47 kolterdyx-pc systemd[1]: Started ngrok.
░░ Subject: A start job for unit ngrok.service has finished successfully
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ A start job for unit ngrok.service has finished successfully.
░░
░░ The job identifier is 11537.
ago 04 13:21:47 kolterdyx-pc sudo[108551]: pam_unix(sudo:session): session closed for user root
ago 04 13:22:05 kolterdyx-pc sudo[108593]: kolterdyx : TTY=pts/0 ; PWD=/home/kolterdyx ; USER=root ; COMMAND=/usr/bin/journalctl -xe
ago 04 13:22:05 kolterdyx-pc sudo[108593]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
ago 04 13:22:13 kolterdyx-pc sudo[108593]: pam_unix(sudo:session): session closed for user root
ago 04 13:22:19 kolterdyx-pc sudo[108602]: kolterdyx : TTY=pts/0 ; PWD=/home/kolterdyx ; USER=root ; COMMAND=/usr/bin/journalctl -xe
ago 04 13:22:19 kolterdyx-pc sudo[108602]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
sudo systemctl start ngrok.service
together withjournalctl -xe
print anything interesting?start.sh
file because if I start ngrok directly, then I can't access a local server that it creates on 127.0.0.1:4040, and I need that to be accessible for another program. If I start it through a bash script, the ngrok is executed as a child process of bash, and the local server is accessible. I'll add the rest to the questionpkill ngrok
line is because sometimes when the service restarts, the ngrok process is still lingering and I can only have one process at the same time.