首先先执行以下命令,一路回车,并查看生成文件路径
ssh-keygen -t rsa
如果是windows系统的话,没有ssh-copy-id命令,我们写了一个py文件来实现ssh-copy-id命令的功能,复制下面代码保存成ssh-copy-id.py
"""ssh-copy-id for Windows.
Example usage: python ssh-copy-id.py ceilfors@my-remote-machine
This script is dependent on msysgit by default as it requires scp and ssh.
For convenience you can also try that comes http://bliker.github.io/cmder/.
"""
#ssh-copy-id.py
import argparse, os
from subprocess import call
def winToPosix(win):
"""Converts the specified windows path as a POSIX path in msysgit.
Example:
win: C:\\home\\user
posix: /c/home/user
"""
posix = win.replace('\\', '/')
return "/" + posix.replace(':', '', 1)
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--identity_file", help="identity file, default to ~\\.ssh\\idrsa.pub")
parser.add_argument("-d", "--dry", help="run in the dry run mode and display the running commands.",
action="store_true")
parser.add_argument("remote", metavar="user@machine")
args = parser.parse_args()
local_key = winToPosix(args.identity_file)
remote_key = "~/temp_id_rsa.pub"
# Copy the public key over to the remote temporarily
scp_command = "scp {} {}:{}".format(local_key, args.remote, remote_key)
print(scp_command)
if not args.dry:
call(scp_command)
# Append the temporary copied public key to authorized_key file and then remove the temporary public key
ssh_command = ("ssh {} "
"touch ~/.ssh/authorized_keys;"
"cat {} >> ~/.ssh/authorized_keys;"
"rm {};").format(args.remote, remote_key, remote_key)
print(ssh_command)
if not args.dry:
call(ssh_command)
windows用户执行以下命令
python ssh-copy-id.py -i :c:/users/{你的用户名}/.ssh/id_rsa.pub root@192.168.1.182
linux或者mac用户执行以下命令
ssh-copy-id -i /Users/luzhipeng/.ssh/id_rsa dxr@192.168.1.182
遇到需要输入密码的时候输入密码按回车即可
做完之后,在使用ssh命令进行登录来查看是否还需要输入密码
ssh dxr@192.168.1.182
可以发现已经可以直接登录上了,这个方法也适用于vscode的远程连接