#3 How To Add SSH Keys on Gitlab Account | How to create SSH Key on Mac, Linux and Windows

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i welcome all of you to our youtube channel so this is the very beginning of our gitlab series so in the previous video we learned how to create an account on gitlab.com so if you have not seen that video yet i will highly recommend go and watch the video and come back to this one so in today's video we will set up the secure shell based authentication for mac and linux in this video we will go through the steps for mac and linux machine unfortunately i do not use windows therefore i cannot show you the setup for windows users um probably i will make another video for the windows user just to show you how to set the top easily on windows i just wanted to let you know that mac setup and linux setup for ssh based authentication is exactly same i have also included the troubleshooting um kind of like session so if you have any problems with the troubleshooting or if you have any any kinds of problem with the authentication something like authentication failed while you were switching from https to ssh so watch the video till the end where i tried to show you how you could fix that problem so to do that we just need to open up a terminal here i'm using vs code for example with the command ssh hyphen keygen without passing any flags or anything so you can just enter and then it will show you where to save the file so in my case slash users slash voice raman slash dot ssh so normally is the home directory where dot ssh folder hidden folder is created and the key pair actually generates called id underscore rsa for the private key and id underscore rsa.pub is the public key so you never show your id underscore rsa which is your private key to anyone so if you show it to someone or share it to someone it means that your security is compromised so now we run the cat command to output the content of that file so we need to grab this content and copy to the clipboard and then paste it to gitlab.com so now we need to open up a browser and we need to log into gitlab.com so now we're gonna click on add ssh key and here we need to paste the public key that we have copied to our clipboard in case you have pasted the private key here by mistakes i would recommend you to generate a new keep here and then copy the public key and paste the content here so anyway so now we're gonna put the title title as you wish name it whatever you want so here we have the option also um expires add which means that you could set the expiry date for the keys and in our case we are not going to set that up so we're going to leave it empty for now so now we are all set everything looks good so you're gonna hit the button called add key so once you do that you can get kind of like a brief summary here you can see the title of the key created on which day expires never and last used we have not used the key yet and you can see the fingerprint as well so here all any keys that you add here in the future you will see the list of the keys that has been added so as you can see on the screen this is a brand new account and we have just added one key therefore we see only one title here which is gitlab underscore key once you generate the key pair by default it will have read write permission for the owner for the private key if you want to you can set it to only read because you don't want to tamper your private key so if you want to do that you can run the command chmod space 400 space then path to your private key so in as i said earlier is going to be your home directory slash dot ssh slash id underscore rsa so that's it so this is a very good practice if you want to follow since i'm on mac therefore i'm gonna fire up the docker container which is running on ubuntu 1804 so that i could show you how to set up the ssh keys on a linux machine so um now i'm inside the container and i'm gonna run ssh dash kijan and then we're gonna put the algorithm for rsa and then right after we're gonna say how many bits is going to be so 2048 i think it's by default 2048 and then we're going to leave a command so which is minus c and then write the command and by default as i said is going to be the home directory i had a key here before therefore is going to say do you want to overwrite and yes pass in yes if you want to and then you can see the location of the key pair so location of the key pair is going to be home directory slash dot ssh so we can run also the ls command followed by the location of the keys so run the cat command output the content and copy to the clipboard and now we need to get back to the gitlab ui so you're gonna do the same thing as we did for mac so you're gonna name it or for the title my linux key we're gonna leave it empty for expiry date and gonna hit the button so as you can see my linux key has been added and we never used it because it's just brand new keep here we just we have just added so now our setup is ready so it's time to test it so in linux and mac you can run the same command that you see on the screen so if you have set the tab successfully you should see the message welcome to get love currently we do not have anything in that particular repository because it's just an empty one therefore you see the message so now let's go ahead and clone the repository so we're gonna clone this time with https so we need to write git clone and then pass in the url so this is gonna prompt for username and then the password so each time you use https in your cases it's gonna ask for the username and the password when you try to clone or when you try to push stuff so as you can see we do not have anything particularly on the repository as you can see the warning you appear to have cloned the empty repository so it's because i had only readme.md file and probably a hidden gitlab gitlab.ci.eml file so now let's try to clone the same repository using ssh based authentication to do that we are gonna remove our repo from the local machine is because if you want to clone it again it will say repo already existed so we're gonna see that after some time but for now just delete the repository so now we are able actually as you can see we have tried the cloning and we are able to clone so it seems like ssh based authentication is working for us excellent so we are able to clone using password-based authentication and also ssh based authentication and now let's move on to the linux one again i'm gonna fire up a docker container because it's the quickest and easiest for me so i have a running container which is running on ubuntu 1804 so you're gonna get into that container so um as you can see i just run this command uh to get into the container so now you're in all right that's really good since it's a fresh i want to machine therefore i would not be able to see the authentication failure issues in my case but many of you might encounter the authentication failed issue once you have git cloned with https then later on you are trying to do that using ssh so in this hands-on session i will show you how to do that and as i said earlier that just delete the repository so first of all we are going to clone so therefore we need to run git clone and the url so you're gonna pass in the username and after that it's gonna ask for the passwords so we need to feed in the passwords as well so let's do that so it seems like authentication is okay and we are able to clone the repository to our linux machine so as you can see repository name is git gitlab tutorial so you're gonna see to that and there is nothing actually right now so there is only readme. nothing else there so git branch here we have master but if you create a git repository it will be main because gitlab has changed their default um branch from master to main so if you say main don't get confused it's still okay so so far on linux machine we have managed to clone our remote repository using the password-based authentication and which is awesome so you can see i have readme.md and one hidden file for um automated build which is dot gitlab.coml i think this is what you're gonna see in the latter videos of the gitlab series so we're gonna try now ssh based authentication that we have set up just a while ago so to do that i need to actually do some pre-work because since you said since it's a docker machine a lot of packages are not installed there so i wanted to install vm at least so that i could edit file and push to remote repository so um our installation of beam is completed so what i'm gonna do is that i'm gonna edit this file readme.md file and then gonna push um to remote repository nothing fancy just a simple text file i'm gonna add here and then run git status so you can see readme.md file has been modified so you're gonna add it to staging so to do that we run git add minus u so it's gonna add the updated file to the to the staging so once that is done we're gonna add a comment message um so we're gonna write troubleshooting kit lab authentication so now you see i have more problem because as i said it is a new machine nothing has been set up so i wanted to also set up my username and then the email i really hate to set global because it messes up everything on your working computer so i like to put dash just local so that's just local means my username and email is going to be applied to only this particular repository so in our case it's git git love tutorial so if you have another repository called um git for example so your username is not going to be um this one so you need to set that up unless you have something globally set so for now this is really good so i'm gonna set my username and then the email address for this particular repository so that i could um push to remote repository so now everything looks good so we're gonna push so to push as you can see this is really um repeatedly when you have password-based authentication it's gonna ask for the username and then password each time you try to do stuff so therefore um i really don't like this approach so the reason actually did not work is because i did not have the commit message added so let's add the comment message again and let's give it a message so fix the ssh authentication that's the message we put in and we're gonna see also git log that's there's pretty equal one line so you can see the comment message in one line so that looks good excellent so i think we are ready to push so again it's gonna ask for the username and then password how hectic is that can you imagine if you are pushing 50 times a day it's gonna ask for a username 50 times i think this is gonna yeah i think this is gonna drive people crazy at work to be honest so as you can see on the screen we have successfully pushed our changes to the upstream repo everything looks good and there is also automated build in this case nothing is going to happen is because my runner is actually not running so it's gonna hang for forever or i need to cancel that pipeline job the ci job anyway so uh we're gonna clone so this time we're gonna use this ssh based authentication so as you can see we have managed to uh clone git gitlab tutorial repo to our local linux machine and this seems to work and we're gonna delete it because we don't want to keep repository under repository but i just wanted to show you you could clone it and this works so what we're gonna do is that we are gonna remove this one from here and we're gonna go one directory up and now you can see already exist and it's not an empty directory so this is what i tried to talk about before so now to overcome this issue you could actually run git remote dash b you can see the up stream actually using https not the one we are using for ssh so to fix this issue as you said earlier we need to change the url so we gonna change the url so let's go and copy the url from here so we're gonna take the clone with ssh url and we need to run git space remote space set url and then we are going to run origin and then the url so git remote set url origin and then the url so now this has changed to the url from https to the newly um added one so you can see now we have git upgrade of gitlab.com so which is what should be for ssh so if you get any authentication failure or something this is what exactly you have to do you need to first check what is your upstream url if is if it is https you need to change it to um the one we just showed it to you so just for an example i'm gonna write one more line on that readme.md file and run give status just to see that we have modified this file and then you can also run git div where you can see what has been changed a readme.md file so as you can see running git div what has been actually added or updated uh to readmitted md file and now we are good so we need to run git status to see what has been actually updated and right after we can run github minus u which means that add all those updated files to the index so that should be enough for now we're gonna write the message so fixed ssh auth issues so our message is added commit message so now what we're gonna do we're gonna do the push as you can see um my key is expiring soon is because i have um set up the expiry key for that linux one so therefore you see the warning so if you do not set up the expiry keys um set up the expiry date for the keys it's not gonna show you that message so now as you have seen we have successfully um post changes with ssh based authentication since everything is working so thanks for watching and like share comment if you like my videos and see ya in the next one is the save cheers and practice a lot thank you very much
Info
Channel: Tech With Foyzur
Views: 892
Rating: undefined out of 5
Keywords: How To Set Up SSH key in Gitlab Account, Gitlab Tutorial, How to create ssh keys in gitlab, what is ssh, gitlab tutorials for beginners, gitlab ce tutorial, how to generate ssh key, how to add ssh key
Id: ZEmQ8pVPLxA
Channel Id: undefined
Length: 18min 50sec (1130 seconds)
Published: Wed Jun 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.