{"id":423,"date":"2020-10-15T01:31:06","date_gmt":"2020-10-15T01:31:06","guid":{"rendered":"https:\/\/dft.wiki\/?p=423"},"modified":"2026-06-09T14:31:48","modified_gmt":"2026-06-09T18:31:48","slug":"ssh-keys-to-access-your-server","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=423","title":{"rendered":"Setting Up and Copying SSH Keys"},"content":{"rendered":"<p>Creating an SSH key is the safest way to access your server.<\/p>\n<p>On your client machine, type:<\/p>\n<pre>ssh-keygen -t rsa-sha2-512 -b 4096 -C \"user@domain.com\"<\/pre>\n<p>Or, for an elliptic curve alternative:<\/p>\n<pre>ssh-keygen -t ed25519 -C \"user@domain.com\"<\/pre>\n<p>It will ask for a location; just hit &#8220;Enter&#8221;. If you want a passphrase, type and confirm it. The key is now created.<\/p>\n<p>To change the passphrase of a private key:<\/p>\n<pre>ssh-keygen -p -f ~\/.ssh\/id_dsa<\/pre>\n<p>Or simply:<\/p>\n<pre>ssh-keygen -p<\/pre>\n<p>To manually extract the public key from the private key:<\/p>\n<pre>ssh-keygen -y -f ~\/.ssh\/id_rsa &gt; ~\/.ssh\/id_rsa.pub<\/pre>\n<p>The entire directory must be protected from being read by other users:<\/p>\n<pre>chmod 700 -R ~\/.ssh<\/pre>\n<p>To transfer your key to the server:<\/p>\n<pre>ssh-copy-id user@domain.com<\/pre>\n<p>Enter the password you normally use to log in to your server.<\/p>\n<p>The public key can also be installed manually by appending <strong>id_rsa.pub<\/strong> to <strong>authorized_keys<\/strong>.<\/p>\n<pre>cat ~\/.ssh\/id_rsa.pub &gt;&gt; ~\/.ssh\/authorized_keys<\/pre>\n<p>To check the algorithm of an existing key:<\/p>\n<pre>ssh-keygen -l -f ~\/.ssh\/id_rsa<\/pre>\n<p><strong>Done!<\/strong> Now try connecting again.<\/p>\n<pre>ssh domain.com<\/pre>\n<p>If you did everything correctly, you are now logged in.<\/p>\n<p>It is always a good idea to have a second account ready in case you make a mistake and lock yourself out. If that happens, log in with the second account, switch to your user or root, and delete the files inside the ~\/.ssh\/ folder.<\/p>\n<p>As a good practice, protect your SSH configuration as much as possible. Edit the config file:<\/p>\n<pre>sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Parameters you should pay attention to:<\/p>\n<pre>AllowUsers <strong>user<\/strong>\r\nPermitRootLogin no\r\nPubkeyAuthentication yes\r\nPasswordAuthentication no\r\nPermitEmptyPasswords no<\/pre>\n<p>Replace &#8220;<strong>user<\/strong>&#8221; with your own username. Then restart the SSH service:<\/p>\n<pre>sudo systemctl restart sshd.service<\/pre>\n<p>Consider adding an <strong>SSHFP<\/strong> (SSH Fingerprint) record to your <strong>DNS<\/strong> zone. It requires the following information:<\/p>\n<ul>\n<li><strong>Algorithm<\/strong> (integer)\n<ul>\n<li>1: RSA<\/li>\n<li>2: DSA<\/li>\n<li>3: ECDSA<\/li>\n<li>or other.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Hash Type<\/strong> (integer)\n<ul>\n<li>1: SHA-1<\/li>\n<li>2: SHA-256<\/li>\n<li>or other.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Fingerprint<\/strong> (text)\n<ul>\n<li>Hexadecimal representation of the hash result.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>To obtain the hash and its parameters, run the following command against the public key with the domain or IP information:<\/p>\n<pre>ssh-keygen -r <strong>domain.com<\/strong> -f ~\/.ssh\/<strong>id_rsa.pub<\/strong><\/pre>\n<p>The output may show a few lines, where the highlighted numbers represent the <strong>Algorithm<\/strong> (RSA) and <strong>Hash Type<\/strong> (SHA-1 and SHA-256) respectively, followed by the <strong>Fingerprint<\/strong>.<\/p>\n<pre>domain.com IN SSHFP <strong>1 1<\/strong> <span style=\"text-decoration: underline;\"><strong>5fc287e33f114f495269480222934d2da805e634<\/strong><\/span>\r\ndomain.com IN SSHFP<strong> 1 2<\/strong> <span style=\"text-decoration: underline;\"><strong>c208d0046676861e11437931eba71c604c499ced7fd24bacd7838daa6842d633<\/strong><\/span><\/pre>\n<p>For <strong>ECDSA<\/strong>, it would look like this:<\/p>\n<pre>domain.com IN SSHFP <strong>4<\/strong> 1 e65c171139b05c47a44c869d2dffc4dfe255201e\r\ndomain.com IN SSHFP <strong>4<\/strong> 2 3f9648811a18efcdf7976a04eea49af1edb433d0ec9ac28c19d0c29d059e9c70<\/pre>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>If you need to hop through a server that is the entry point of a network to reach an internal server, use the ProxyJump option:<\/p>\n<pre>ssh -J user1@200.200.200.200 user2@10.0.0.1<\/pre>\n<p>Or create a configuration to automate this:<\/p>\n<pre>nano ~\/.ssh\/config<\/pre>\n<p>With the following configuration adjusted accordingly:<\/p>\n<pre>Host external\r\n    HostName 200.200.200.200\r\n    User user1\r\nHost internal\r\n    HostName 10.0.0.1\r\n    User user2\r\n    IdentityFile ~\/.ssh\/id_rsa\r\n    ProxyJump external<\/pre>\n<p>Many other parameters can be set in this file:<\/p>\n<pre>Host serverA\r\n    HostName 192.168.0.1\r\n    User user3\r\n    Port 2222\r\n    Protocol 2\r\n    IdentityFile ~\/.ssh\/serverA.key\r\n    LogLevel INFO\r\n    Compression yes\r\n    ServerAliveInterval 60\r\n    ServerAliveCountMax 30\r\n    ForwardAgent no\r\n    ForwardX11 no\r\n    ForwardX11Trusted yes\r\n    ProxyJump user1@10.0.0.1:22,user2@10.10.10.100:2222\r\n\r\nHost * !192.168.0.1\r\n    User ubuntu<\/pre>\n<p>To bypass any pre-configuration and pass only the command arguments directly:<\/p>\n<pre>ssh -F \/dev\/null user@host<\/pre>\n<p>Check out <strong>LazySSH<\/strong> [<a href=\"https:\/\/github.com\/Adembc\/lazyssh\">Link<\/a>]. It reads the <code>~\/.ssh\/config<\/code> file and presents a TUI for easy navigation between configured servers.<\/p>\n<hr \/>\n<p><strong>READ MORE<\/strong><\/p>\n<ul>\n<li>Discover new SSH functionalities in <strong>Reverse Shell with AutoSSH<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=1462\">Link<\/a>].<\/li>\n<li>Identify bad practices with <strong>SSH Audit Server and Client<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=2214\">Link<\/a>].<\/li>\n<li>Add defence in layers with <strong>Using Port Knocking to Secure SSH<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=2303\">Link<\/a>].<\/li>\n<li>Do not skip <strong>Hardening OpenSSH with 2FA<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=2379\">Link<\/a>].<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Creating an SSH key is the safest way to access your server. On your client [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-423","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=423"}],"version-history":[{"count":20,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/423\/revisions"}],"predecessor-version":[{"id":5824,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/423\/revisions\/5824"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}