SSH basics
SSH
- Secure remote connection to another device.
- Remember to open port 22 on the firewall so it can work (table, TCP).
Enabling
On most modern Linux and Unix based operating systems SSH is already enabled by default
Is it installed? Look for
openssh*
(architecture dependant), andopenssh-server
.1
sudo yum list openssh*
Install it.
1
2
3
4
5sudo yum install openssh-server
# start in system V
sudo service sshd start
# start in systemd
sudo systemctl start sshdRun it on systen boot.
1
sudo chkconfig sshd on
Connect (and you will see the certificate, you can save the fingerprint)
1
ssh username@123.123.123.123
Using certificates
Autheticating with a certificate instead of a password. Can not be keylogged as it is not typed.
Generate private and public key
Public key is shared, private key is “for us”.
- Set up on client
- Generate certificate, RSA type (in your user profile in there ll be an
.ssh
hidden folder, with a fileid_rsa
, you may use a passphrase).1
ssh-keygen -t rsa
- Go to
.ssh
folder: checkid_rsa
private,id_rsa.pub
. - (Optional) send it to a server.
1
ssh-copy-id -i /path/to/key.pub SERVERNAME
- Generate certificate, RSA type (in your user profile in there ll be an
Set up on server
Set up on server (sane as
ssh-copy-id
)- Check if we have the
.ssh folder
on userspace.1
2
3ls -la
# if not found
mkdir .ssh - Create file for authorized connections
1
2
3cd .ssh
# if not found
vi /authorized_keys - Type one key per line on the
/authorized_keys
files.
- Check if we have the
Test it works in the server (using the private key, always do that if you can not access physically to the server to fix it if it goes wrong).
1
ssh -i ./id_rsa username@123.123.123.123
Disable access via password.
- Access the SSH configuration file
1
sudo vi /etc/ssh/sshd_config
- Find
#PubkeyAuthentication
, remove the#
- Check
AuthorizedKeysForFile
value, so the file matches. - Check line is
PasswordAuthentication
, so its value isno
. ChallengeResponseAuthentication
should beno
- Save and restart the service on the server (
sudo systemctl restart shhd
).
- Access the SSH configuration file
Connect
- Even if we reference it, we do not send the private key to the server: we decrypt the answer from the server, which was encoded with the public key, and send the result back. Traffic only moves the public key.
1
ssh -i ./id_rsa username@123.123.123.123
Hardening
Check at least is version 2
- Go to the server, and check the config file.
1
sudo vi /etc/ssh/sshd_configfiles
- Look for “the default requires explicit activation of protocol 1” If you see
Protocol 1
, this is bad (You may also findProtocol 2,1
meaning it accepts both, been tried in that order).
Filer users allowed
- Go to the server, and check the config file.
1
sudo vi /etc/ssh/sshd_configfiles
- Remove root user: look for
PermitRootLogin
and ensure it is either commented out or set asno
. - Review the allowed users list: look for the
AllowedUsers
andAuthorizedKeyCommands
(a list example would be:AllowUsers user1 user2 user3
). - Change the port number: look for
Port
and change it (e.g.Port 9999
, and review it is open in your firewall viasudo firewall-cmd --add-port 9999/tcp --permanent
). - Restarts the server (
systemctl restart sshd
). - Connect from client specifying the port (
ssh -i ./id_rsa username@123.123.123.123 -p 9999
).