Detailed explanation of the basic usage of SSH's ssh-keygen command

Detailed explanation of the basic usage of SSH's ssh-keygen command

SSH public key authentication is one of the SSH authentication methods. SSH password-free login can be achieved through public key authentication, and git's SSH method is also authenticated by public key.

In the home directory of the user directory, there is a .ssh directory. Almost all the files related to the current user's ssh configuration authentication are in this directory.

ssh-keygen can be used to generate the public and private key files required for ssh public key authentication.

When using ssh-keygen, please go to the ~/.ssh directory first. If it does not exist, please create it first. And make sure that the permissions of ~/.ssh and all parent directories cannot be greater than 711

Generated file name and location

Using ssh-kengen will generate two files in the ~/.ssh/ directory. When the file name and key type are not specified, the two files generated by default are:

id_rsa

id_rsa.pub

The first is the private key file and the second is the public key file.

When generating an ssh key, you can specify the file name of the generated file through the -f option, as follows:

[[email protected]]$ ssh-keygen -f test -C "test key"
               ~~File Name~~~~ Notes

If no file name is specified, you will be asked to enter one:

[[email protected]]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/huqiu/.ssh/id_rsa):

You can enter the file name you want, here we enter test.

After that, you will be asked if you need to enter a password. After entering the password, you will need to enter it every time thereafter. Please decide whether you need a password based on your security needs. If not, just press Enter:

[[email protected]]$ ssh-keygen -t rsa -f test -C "test key"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

If the file name is test, the result is:

[[email protected]]$ll test*
-rw------- 1 huqiu huqiu 1675 Sep 15 13:24 test
-rw-r--r-- 1 huqiu huqiu 390 Sep 15 13:24 test.pub

In the command generated above, the -C option is a comment in the public key file:

[[email protected]]$ cat test.pub
ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAQEAlgjiMw7AskxbvpQY9rmZPQxQBzh9laxFvbaini2EgmQkNsXBA9WJOXn2YBJauoiVsdUKBWA97avjsobrTxsCYvFr1yQQvTfTlbqlqGNIhQc/3HjTl2pIkClpDWvBrRN+jpyESS4MNbfOL1qjT4c/QhGvj6U6HrN6kUyn58oyyJpTzOLG74AZELJ2Led57QvTw1yJXZuAMWioR0A3BGd25fdocLX3ebux6ya8AsloOVYfsAqGlggrARe6FXjLfMH4a/nxaAdiDYVXU/Vr1ybK9P7SfyEDGJi3JtgiPUlA6vPxUC
E+9IJPQaqqeqCGzrJ6G/XO7om1v9YLLG/H/ZN2tQ== test key
                      ~~~~Remarks

In order for the private key file and public key file to work in authentication, please ensure that the permissions are correct.

For .ssh and its parent folder, the current user must have execute permission, and other users can only have execute permission at most.

The same is true for public and private key files: the current user must have execution permissions, and other users can only have execution permissions at most.

The above is the basic usage of SSH's ssh-keygen command. I hope it is useful to everyone. For more articles about the usage of ssh-keygen, please see the following related articles

You may also be interested in:
  • SSH remote login and port forwarding detailed explanation
  • Install OpenSSH on Windows and log in to the Linux server by generating an SSH key
  • Unbind SSH key pairs from one or more Linux instances
  • In-depth analysis of the three components of SSH: ssh, sftp, and scp
  • Use PSSH to batch manage Linux servers
  • Detailed explanation of Redis unauthorized access and SSH key file utilization
  • SSH principle and two login methods detailed illustration
  • How to create a Docker image that supports SSH service
  • How to add or modify SSH port number in CentOS7
  • How to set up SSH remote debugging in PyCharm
  • Detailed explanation of a simple example of webssh based on Django
  • How to upload files and folders to Linux server via SSH
  • How to modify the default port number of CentOS6.5 and CentOS7 ssh
  • Spring+Hibernate+Struts(SSH) framework integration practice
  • How to install ssh service and realize remote access in ubuntu16.04
  • How to specify ssh port for rsync file synchronization
  • Use Python to update the ssh remote code to operate the remote server implementation code
  • How to write a lightweight ssh batch operation tool in Go
  • SSH port forwarding, local port forwarding, remote port forwarding, dynamic port forwarding details

<<:  Detailed explanation of Vue's seven value transfer methods

>>:  A comprehensive summary of frequently used statements in MySQL (must read)

Recommend

A brief analysis of the use of the HTML webpack plugin

Using the html-webpack-plugin plug-in to start th...

Detailed explanation of how to customize the style of CSS scroll bars

This article introduces the CSS scrollbar selecto...

How to run nginx in Docker and mount the local directory into the image

1 Pull the image from hup docker pull nginx 2 Cre...

How to change the Ali source in Ubuntu 20.04

Note that this article does not simply teach you ...

Detailed process of configuring Https certificate under Nginx

1. The difference between Http and Https HTTP: It...

How to customize at and cron scheduled tasks in Linux

There are two types of scheduled tasks in Linux s...

HTML table markup tutorial (22): row border color attribute BORDERCOLORLIGHT

Within rows, light border colors can be defined i...

How to use Nginx to solve front-end cross-domain problems

Preface When developing static pages, such as Vue...

Improving the effect of hyperlinks in web design and production

Hyperlinks enable people to jump instantly from pa...

How to use Docker Compose to implement nginx load balancing

Implement Nginx load balancing based on Docker ne...

What command is better for fuzzy searching files in Linux?

1. Introduction This article mainly explains how ...

MySQL 8.0 DDL atomicity feature and implementation principle

1. Overview of DDL Atomicity Before 8.0, there wa...