Blog

How to create and add SSH-key to your GitHub-account

In order to access the repository on GitHub via ssh, you first need to create an account itself.

Further, after authorization, you need to go to your account settings (click on your avatar icon in the upper right corner of the screen and select settings) --> SSH and GPG keys), and then click the New SSH key button (also on the top right).

Now in these two opened fields (Title and Key) you need to insert the name of the key (any) and the code of the public part of the key itself (copy the contents of the key).

Where to get them? First of all, it makes sense to check if you have generated your key before and simply forgot about it. To do this, go to the Home folder and look for keys there in the hidden .ssh folder (you must first add the ability to view hidden files in the Navigator). Another way to find this folder is through the terminal window with the command:

$ ls -al ~/.ssh

If there are keys, then we will see at least 4 lines of text

drwx------ 2 su su 4096 сеп 24 23:24 .
drwxr-xr-x 23 su su 4096 сеп 24 23:24 ..
-rw------- 1 su su 419 сеп 24 23:24 id_ed25519
-rw-r--r-- 1 su su 104 сеп 24 23:24 id_ed25519.pub

Well, if not, then only the top two. In this case, we will have to generate the keys ourselves:

$ ssh-keygen -t ed25519 -C "my_email@example.com"

The system will inform about the beginning of the generation process and specify under what name the file with the key should be saved:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/su/.ssh/id_ed25519):

Just agree (Enter).

Next, the system will inform you about the creation of the .ssh directory and prompt you to enter a password phrase (it is not necessary to enter):

Created directory '/home/su/.ssh'.
Enter passphrase (empty for no passphrase):

If you have entered a password, then the next step will require you to enter confirmation, if not, then just press Enter.

And as a result, the password will be generated and something like this will be displayed on the screen:

Enter same passphrase again:
Your identification has been saved in /home/su/.ssh/id_ed25519
Your public key has been saved in /home/su/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:FOkr66ZIZN5eTZ1omlY77s1GkgZtWa+A3NNDwEKCzcE my_email@example.com
The key's randomart image is:
+--[ED25519 256]--+
| =oo..oo |
| . E.. o.o |
| . *.= . |
| +.Ooo.. |
| o oS=oo |
| + . .B=.o |
| o . =++o |
| . o oo. +. |
| . o+..o.o |
+----[SHA256]-----+

Now you need to go to the .ssh folder and copy the public part of the key, which is in the file with the extension: .pud (id_ed25519.pub).

We need to copy the contents of this particular file and paste it into the Key field of the key settings screen on GitHub. Next, you need to come up with a name for this key (in case there are several keys) and enter this name in the Title field. After filling in both fields and clicking the Add SSH key button, adding the key can be considered completed. And now it would be nice to make sure that everything works.

To do this, we will create a test project in our repository, copy it to the local machine, try to make changes to our project and "push" the result to the repository.

Clicks the "plus" to the left of the avatar in the upper right corner of GitHub, between the "bell" and the avatar --> New repository

Next, in the page that opens, enter the project name under Repository name, write a few words about the project in the Description section, mark "add" for Add a README file > and Add .gitignore, and in the ".gitignore" options, choose Python. And at the end of everything, click Create repository.

The project has been created, and now we copy it to a folder on the local computer, for which we select the latter in the HTTPS / SSH switcher and copy the link to our newly created project in the GitHub repository.

But before copying, we need to make sure that the git package is installed on our computer. This can be done with the command:

$ git --version

If it turns out that git is not installed, then we use the following commands;

$ sudo apt-get update
$ sudo apt-get install git

Now, directly in the folder where we want to place this project, open the terminal window (right mouse button --> "Open in Terminal"), and then, in the terminal itself, enter:

$ git clone git@github.com:it4each/my-project.git

The first time we clone, we will most likely end up with something like this:

Cloning into 'my-project'...
The authenticity of host 'github.com (140.82.121.4)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

We answer yes and the project is successfully added to the specified folder on our computer. In addition, the GitHub address has been added to the list of known hosts (see below).

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,140.82.121.4' (RSA) to the list of known hosts.
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.

And if we now once again examine the contents of the keys folder, we will see a new file (known_hosts) in it with a list of known hosts, where the GitHub host is also added:

$ ls -al ~/.ssh drwx------ 2 su su 4096 сеп 25 10:49 .
drwxr-xr-x 23 su su 4096 сеп 24 23:39 ..
-rw------- 1 su su 411 сеп 24 23:43 id_ed25519
-rw-r--r-- 1 su su 102 сеп 24 23:43 id_ed25519.pub
-rw-r--r-- 1 su su 884 сеп 25 10:49 known_hosts

Now, for the project to work, we need to create a virtual environment. This requires the packages pip3 and virtualenv. If these packages are not installed yet, use the commands:

# install pip3
$ sudo apt-get install python3-pip  

# install virtual environment
$ sudo pip3 install virtualenv

To create a virtual environment, go to the project folder and then run the installation command:

$ cd my-project

$ virtualenv venv -p python3

The virtual environment has been created. Now it needs to be run:

user@user:~/Projects/django-app/my-project$ source venv/bin/activate

(venv) user@user:~/Projects/django-app/my-project$

The proof that the virtual environment is running is the appearance of the venv folder in parentheses - (venv) in the system response.

Install the first package - the Django package. And immediately after installing it, we create a new project (to create it inside the project folder, add a dot to the end of the command):

$ pip3 install Django

$ django-admin startproject main .

We remember all installed packages in the requirements.txt file:

$ pip3 freeze > requirements.txt

Now there are enough files in our folder to make the first commit and save the work done in the GitHub repository. To do this, first add all the created files with the add --all command, then create a commit commit and send this commit to the repository with the push command:

$ git add --all

$ git commit -am "first commit"

$ git push

If everything went without errors, then the following files should be added to the repository:

[main 0a792bb] first commit
7 files changed, 200 insertions(+)
create mode 100644 main/__init__.py
create mode 100644 main/asgi.py
create mode 100644 main/settings.py
create mode 100644 main/urls.py
create mode 100644 main/wsgi.py
create mode 100755 manage.py
create mode 100644 requirements.txt

Please note that the largest folder of our project - the virtual environment folder venv was not included in the repository, because by default it is located in the .gitignore file - in the list of exclusion files for copying to remote storage. If you remember, GitHub offered us to add this file when creating the my-project project.

It should be noted that there is no .idea folder for PyCharm service files in .gitignore. Therefore, if you use this IDE to create your project, don't forget to add an exception for the .idea folder yourself.

For more information on adding an SSH key and creating a project, watch this video (RU voice):