In this post I have gathered various tips and tricks that can make you a more productive SSH user.
Copying SSH keys to the server
A lot of beginners when faced with the problem of uploading their public keys to the server, follows a manual process. I will demonstrate this, by trying to upload my public key to a Raspberry PI manually:
Now I can connect to Raspberry PI using my private key:
I was lucky - in my case everything worked, but often it doesn’t.
The main culprit is usually a wrong access permission set either on
Here is how the right permissions should look like:
PROs do not think about permissions, nor they manually edit
Instead they use
ssh-copy-id util (a part of the official SSH distribution).
One nice thing about
ssh-copy-id is that it always uploads the public key,
even if you (like I in the above example) specify private key file on the command line.
ssh-add is a nice utility that manages currently active (loaded) private keys.
Loaded keys can be automatically used for authentication.
To load a key type:
ssh-add to load
To list currently loaded keys:
To remove all keys:
When you connect to a remote machine you have an option to pass your currently loaded keys along. This is called ssh agent forwarding and allows you to ssh into further machines using your private keys:
For example without key forwarding I cannot log from my Raspberry PI into my PC:
With key forwarding enabled (
-A) I can:
WARNING: You should only use forwarding when logging into servers that
you fully trust (see
man ssh for details).
TIP: With the power of
ssh-add there is no reason to have a key without password protection.
Remember to password protect all your keys. Always!
Sometimes your user name is too long or hard to remember or maybe the server name is
or maybe your server uses a non-standard SSH port. Any of these can make typing the right ssh command
in the terminal very hard. But there is a nice solution to this,
we just need to add an entry for our server to
For example for my Raspberry PI computer I added the following entry:
Now I can ssh into it by simply typing:
scp command may be used to copy files, both from a server to the local machine:
Or from the local machine to the server:
TIP: If are going to transfer a big file remember to
gzip it first.
For more complex scenarios we can use either
or use command line file manager like
mc (Midnight Commander):
F9to select menu bar and
- Enter the connection URL:
And then you may use standard
mc commands to copy/move/modify files
on the server and the local machine.
To exit SFTP mode just enter
.. in the top directory to which you
Let’s say that there there is a service running on my Raspberry PI that is accessible only
localhost. For example it can be a simple web server:
Because server is listening only on
127.0.0.1 instead of
we cannot access it from outside of my Raspberry PI:
To access the server we may e.g. forward connections to port 5432 on my laptop to port 7777 on pi (this is called tunneling or port forwarding). We can do this using SSH:
The general syntax is
Notice that for the
remote-machine we have chosen
localhost, but we could e.g.
chose other computer to which
pi can connect.
local-machine part can be omitted (
localhost is the default).
If you don’t need the SSH session only the tunnel, you can run
ssh as a background task
(this will not be a shell background task, so it will not show up in
Port forwarding can also work in the opposite direction. We may make a service running on my laptop accessible on Raspberry PI:
Here syntax is
remote-machine:remote-port are forwarded to
By default the open ports are available on
localhost only (think security!).
If you want to accept connections from outside, you need to change
GatewayPorts settings of sshd daemon to
yes on the server (the machine to which you connect to):
Now we can make
pi accept connection from the entire local network and
forward them to my laptop:
TIP: The single quotes are needed to prevent shell expansion of
Providing internet connection via SSH
Say on your local machine you cannot access e.g.
facebook.com for some strange reason.
But you noticed that on a certain server there is no such restriction,
and you can e.g. use
links to browse
Since FB does not looks good in
links you really need a better solution…
Behold, SSH comes to the rescue. By using a dynamic port forwarding we may establish SOCKS tunnel to the server:
-C is for compression,
1080 is the standard SOCKS port.
Now we need to configure a browser. Firefox is well known for excellent SOCKS support:
facebook.com is working again!
A trick like this was very popular when I was living in a dormitory. Our dorm internet connection was very slow, but as CS students we had SSH access to a few servers, some of them with very fast internet connections…
Here I will explain how to do this on MacOS. First we need to install XQuartz, which is X11 port for MacOS:
Next we need to start XQuartz.app (it will only show in the dock).
Now we must enable X11 forwarding support on Raspberry PI:
And finally we can start our X session:
Now when I typed
vlc a VLC window popped up on my laptop.
The resolution isn’t great (it would look native if I used X11 port forwarding on Linux),
this is due to poor retina display support in XQuartz. But still it works:
Tmux session manager
Sometimes you want to leave some program running on the server even if you close
you SSH connection. Or maybe you are doing something important, e.g. devops stuff and
you want to make sure that the command you are executing will not be interrupted.
For cases like this (…and for hacker movie lovers) terminal multiplexers where invented.
The two most popular ones are
Here I will shortly explain what
tmux deserves its own blog post,
fortunately for me this post is already written.
Here I will only show you one single use-case, just to give you the taste what
tmux is all about.
Let’s connect to Raspberry PI and start
Next I will perform some manual intervention (the physical connection got busted):
My SSH session on my laptop hanged so I had to kill it with
Now after I fixed the Ethernet connection and reconnected to
I can execute
tmux ls and see my previous
Let’s attach tmux to this session:
Looks like after attaching I could recover all my previous work!
tmux session will not survive the server restart. Make sure that the server
will not be restarted while running important computations within
Congrats! Now you are SSH PRO!