This document is a WORK IN PROGRESS.
This is just a quick personal cheat sheet: treat its contents with caution!
SSH¶
SSH is a cryptographic network protocol for operating network services securely over an unsecured network.
In addition to remote terminal access provided by the main ssh
binary, the ssh
suite of
programs has grown to include other tools such as scp
(Secure Copy Program) and sftp
(Secure
File Transfer Protocol).
Reference(s)
- https://wiki.archlinux.org/index.php/OpenSSH
- https://wiki.archlinux.org/index.php/SSH_keys
- https://wiki.gentoo.org/wiki/SSH
- https://www.funtoo.org/OpenSSH_Key_Management,_Part_1
- https://www.funtoo.org/OpenSSH_Key_Management,_Part_2
- https://www.funtoo.org/OpenSSH_Key_Management,_Part_3
- https://infosec.mozilla.org/guidelines/openssh
- https://tutox.fr/2020/04/16/generer-des-cles-ssh-qui-tiennent-la-route/
- https://github.com/arthepsy/ssh-audit
- https://github.com/jtesta/ssh-audit
- https://linuxfr.org/users/gouttegd/journaux/utilisation-d-un-tpm-pour-l-authentification-ssh
- https://stribika.github.io/2015/01/04/secure-secure-shell.html
Table of content¶
Install¶
Install openssh
:
If you also want to host an SSH server (sshd
, SSH daemon), then add it to your init system and
start it:
Depending on your runit
implementation, either run:
Config¶
- Create an
ssh
key pair:
When creating the key pair, if asked to enter a passphrase: no passphrase might be just fine (e.g. for git use). In this case you can instead use this command:
-
Add
ssh
key pair: -
Check
ssh
key pair:
TODO: motd
: https://www.kali-linux.fr/astuces/comment-securiser-son-serveur-ssh
Use¶
Prerequisite(s)
Make sure sshd
is running on the remote computer, e.g. on SystemD
based distro:
sshd
is not active and running, then start the sshd
service, e.g. on SystemD
based
distro:
-
Basic, password based,
ssh
connection (e.g. to remote user with 123.1.2.3 IP address): -
RSA (key pair) based
ssh
connection:-
Copy and authorize the public key to the remote user (e.g. with 123.1.2.3 IP address):
If you have to manually copy the public key, see https://web.archive.org/web/20220223140333/https://linuxhandbook.com/add-ssh-public-key-to-server/
-
Then configure
ssh
to use yourssh_key_name
, e.g. with 123.1.2.3: -
Finally, you should be able to connect to
123.1.2.3
without being asked for a password: -
If you still have to enter a password, make sure the target
sshd
server is well configured, e.g. check the/etc/ssh/sshd_config
file and make sure the following options are configured appropriately:PubkeyAuthentication
should not be set tono
(default isyes
)ChallengeResponseAuthentication
should be set tono
AuthorizedKeysFile
should be set to.ssh/authorized_keys
- if
StrictModes
is set toyes
(which is a sane default), then please make sure that the target$HOME/.ssh
folder has the following permissions:drwx------
(if not, run$ chmod 700 $HOME/.ssh
), and please make sure that the target$HOME/.ssh/authorized_keys
file has the following permissions:-rw-------
(if not, run$ chmod 600 $HOME/.ssh/authorized_keys
)
-
-
Test
ssh
key: -
Copy a file trough
ssh
on a specific port (e.g. port 2222): -
Copy a directory trough
ssh
: -
Avoid
ssh
freeze after inactivity (see https://serverfault.com/questions/575112/why-do-my-ssh-sessions-freeze-after-some-time): -
Allow SSH agent forwarding in order to use your private and local SSH key remotely without worrying about leaving confidential data on the server you’re working with. See https://web.archive.org/web/20221214073944/https://www.howtogeek.com/devops/what-is-ssh-agent-forwarding-and-how-do-you-use-it/ :
ssh-agent¶
Reference(s)
TODO
ssh
tunneling¶
Reference(s)
Local forwarding¶
Reference(s)
-
Forward remote port
42001
, of10.10.0.1
, on your local port10042
: -
Forward remote port
42001
, of10.10.0.1
, on your local port10042
, through multiple intermediate machines (multiple "hops"): -
Forward remote ports
42001
and42002
(of10.10.0.1
) on your local ports10042
and20042
, through multiple intermediate machines (multiple "hops"): -
Forward remote ports
42001
and42002
(of192.168.0.1
, seen by10.10.0.1
on another network interface) on your local ports10042
and20042
, through multiple intermediate machines (multiple "hops"): -
Forward remote port
42001
, of10.10.0.1
, on your local port10042
(without allowing to execute remote command-N
, and allowing to bind your local port10042
to any available interface - not justlocalhost
the loopback interface):
Remote forwarding¶
Reference(s)
- TODO
Dynamic forwarding¶
Reference(s)
- TODO
Security considerations¶
-
Use SSH key pairs whenever possible to authenticate.
-
Set up a fail2ban (or similar alternative) service.
-
Set up a firewall service, excluding all unused open ports.
-
Lock SSH access for the
root
user. -
Optionally don't use port 22 for SSH.
-
When using
rsync
through SSH, consider using restrictedrsync
.
Troubleshooting¶
OpenSSH RSA SHA-1 signatures¶
When connecting to a SSH server, you might get a similar error:
Unable to negotiate with 123.1.2.3 port 22: no matching host key type found. Their offer: ssh-rsa,ssh-dss
As of version 8.8, OpenSSH disables RSA signatures using the SHA-1 hash algorithm by default. This change affects both the client and server components.
After upgrading to this version, you may have trouble connecting to older SSH servers that do not
support the newer RSA/SHA-256/SHA-512 signatures. Support for these signatures was added in OpenSSH
7.2
.
As well, you may have trouble using older SSH clients to connect to a server running OpenSSH 8.8 or higher. Some older clients do not automatically utilize the newer hashes. For example, PuTTY before version 0.75 is affected.
To resolve these problems, you can upgrade your SSH client/server wherever possible. If this is not feasible, support for the SHA-1 hashes may be re-enabled using the following config options:
Or using the ssh
command with the following options:
See https://www.gentoo.org/support/news-items/2021-10-08-openssh-rsa-sha1.html.
Misc¶
- Play tron with
ssh
If this cheat sheet has been useful to you, then please consider leaving a star here.