Skip to content

Instantly share code, notes, and snippets.

@iskolbin
Last active January 15, 2024 13:30
Show Gist options
  • Save iskolbin/f6aa305c880fe22544d9454e724ed064 to your computer and use it in GitHub Desktop.
Save iskolbin/f6aa305c880fe22544d9454e724ed064 to your computer and use it in GitHub Desktop.
Ubuntu(systemd) tmate service for ssh

The motivation is: ssh to remote computer without port forwading, dynamic DNS and so on, the only thing needed is that the remote is connected to the Internet.

  1. On remote get tmate, put it in /usr/local/bin (apt version as of 18.04LTS is too old):
curl -L https://github.com/tmate-io/tmate/releases/download/2.4.0/tmate-2.4.0-static-linux-amd64.tar.xz|tar xJv
sudo mv tmate-2.4.0-static-linux-amd/tmate /usr/local/bin
  1. Put public keys in ~/.ssh/authorized_keys

  2. Get API key from https://tmate.io/ (or host your own server)

  3. Create ~/.tmate.conf

set tmate-api-key "<YOUR_API_KEY>"                                
set tmate-session-name "<SESSION_NAME>"                     
set tmate-authorized-keys "~/.ssh/authorized_keys"        

Session name is chosen by you

  1. Create /etc/systemd/system/tmate.service
[Install]
WantedBy=multi-user.target

[Unit]
Description=Tmate-SSH
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
Restart=always
User=root
ExecStart=/usr/local/bin/tmate -F

probably you want to change User

  1. Install tmate as service
systemctl daemon-reload
systemctl start tmate
systemctl enable tmate
  1. You can reboot and ssh from any computer with private key:
ssh <REGISTRED_USERNAME>/<SESSION_NAME>@lon1.tmate.io
@jaskiratsingh2000
Copy link

What do you mean by change of "User"?

@iskolbin
Copy link
Author

iskolbin commented Sep 3, 2021

@jaskiratsingh2000 In /etc/systemd/system/tmate.service:

Type=simple
Restart=always
User=root
ExecStart=/usr/local/bin/tmate -F

User=root the root can be changed on your any username possible on remote

@jaskiratsingh2000
Copy link

@iskolbin I actually tried with the following steps which you have mentioned above but I got an error when I am trying to access the SSh from another computer.

I actually want to access my Raspberry Pi through tmate ssh on another computer. SO I have to follow all these steps on the raspberry pi right?

@jaskiratsingh2000
Copy link

I am getting Invalid Session Token. Not sure why

@iskolbin
Copy link
Author

iskolbin commented Sep 4, 2021

Note that ssh <REGISTRED_USERNAME>/<SESSION_NAME>@lon1.tmate.io
REGISTRED_USERNAME -- is what you typed on tmate website in Username during API key registration
maybe this helps

@jaskiratsingh2000
Copy link

Yes! @iskolbin this is what I did.

@jaskiratsingh2000
Copy link

Can you please let me know once? It would really has been amazing that if there would have been some kind of video demonstration of it to check if I am not failing at some point

@jaskiratsingh2000
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment