You can create a public key primarily by generating an SSH key pair using the ssh-keygen
command-line utility. This generates both a private key (which you keep secret) and a public key (which you can share).
Generating an SSH Key Pair
The most common method for creating a public key is through generating an SSH (Secure Shell) key pair. Here's a breakdown of the process:
-
Using
ssh-keygen
: The core tool for generating SSH key pairs is thessh-keygen
command-line utility.-
Open your terminal or command prompt.
-
Type the following command:
ssh-keygen
-
Follow the prompts. You will be asked:
- Where to save the key (the default is usually
~/.ssh/id_rsa
). - To enter a passphrase (it's highly recommended to use a strong passphrase to protect your private key). You can leave it empty, but that is less secure.
- Where to save the key (the default is usually
-
-
Key Pair Location: By default, the
ssh-keygen
command creates two files in the~/.ssh/
directory (on Unix-like systems) or a similar location on Windows if you have SSH utilities installed.- Private Key:
id_rsa
(or the filename you specified). Keep this file secret and secure. This is your identity. - Public Key:
id_rsa.pub
(or the filename you specified with the.pub
extension). You can share this key.
- Private Key:
-
Accessing the Public Key: The
id_rsa.pub
file contains your public key. You can open this file with a text editor to copy its contents. The public key usually starts withssh-rsa
,ssh-dss
,ecdsa-sha2-nistp256
, ored25519
, followed by a long string of characters, and ends with your username and hostname.
Important Considerations
- Security: Never share your private key. Anyone with your private key can impersonate you.
- Windows: If you are using Windows and don't have access to
ssh-keygen
, you may need to install an SSH client like Git for Windows, which includes the OpenSSH suite of tools.