ssh免密登录

 首先先执行以下命令,一路回车,并查看生成文件路径

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的远程连接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值