{"id":423,"date":"2020-10-15T01:31:06","date_gmt":"2020-10-15T01:31:06","guid":{"rendered":"https:\/\/dft.wiki\/?p=423"},"modified":"2025-11-20T18:06:00","modified_gmt":"2025-11-20T23:06:00","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 a key to have access to your server through ssh is the safest way to get access to your server.<\/p>\n<p>In your client machine just type:<\/p>\n<pre>ssh-keygen -t rsa-sha2-512 -b 4096 -C \"user@domain.com\"<\/pre>\n<p>Or, for an elliptic curve signing algorithm alternative:<\/p>\n<pre>ssh-keygen -t ed25519 -C \"user@domain.com\"<\/pre>\n<p>It is going to ask you the location, just hit &#8220;Enter&#8221;, and if you want a password just type, confirm, and the key is created.<\/p>\n<p>When needed to change the password of the private key issue:<\/p>\n<pre>ssh-keygen -p -f ~\/.ssh\/id_dsa<\/pre>\n<p>Or simply:<\/p>\n<pre>ssh-keygen -p<\/pre>\n<p>For manually extract the public key from the private:<\/p>\n<pre>ssh-keygen -y -f ~\/.ssh\/id_rsa &gt; ~\/.ssh\/id_rsa.pub<\/pre>\n<p>The whole 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 issue the command:<\/p>\n<pre>ssh-copy-id user@domain.com<\/pre>\n<p>Confirm the password that you used to type to log in to your server.<\/p>\n<p>The public key can be manually installed by appending the <strong>id_rsa.pub<\/strong> into the <strong>authorized_keys<\/strong>.<\/p>\n<pre>cat ~\/.ssh\/id_rsa.pub &gt;&gt; ~\/.ssh\/authorized_keys<\/pre>\n<p>To check the algorithm type of an existent key:<\/p>\n<pre>ssh-keygen -l -f ~\/.ssh\/id_rsa<\/pre>\n<p><strong>Done!<\/strong> Now just try to connect again.<\/p>\n<pre>ssh domain.com<\/pre>\n<p>If you did everything correctly you are already logged in.<\/p>\n<p>It is always a good idea to have another account set just in case you type something wrong and lock yourself out. If this is the case, log in with the second account, switch to your user, or root, and delete the files inside the folder ~\/.ssh\/.<\/p>\n<p>As a good practice, always protect your SSH as much as you can. See the recommendations below:<\/p>\n<pre>sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Configuration 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 user id.\u00a0Restart your server:<\/p>\n<pre>sudo systemctl restart sshd.service<\/pre>\n<p>Consider using an <strong>SSHFP<\/strong> (SSH FingerPrint<span style=\"font-size: 1rem;\">) record in your <strong>DNS<\/strong> zone. It will require the following information.<\/span><\/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\u00a0<\/strong><strong>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 issue the following command against the Public key with the domain\/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 offer a few options (one per line), where the highlighted numbers are <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>, 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 on a server that is the entry point of a network to reach one internal server, use the ProxyJump functionality:<\/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 process:<\/p>\n<pre>nano ~\/.ssh\/config<\/pre>\n<p>With the following configuration customized 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 configured 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>Or to bypass any pre-configuration and only give the arguments of the command:<\/p>\n<pre>ssh -F \/dev\/null user@host<\/pre>\n<p>Don&#8217;t forget to 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 hop-on and off configured servers.<\/p>\n<hr \/>\n<p><strong>READ MORE<\/strong><\/p>\n<ul>\n<li>Discover new functionalities over SSH on the post <strong>Reverse Shell with AutoSSH<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=1462\">Link<\/a>].<\/li>\n<li>Highlight bad practices and smells with <strong>SSH Audit Server and Client<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=2214\">Link<\/a>].<\/li>\n<li>Apply defence in layers by <strong>Using Port Knocking to Secure SSH<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=2303\">Link<\/a>].<\/li>\n<li>You can 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 a key to have access to your server through ssh is the safest way [&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":19,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/423\/revisions"}],"predecessor-version":[{"id":5173,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/423\/revisions\/5173"}],"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}]}}