Site icon C1CTech

How to set up SSH on Github

<p>GitHub offers a convenient SSH key feature for accessing your repositories on the command line&period; Instead of using usernames and passwords every time you push to or pull from your repository&comma; you can use your ssh key&period;  In this article&comma; we will talk about how you can set up SSH on GitHub step by step&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">About SSH<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">SSH<&sol;span><&sol;strong>&comma; or Secure Shell&comma; is a protocol that allows users to control and modify their remote servers over the Internet&period; Using SSH protocol&comma; you can connect and authenticate to remote servers and services&period; With SSH key&comma; you can connect to GitHub without supplying your username or password at each visit&period; Using an SSH key is a convenient and secure way to access your account&period; It’s convenient because you don’t need to remember a long password&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Why we need SSH key&quest;<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>As you know&comma; if you use git and want to clone anything from remote repositories&comma; you have to choose one of these 2 ways&colon; <span style&equals;"color&colon; &num;008000&semi;"><strong>HTTPS<&sol;strong><&sol;span> or <span style&equals;"color&colon; &num;008000&semi;"><strong>SSH<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;" size-full wp-image-1154 alignleft" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;clone&lowbar;ssh&period;png" alt&equals;"clone&lowbar;ssh" width&equals;"517" height&equals;"257" &sol;><img class&equals;"alignnone size-full wp-image-1153" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;clone-https&period;png" alt&equals;"clone https" width&equals;"505" height&equals;"261" &sol;><&sol;p>&NewLine;<p>If you use <span style&equals;"color&colon; &num;008000&semi;"><strong>HTTPS<&sol;strong><&sol;span> way&comma; you have to type your account&&num;8217&semi;s access &lpar;username or password&rpar; every time you communicate with remote repository&period; On the other hand&comma; we have <span style&equals;"color&colon; &num;008000&semi;"><strong>SSH<&sol;strong><&sol;span> authentication method&period; This method uses SSH key pair using which you can connect to GitHub without supplying your username or password at each visit that you do while using <span style&equals;"color&colon; &num;008000&semi;"><strong>HTTPS<&sol;strong><&sol;span>&period; Each SSH key pair consists of a private key and a corresponding public key&period; When you use SSH key for git&comma; you say git that this PC is authenticated for that github&&num;8217&semi;s account&comma; and never ask me again about any access&comma; because I have already given you my SSH key &lpar;for this pc&rpar;&period;<&sol;p>&NewLine;<p>SSH keys are stored in the <span style&equals;"color&colon; &num;008000&semi;"><strong>~&sol;&period;ssh<&sol;strong><&sol;span> folder&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1155" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;ssh&period;png" alt&equals;"ssh" width&equals;"645" height&equals;"178" &sol;><&sol;p>&NewLine;<p>As you can see above the &period;<span style&equals;"color&colon; &num;008000&semi;"><strong>ssh<&sol;strong><&sol;span> folder consist of two files <span style&equals;"color&colon; &num;008000&semi;"><strong>id&lowbar;rsa<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>id&lowbar;rsa&period;pub<&sol;strong> &period;<&sol;span><&sol;p>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">&period;pub<&sol;span><&sol;strong> file contains the public key&comma; while the other file contains the private key which is only for private use&period; <b><span style&equals;"color&colon; &num;0000ff&semi;">Never ever share your private key with anyone<&sol;span>&period; <&sol;b>If you lose the private key&comma; you’ll have to regenerate a new private&sol;public key pair&comma; as the authentication cannot be successfully completed without the private key part&period;<&sol;p>&NewLine;<p>Now let&&num;8217&semi;s talk about how to setup SSH on Github step by step&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Step 1 &&num;8211&semi; Generating a new SSH key <&sol;span><&sol;strong><&sol;h3>&NewLine;<h5 class&equals;"border-bottom-0"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Checking for existing SSH keys<&sol;span><&sol;strong><&sol;h5>&NewLine;<p>If you don&&num;8217&semi;t already have an SSH key&comma; you must generate a new SSH key&period; If you&&num;8217&semi;re unsure whether you already have an SSH key&comma; check for existing keys&period;<&sol;p>&NewLine;<p>If you want to check whether you have existing ssh key or not &colon;<&sol;p>&NewLine;<ol>&NewLine;<li>Open <span class&equals;"platform-windows">Git Bash<&sol;span>&period;<&sol;li>&NewLine;<li>Enter  <strong><span style&equals;"color&colon; &num;0000ff&semi;">ls -al ~&sol;&period;ssh<&sol;span><&sol;strong>  to see if existing SSH keys are present&colon;<&sol;li>&NewLine;<&sol;ol>&NewLine;<pre><code><strong>&dollar; ls -al ~&sol;&period;ssh<&sol;strong><&sol;code> <code><strong><span style&equals;"color&colon; &num;008000&semi;">&num; Lists the files in your &period;ssh directory&comma; if they exist<&sol;span><&sol;strong><&sol;code><&sol;pre>&NewLine;<p>3&period;Check the directory listing to see if you already have a public SSH key&period; By default&comma; the filenames of the public keys are one of the following ie&period; <em style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;">id&lowbar;dsa&period;pub&comma; <&sol;em><em style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;">id&lowbar;ecdsa&period;pub&comma; id&lowbar;ed25519&period;pub&comma; <&sol;em><em style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;">id&lowbar;rsa&period;pub&period;<&sol;em><&sol;p>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Generating a new SSH key<&sol;span><&sol;strong><&sol;h5>&NewLine;<p style&equals;"background&colon; white&semi; margin&colon; 0cm 0cm 12&period;0pt 0cm&semi;">After you&&num;8217&semi;ve checked for existing SSH keys&comma; you can generate a new SSH key to use for authentication&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ol>&NewLine;<li>Open Git Bash&period;<&sol;li>&NewLine;<li>Paste the text below and substitute your GitHub email address&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<pre><strong>&dollar; ssh-keygen -t rsa -b 4096 -C "your&lowbar;email&commat;example&period;com"&NewLine;<span class&equals;"hljs-comment" style&equals;"color&colon; &num;008000&semi;">&num; Creates a new ssh key <&sol;span>&NewLine;<span class&equals;"hljs-comment" style&equals;"color&colon; &num;008000&semi;">&num; Generating public&sol;private rsa key pair&period;<&sol;span>&NewLine;<&sol;strong><&sol;pre>&NewLine;<p>This creates a new ssh key&comma; using the provided email as a label&period;<&sol;p>&NewLine;<p><img class&equals;" size-full wp-image-1146 alignleft" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;ssh5-4126024683-1563285334212&period;png" alt&equals;"ssh5" width&equals;"722" height&equals;"153" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>3&period;It will ask you &&num;8220&semi;<strong>Enter a file in which to save the key<&sol;strong>&&num;8220&semi;&comma; press Enter&period; This accepts the default file location&period;<&sol;p>&NewLine;<p><img class&equals;" size-full wp-image-1147 alignleft" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;ssh6-1827107409-1563285297647&period;png" alt&equals;"ssh6" width&equals;"722" height&equals;"178" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>4&period;Type a secure passphrase and then enter the same passphrase again&period; Then it will show you the saved Identification and public key location and also the key fingerprint and key&&num;8217&semi;s randomart image&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Step 2 &&num;8211&semi; Adding your SSH key to the ssh-agent<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>If you don&&num;8217&semi;t want to reenter your passphrase every time you use your SSH key &comma; you can <span style&equals;"color&colon; &num;008000&semi;"><strong>add your key to ssh-agent<&sol;strong><&sol;span>&comma; which manages your SSH keys and remembers your passphrase&period;<&sol;p>&NewLine;<p>Now after generating a new SSH key&comma; we will add a new SSH key to the ssh-agent to manage the key&period;<&sol;p>&NewLine;<p>1&period;Ensure the ssh-agent is running&colon;<&sol;p>&NewLine;<p>To start the ssh-agent manually write the below command &colon;<&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">&num; start the ssh-agent in the background<&sol;span><&sol;strong>&NewLine;&NewLine;<strong>&dollar; eval &dollar;&lpar;ssh-agent -s&rpar;<&sol;strong><&sol;pre>&NewLine;<p><img class&equals;" size-full wp-image-1148 alignnone" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;ssh3-479037150-1563285144513&period;png" alt&equals;"ssh3" width&equals;"720" height&equals;"80" &sol;><&sol;p>&NewLine;<p>2&period;Add your SSH private key to the ssh-agent&period; If you created your key with a different name&comma; or if you are adding an existing key that has a different name&comma; replace <strong><span style&equals;"color&colon; &num;008000&semi;"><em>id&lowbar;rsa<&sol;em><&sol;span><&sol;strong> in the command with the name of your private key file&period;<&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong>&dollar; ssh-add ~&sol;&period;ssh&sol;id&lowbar;rsa<&sol;strong><&sol;pre>&NewLine;<p><img class&equals;" wp-image-1149 alignleft" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;ssh3-1-726506019-1563285256272&period;png" alt&equals;"ssh3" width&equals;"721" height&equals;"82" &sol;><&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<h3><&sol;h3>&NewLine;<h3><&sol;h3>&NewLine;<h3><&sol;h3>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Step 3 &&num;8211&semi; Adding a new SSH key to your Github account<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>To configure your GitHub account to use your new &lpar;or existing&rpar; SSH key&comma; you&&num;8217&semi;ll also need to add it to your GitHub account&period;<&sol;p>&NewLine;<ul>&NewLine;<li> Copy the SSH key to your clipboard by writing the below command &colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong>&dollar; clip &lt&semi; ~&sol;&period;ssh&sol;id&lowbar;rsa&period;pub<&sol;strong>&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&num; Copies the contents of the id&lowbar;rsa&period;pub file to your clipboard<&sol;span><&sol;strong><&sol;pre>&NewLine;<ul>&NewLine;<li> Log In to your Github account&period; In the upper-right corner of any page&comma; click your profile photo&comma; then click <span style&equals;"color&colon; &num;008000&semi;"><strong>Settings<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><img class&equals;"alignnone size-full wp-image-1142" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;settings&lowbar;github-1824820123-1563280129876&period;png" alt&equals;"settings&lowbar;github" width&equals;"243" height&equals;"510" &sol;><&sol;p>&NewLine;<ul>&NewLine;<li> In the user settings sidebar&comma; click <span style&equals;"color&colon; &num;008000&semi;"><strong>SSH and GPG keys<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><img class&equals;"alignnone size-full wp-image-1143" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;GitHub&lowbar;sshkey&lowbar;add-1158996605-1563286857128&period;png" alt&equals;"GitHub&lowbar;sshkey&lowbar;add" width&equals;"277" height&equals;"239" &sol;><&sol;p>&NewLine;<ul>&NewLine;<li> Click <span style&equals;"color&colon; &num;008000&semi;"><strong>New SSH key <&sol;strong><&sol;span>and then you can see something like this as shown below &colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><img class&equals;" size-full wp-image-1144 alignnone" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;07&sol;ssh4&period;png" alt&equals;"ssh4" width&equals;"968" height&equals;"537" &sol;><&sol;p>&NewLine;<ul>&NewLine;<li> In the &&num;8220&semi;Title&&num;8221&semi; field&comma; add a descriptive label for the new key&period;<&sol;li>&NewLine;<li> Paste your key into the &&num;8220&semi;Key&&num;8221&semi; field&period;<&sol;li>&NewLine;<li> Click <span style&equals;"color&colon; &num;008000&semi;"><strong>Add SSH key<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li> Finally&comma; it will ask you the confirm GitHub password to continue&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Thank you&comma; I hope this tutorial will help you in understanding how to setup SSH on Github&period;&NewLine;

Exit mobile version