Generate key
ssh-keygen -t ed25519
Write config
Create a file named config
inside ~/.ssh
and add the following.
Host <server-name>
Hostname <ip>
User <username>
IdentityFile <path-to-private-key>
Now you can log in to your server easily with
ssh <server-name>
instead of having to type ssh -i <path-to-private-key> <username>@<Hostname>
Fix disconnection on idle
Client will keep sending null packets every 100 seconds to keep the connection alive. Add
ServerAliveInterval 100
to either
sudo vi /etc/ssh/ssh_config
or
vi ~/.ssh/config
Alternatively, you can do ssh -o ServerAliveInterval=100 me@remote
Reverse Port Forwading
Add these lines to /etc/ssh/sshd_config
on your server
GatewayPorts clientspecified
AllowTcpForwarding yes
Then open the terminal on your local computer, paste the following and change the values suitably
ssh -o ExitOnForwardFailure=yes -v -N -R "*:$SERVER_PORT:*:$CLIENT_PORT" server_name
Flags:R
= Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side.N
= Do not execute a remote command, in simple words, do not log in to the server.v
= Verbose mode. Causes ssh to print debugging messages about its progress.
server_name
is either username@ip
or server name that you configured on your ~/.ssh/config
If you want to bind to only IPv4 addresses, use 0.0.0.0
instead of *
If there is frequent disconnection, follow this to fix it.