42

Question: How do I generate ssh private and public keys (to be used in GitHub/GitLab) using command line.

The command below generates the error

sh.exe": syntax error near unexpected token '('

I am using Windows XP.

$ ssh-keygen -t rsa -C "xxxx@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxxx/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /c/Users/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db xxxx@gmail.com
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Q_Mlilo
  • 1,729
  • 4
  • 23
  • 26
  • The error message looks like you are trying to run a `bash` script but using `sh` instead. See [Difference between sh and bash](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – tripleee Jan 22 '22 at 11:45
  • As a note, for many years `-t ed25519` has been enough better that it's now the default and strongly preferred type. It delivers better security with shorter keys and shorter signatures that are faster to use and check. To match the security ed25519 delivers with 256-bit keys you need 4096 bits of RSA key. – jthill Sep 01 '23 at 11:01

6 Answers6

60

The command to run is only

ssh-keygen -t rsa -C "you@example.com"

All the rest beginning with line 2 of your script is the output of ssh-keygen.

And replace you@example.com with your email address.

Have a look at the manual for ssh-keygen to look for additional options. You should probably use a longer key by adding -b 4096 to the option list.

Residuum
  • 11,878
  • 7
  • 40
  • 70
13

Here is the command

ssh-keygen -t rsa -b 4096 -C "[your github's email]"
# Creates a new ssh key
# Generating public/private rsa key pair.

This will generate a key for you. You have to copy that and insert into your Github's account (just one time).

Steps how to do It

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Hazarapet Tunanyan
  • 2,809
  • 26
  • 30
6

Step 1: Generate Your SSH Key

$ ssh-keygen -t rsa -b 4096 -C "example@example.com"

Step 2: Use the Key

$ eval $(ssh-agent -s)

Then add the key we just generated. If you selected a different path than the default, be sure to replace that path in the command.

ssh-add ~/.ssh/id_rsa

Step 3: Add the SSH Key on GitHub

clip < ~/.ssh/id_rsa.pub

if clip not found then add the next command

cat ~/.ssh/id_rsa.pub

Finally Result something like on your cmd

ssh-rsa AAAAB3NzaC1yc2EAAAAD173Oe1kp0Ojnsn7sRWt/XT5nFH3CSTv6VWyxq4YUJ4ZykWa5opyiAJmvtjxOMCmVTuX2r1T4Livn0foHGt7+66FJXrXUQgyJ4RXanufb3bAekxbFPg3S6Gyr2kk+I3TYWcFsLLwjU0VVJwodQkpyygAUzhomx9OQ0FMpfkQa5VrZnaSLjjtNOUSAaq30e7JWYxtoVih2HfVkcmqc53QjY4T3Xb0cmLZF3EmwCeB66dgJtpTNMvM54ceD30INsaMFNqG8XjbJtGUCHYEIR5l/LI20K5F25BRTnCzRV9dB5FUJ8taVMGRHJob9PDUdxpA2HEJYilm8ZbLbRmKJtU+H91WNCG6pvy9Yf9MREkOZQHcWIuAsH8uJvTTbqm1eAY2g34FjarRdqZIpvdxkrErDHqh4k42owNEmHjSaAw53Y8M54fjBdFHTjGDqHBamlKcIzQbin/czFq1a+pgopIANDhZjWZNvMiTak7BjVrVOciKD3Pa/KKy03nSEfEGExLbFEi1Q8QEGd6idtUAjL7fyVPWatRwCnOvGLMPTk73m7t0IAYTm4n7dO0S5OeWTUPTT+8vIfFCmE9OT2aJDIpZY1+f8Q== youremail@gmail.com

copy from your cmd and go to your GitHub account setting find ssh and gpg keys

Naved Khan
  • 1,699
  • 16
  • 13
2

Just apply two commands :

ssh-keygen -t rsa -b 4096

cat ~/.ssh/id_rsa.pub

Copy ssh key and paste on browser

Go to home/.ssh folder. Click on config file and add these two line(if config file doesn't exist then create a file name as "config")

HostKeyAlgorithms ssh-rsa

PubkeyAcceptedKeyTypes ssh-rsa

Majedul Islam
  • 213
  • 2
  • 5
1

Generate SSH key using below command

ssh-keygen -trsa -C user@xyz.com

just hit enter to apply the default value for all the inputs it asks

the output will be

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): 
/home/user/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx user@xyz.com
The key's randomart image is:
+---[RSA xxxx]----+
| ..++xX*Ox        |
| x..xx/+.x. + .   |
|.. +..*+xx x +    |
|... +x+o x.       |
|x.. + +..x        |
|xx.. x            |
|...               |
|x x               |
|x  x.             |
+----[SHA256]------+
  

just use the cat command to log the ssh key file contents on the terminal(use your own path here).

cat /home/user/.ssh/id_rsa.pub

the output will be

ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx user@xyz.com

copy the contents and go to your github account go to settings under Account settings>SSH and GPG keys click on New SSH key, provide the name you wish and paste the copied contents in the value and save. that's it you are ready to commit your changes without using username, password every time.

Akhil S
  • 955
  • 11
  • 16
0

Solution: ssh-keygen -t rsa

Explanation : ssh-keygen is a tool for creating new authentication key pairs for SSH. Such key pairs are used for automating logins, single sign-on, and for authenticating hosts

(for example cloning project from your private repo on Github straight to your aws machine).

Options: You can perform more complicated operations and using flags in order to generate a tailor-made key according to your use case, extended functionality are more powerful and secured. The basic flags are: bits (Large integers making the the RSA key be less vulnerable and hard to crack), passphrase (similar to password) , type (dsa/ecdsa/ed25519/rsa) , comment for the specific ssh token (email or user name) and output key (default store on ~/.ssh/ path)

Synopsis: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa] [-N new_passphrase] [-C comment] [-f output_keyfile]

Example:

ssh-keygen -b 4096 -t rsa -n "tHiSiaPasSWoRd" -c "johnDoe@gmail.com" -f ~/.ssh/id_rsa
avivamg
  • 12,197
  • 3
  • 67
  • 61