ssh密钥登录 linux
Ssh provides secure remote connections to the remote systems. It is defacto protocol used to manage Linux and networking systems remotely via command line. Ssh provides a lot of extra features which makes it useful for different purposes. For example we can copy files to remote and local system via ssh with a secure manner. In order to make things straightforward we can use passwordless key based authentication for ssh protocol.
Ssh提供到远程系统的安全远程连接。 它是事实上的协议,用于通过命令行远程管理Linux和网络系统。 Ssh提供了许多额外的功能,使其可用于不同目的。 例如,我们可以通过ssh以安全的方式将文件复制到远程和本地系统。 为了使事情变得简单,我们可以对ssh协议使用基于无密码密钥的身份验证。
为客户端列出本地系统上的公钥和私钥 (List Public and Private Keys On Local system For Client)
In some cases there may exist public and private keys all ready. The default location for ssh user public and private keys is /home/user/.ssh
. So we will list whether the directory and keys exist. We will use ls
command for this.
在某些情况下,可能存在准备好的公用密钥和专用密钥。 ssh用户公钥和私钥的默认位置是/home/user/.ssh
。 因此,我们将列出目录和密钥是否存在。 我们将使用ls
命令。
$ ls -l /home/ismail/.ssh/
As we can see there is no ssh public or private key in this example.
如我们所见,此示例中没有ssh公钥或私钥。
在客户端上创建公钥和私钥 (Create Public and Private Keys On Client)
The first step is creating public and private key pairs on the client system.Public key is copied to the remote system and private key is hold on the local system. Private key should be only known by the local system user. We will use ssh-keygen
command. This command will create keys named id_rsa.pub
and id_rsa
.
第一步是在客户端系统上创建公钥和私钥对。将公钥复制到远程系统,并将私钥保留在本地系统上。 私钥应仅由本地系统用户知道。 我们将使用ssh-keygen
命令。 该命令将创建名为id_rsa.pub
和id_rsa
密钥。
$ ssh-keygen
使用ssh-copy-id将公用密钥复制到服务器(Copy Public Key to the Server with ssh-copy-id)
We will use ssh-copy-id
command in order to copy ssh public key to the remote system. We will provide the remote system IP address and if the remote user is different from local user the username. In this example we use current user.
我们将使用ssh-copy-id
命令将ssh公钥复制到远程系统。 我们将提供远程系统IP地址,如果远程用户与本地用户不同,则提供用户名。 在此示例中,我们使用当前用户。
$ ssh-copy-id 192.168.43.129
As we can see from output 1 key is added to the remote system.
从输出1中可以看到,密钥已添加到远程系统。
使用ssh echo将公钥复制到服务器(Copy Public Key to the Server with ssh echo)
We have alternative way to copy key to the remote other than ssh-copy-id
. As stated previously ssh
provides different features which makes system administrators life easy. We can run commands on remote system without an interactive shell. We will use this feature with echo
command where our key will be copied to the remote system.
除了ssh-copy-id
之外,我们还有其他方法可以将密钥复制到远程计算机。 如前所述, ssh
提供了不同的功能,使系统管理员的工作变得轻松。 我们可以在没有交互式shell的远程系统上运行命令。 我们将在echo
命令中使用此功能,其中我们的密钥将被复制到远程系统。
$ cat .ssh/id_rsa.pub | ssh 192.168.43.129 'cat >> .ssh/authorized_keys'
使用无密码连接 (Connect with Passwordless)
Now we have completed all steps. We can simply connect remote system passwordless with ssh just running following command.
现在我们已经完成了所有步骤。 我们只需运行以下命令就可以使用ssh简单地连接无密码的远程系统。
$ ssh 192.168.43.129
ssh密钥登录 linux