Authenticating on multiple GitHub accounts using SSH

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings friends welcome today we're talking about ssh keys and using them to authenticate on github and in particular how we can do this to authenticate to multiple github accounts all right let's jump in um if you've got git installed this is our window it's going to be an awesome windows uh you should have a git bash prompt uh like so and so i'm going to use that to create our keys just make this a bit bigger uh so by default this puts us into the users folder and you should be able to move well if you yeah you should be able to find a dot ssh folder in here and that should be blank because there's nothing in there at the moment we're going to create our first key in here and to do that we're going to use ssh key gen i'm not going to give any parameters for this first one when we create our second key we'll have to but for this we can just take all the defaults it's going to put it into the current folder so we can accept that and it's going to create an id rsa file so that's fine you can add a passphrase on here as well that's fine for extra security we're not going to do that today so let's have a look in there it's created two files for us idrsa which is our private key and the public key version which we're gonna go and put on to github now so we need to grab the contents of the public one so let's do id r s a dot pub right so and we can just grab this entire um key here so we'll copy that right i'll come into um into github let me just do that right okay so if we open up github and you come over to your avatar and then click settings then you should also be able to go down to ssh keys here and what we can do now is add a new ssh key so we're going to do that and we kind of want to name this um related to the computer on you know you might have you might want to do this from your main computer from a laptop and from another device so you kind of want the name of that so we'll call it a main pc in there and that should be okay and we'll just paste that that key in and say add ssh key all right that's added in now when we try and connect using this key we need to tell ssh where to get these keys from uh so there's two ways to do that you can add them to ssh agent or you can set up a config file um now to use multiple keys we need the config file so that's what we're going to use here so i'm going to create create a new config file in this dot ssh folder and we can set it up like this so we're going to say account one so a hash in here is just a comment line and then we need to specify the host now this is what we're going to use to distinguish between the two accounts so this is going to change each time by default um it'll be something like git github.com so we use that for the first one the default's fine and we need to specify because this you can use ssh to connect to various services we need to specify our host as github in this case so we need to say github.com and we need to say for this account which key we're going to be using so we need to have an identity um file and we want to specify our current folder so we can go to the root and then into dot ssh and our file is id rsa and that's good that should be set up fine for this single account now so we'll save that one off and let's try and connect now using that key so i'm going to be connecting to my marc james howard account right so i'll open up another command line and let's create uh that's credit directory mark james howard okay now let's try and pull in a repository from that account so we do a git clone now when we're using ssh let's go and look at the repository we've got a test repository here that we're going to use and you if you go to the code file you can switch between uh using https which gives you that or using ssh which gives you this um this identifier so we're going to copy this one and come back i'm going to get we're going to get clone you notice at the start here this is the string i've got that string wrong because this the host that we want to specify is the bit here this is the user and this is the host and that needs to match what's in our config file so let's go back into the config and for the host we're just going to say github.com as well and now we should be able to clone this okay so that's fine all right that's good and we have a look in here so we can move into there and we've just got the single file in here text and what we'll do now is let's just edit that file just to show that we can um push this up successfully so we're just making a change and then we're going to push that up all right so that's good we are connecting with ssh there straight away but only for one account but because we're using the config file we're in a good position now to have two accounts so so let's do that let's create um another key because you can't use the same key on different uh github accounts um let's go to the account we're gonna we're gonna try and work with now so if i sign out of this one and we'll go to um a shot of code and we'll sign in there and if i went to settings again and ssh and then if i try to add our existing key so id rsa dot pub and grab that again so if i try here and say searched and paste that in it will tell me key is already in use so it's checked across the whole of github and that ssh key has got to be unique per account so we can't add that we need a new key right let's create a new one so again we need to do ssh key gen now this time we need to provide a file name because we can't use that default filename it will overwrite our id underscore rsa file so we need to say and this one's going to be a shot of code so we'll do a shot of code underscore id underscore rsa and again we won't enter a passphrase it didn't ask us to file because we provided it but now we can see in here we got an extra two files um we've got a shot of code and we've also got id rsa now this config file needs to be not not a dot txt so let me let me move to just config which means on our last one it wasn't picking it wasn't picking from there i think it was just using the the the default because this is the correct file name i don't think that was using the config before but we'll check again now um right we need to add this new key now to our config so let's let's go into the config again and we're going to say account 2 and the host now the host is what we're going to use to distinguish genomes so this needs to change so we're going to have a shot of code um actually now we're going to do github github.com dash a shot of code and the others are going to be similar so we'll have host name this needs to be github.com and the identity file and this needs to be um a shot of code uh underscore id underscore rsa that looks good so we'll save that off and let's grab a shot of code dot public key because we need to add this into github now so we'll copy this key again we'll go back to github this time it's going to let us add it because it's a new key um what did we call the last one it was main pc but this one is main pc the shot of code and we'll paste that public key in so that was good um and then let's look now at the um the repository we're going to play with here so we've got a test two repository in here and by default it's still using github.com so we're gonna have to change that to our host in order for it to pick the correct ss ssh key to use um so i'll copy that but we're gonna have to change it slightly and we'll come back into here and let's go up to the top and we'll create a deck directory a shot of code like so and we're going to do our git clearing again there so git clone and that's the string we've got but we need to change this host we need this to match what is in our config file so let me move this over here we'll bring up the config and move that over there so our host for a shot of code is this so github.com hyphen a shot of code okay hyphen a shot of code and by doing that we should now get it using the correct it will come in here it will match the host and then it will pick our correct ssh key all right so let us clone it that's good will it let us make a change so let's go in there and again we've got a single file test so let's edit that file like so and if we look now we've got a we've got a change in there so we can do a git commit like so and then push that up and it should let us add that in and that is working fine so what we've managed to do is have um two ssh keys on our machine connecting to two different accounts and the the main thing that we need that enables that is this config file that we create in the dot ssh folder so there you go ssh keys um what's you know i've done a video uh previously on using https to have multiple accounts so if you want to see that video i'll put a link to it here which is better i don't know pro yeah i don't know maybe https is probably what i'm going to stick with um but that you know once you've played around with them both a bit uh they both work fairly well i hope you enjoyed that video give me a thumbs up if you did don't forget to click subscribe for future videos and i'll catch you next time [Music] bye you
Info
Channel: A shot of code
Views: 39,544
Rating: undefined out of 5
Keywords:
Id: N2hMGEeYR7c
Channel Id: undefined
Length: 13min 53sec (833 seconds)
Published: Sun Jul 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.