{"id":369,"date":"2021-05-03T18:57:46","date_gmt":"2021-05-03T13:12:46","guid":{"rendered":"https:\/\/nil.pro.np\/?p=369"},"modified":"2021-11-13T10:29:02","modified_gmt":"2021-11-13T04:44:02","slug":"linux-ssh-key-guide","status":"publish","type":"post","link":"https:\/\/nil.pro.np\/?p=369","title":{"rendered":"Linux SSH Guide &#8211;  SSH Key Management, Agent Forwarding"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The SSH is a cryptographic network protocol for operating network services securely over an unsecured network. It stands for Secure Shell. Mainly used to remote login, secure file transfer (sFTP).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to generate a new SSH key in Linux?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Open Terminal.<\/li><li>Enter the below command. (RSA Key)<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen -t rsa -b 4096 -C \"youremail@gmail.com\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here this email part is just a comment to label key, you can replace email with your email address or any text you like. <br>This generates a 4096-bit RSA key, we can use 2048 also.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Using ED25519 key (This is preferred over RSA key)<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen -t ed25519 -C \"youremail@gmail.com\"<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Now, enter a location to save ssh key file on your computer. Press <kbd>Enter<\/kbd> to save in the default location.<\/li><li>Enter passphrase. It is used to encrypt your SSH key.<br>This is a password that will be asked every time when you use this key. Press <kbd>Enter<\/kbd> not to have any password in the SSH key.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>Note<\/strong>: ED25519 is more robust and secure than the RSA key, so I recommend using ED25519. Also, it&#8217;s a good idea to protect your SSH key with a password so that hackers can&#8217;t access your server immediately even after they get access to your private key. <\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to update passphrase (password in SSH key)?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Enter the following command in the terminal to change the password, it will ask your old password before giving the option to set a new one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen -p -f \/path\/to\/ssh_key<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to check already generated SSH keys?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Open Terminal and, enter the below command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -al ~\/.ssh<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This list the all file inside the .ssh folder in the user home (default location).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to connect to the server using SSH key?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Copy your public key and paste it on ~\/.ssh\/authorized_keys file in the remote server.<ul><li> This can be done manually or<\/li><li>We can directly add our key to ~\/.ssh\/authorized_keys file of the remote server. Enter the below command local computer terminal.. <\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-copy-id -i ~\/path\/to\/privatekey user@remote-server-ip<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here your public key should be in the same directory with the name privatekey name and .pub at the end.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Now, Enter the below command in your local computer terminal to connect to your remote server with an SSH connection.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -i \/path\/to\/your\/privatekey user@remote-server-ip<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use your appropriate private key name\/path, remote server user name, and server IP.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note: if you have your ssh private\/public key in the default location then you don&#8217;t need -i flag and path to the key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, If your computer doesn&#8217;t have SSH (password-based) already enabled, you may need to <a href=\"https:\/\/nil.pro.np\/install-firewalld-centos\/\" class=\"rank-math-link\">add a firewall rule to allow SSH connection<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"add-ssh-key-to-ssh-agent\">How to add our SSH key to ssh-agent (SSH authentication agent)?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ssh-agent is a key manager for SSH. ssh-agent holds our keys and certificates in memory, unencrypted and ready for use by ssh in the background. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, we only need to enter passphrase once. Otherwise, we will need to retype the passphrase on each connection.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First,  start the ssh-agent in the background.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>eval \"$(ssh-agent -s)\"<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Add the SSH private key to the ssh-agent.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-add ~\/.ssh\/id_rsa<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This ssh-agent and ssh-add command allow the user to use any number of servers, spread across any number of organizations, without having to type in a password every time when moving between servers. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is commonly used by system administrators to move among the machine they administer.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To check ssh key added into ssh-agent<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-add -L<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to Fix the SSH Error &#8220;sign_and_send_pubkey: signing failed for ED25519 &#8220;.ssh\/id_ed25519&#8243; from agent: agent refused operation Permission denied (publickey).&#8221;?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is a common error we face when we copy the SSH key from another computer and try to connect with the server. This is most of the due to the permission of the SSH key file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I face this annoying problem when I change my system from Ubuntu to Pop!_OS. I spent almost a day troubleshooting this issue.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Error Message: &#8220;sign_and_send_pubkey: signing failed for ED25519 &#8220;.ssh\/id_ed25519&#8243; from agent: agent refused operation Permission denied (publickey).&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you copy a file in the system, the file loses its permission and is wide open to everyone. SSH key being a sensitive door to system, Linux tries to protect the user from unauthorized use of SSH key. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To check it, manually <a href=\"#add-ssh-key-to-ssh-agent\" class=\"rank-math-link\">add the SSH key to ssh-agent<\/a>. It will show <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8220;Permissions 0777 for &#8216;\/ssh\/id_ed25519&#8217; are too open.<br>It is required that your private key files are NOT accessible by others.<br>This private key will be ignored.&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Solution: To solve this issue change the permission of the SSH key file so that only the key owner can read\/write.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd to\/key\/location #Go to SSH key file location\nsudo chmod 400 id_ed25519 id_ed25519.pub # To make it read only\n\/\/or \nsudo chmod 600 id_ed25519 id_ed25519.pub # To make it read\/write only <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Actually, it turns out to be a security feature of SSH.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SSH Agent Forwarding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SSH agent forwarding is a mechanism whereby an SSH client allows an SSH server to use the local ssh-agent on the server, the user logs into as if it was local there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In simple terms, SSH agent forwarding allows us to use our local SSH keys on a remote server\/host without copying or moving them to a remote server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a scenario, when you are working on a remote server, and you want to git pull code that you&#8217;re storing on Github, Gitlab. And, you want to use SSH authentication for Github\/Gitlab, but your private keys are on your local machine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One method is copying private keys from the local machine to the server and connecting to GitHub. If we need to configure multiple machines then we need to repeat the whole process in each machine very tedious. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, it&#8217;s dangerous to store our personal private keys in the remote server, anyone who has access to our remote server [ if anyone\/hacker grants access] can retrieve our private keys and can access our other host\/machines as well.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To solve this problem, we use SSH agent forwarding in our local machine, then we can directly connect to GitHub from the server like we are doing from the local machine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How does SSH Agent Forwarding work?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To explain, SSH agent forwarding process here I will take the same example of connecting to Github from a remote server.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/nil.pro.np\/wp-content\/uploads\/2021\/05\/Block-Diagram-of-SSH-Agent-Forwarding.jpg\" alt=\"Block Diagram of SSH Agent Forwarding\" class=\"wp-image-385\"\/><figcaption>Block Diagram of SSH Agent Forwarding<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"> When you try to pull code from Github in a remote server with SSH authentication, Github will ask the remote server for authentication verification. In normal cases, the SSH agent responds with key verification using the private key, but in the case of SSH forwarding, it will forward this request to the local computer [SSH client that originally contacted the server] then the SSH Agent of the local computer responds with verification message. When Github gets the message, it verifies the request and lets us pull the code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps to establish SSH Agent Forwarding<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We can establish SSH Agent Forwarding in two ways:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Method 1:<\/strong> Editing ssh config file. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add below code in ~\/.ssh\/config file in local machine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Host ServerIP\n  ForwardAgent yes<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, our SSH agent forwarding will work next time in a normal ssh connection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Method 2<\/strong>: Passing A flag in the request. This method is useful if you only need to forward the SSH agent for one time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add -A flag when doing SSH to the server like below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -A user@ServerIP<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>To test the connection from a remote server<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#for checking ssh connection with Github from remote server, type this in remote server terminal.\nssh -T git@github.com<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also Read: <\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/nil.pro.np\/free-blogging-tutorials-for-beginner\/\" target=\"_blank\">Blogging Tutorials Series for Beginner [Free]<\/a><\/li><li><a href=\"https:\/\/nil.pro.np\/css-flexbox-guide\/\">CSS Guide: Flexbox Cheatsheet<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/nil.pro.np\/set-up-cron-job-linux\/\" target=\"_blank\">How to set up a cron job in Linux?<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">If you have any confusion following this tutorial or any topics on this <a href=\"https:\/\/nil.pro.np\/\">Nil blog,<\/a> don\u2019t hesitate to ask in the comment section. You can also reach me on <a rel=\"noreferrer noopener\" href=\"http:\/\/twitter.com\/knilkantha\/\" target=\"_blank\"><strong>Twitter<\/strong><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SSH is a cryptographic network protocol for operating network services securely over an unsecured network. It stands for Secure Shell. Mainly used to remote login, secure file transfer (sFTP). How to generate a new SSH key in Linux? Open Terminal. Enter the below command. (RSA Key) Here this email part is just a comment&hellip;&nbsp;<a href=\"https:\/\/nil.pro.np\/?p=369\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Linux SSH Guide &#8211;  SSH Key Management, Agent Forwarding<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[4,15],"tags":[],"class_list":["post-369","post","type-post","status-publish","format-standard","hentry","category-how-to","category-linux"],"_links":{"self":[{"href":"https:\/\/nil.pro.np\/index.php?rest_route=\/wp\/v2\/posts\/369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nil.pro.np\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nil.pro.np\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nil.pro.np\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nil.pro.np\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=369"}],"version-history":[{"count":24,"href":"https:\/\/nil.pro.np\/index.php?rest_route=\/wp\/v2\/posts\/369\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/nil.pro.np\/index.php?rest_route=\/wp\/v2\/posts\/369\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/nil.pro.np\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nil.pro.np\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nil.pro.np\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}