Linux服务器每次登录或者scp复制文件时都须要繁琐的输入密码过程,而使用SSH Key来实现SSH无密码登陆不只免去了繁琐的密码输入步骤,也为Linux服务器增长了又一道安全防线(能够禁用掉ssh-root密码登陆).python
不少文章介绍ssh无密码登陆方式都有多个步骤,其实远没必要这么麻烦,接下来咱们以windows系统cmder为例完成ssh无密码登陆设置,要求下载的cmder为完整版。git
首先看C:Users{用户名}目录下有没有.ssh目录,而且目录中是否已经存在id_rsa.pub文件,若是已经有该文件,请跳到步骤3,请不要轻易删除该文件,除非你知道该文件被覆盖/删除意味着什么。github
打开cmder,执行:ssh-keygen -t rsa,按Enter键,输入一个密码,而后再次输入一样的密码,密码至少要20位长度,随后就会在.ssh文件夹生成相对应的公私钥文件。windows
"""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/. """ 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", default=os.environ['HOME']+"\\.ssh\\id_rsa.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 {} " "mkdir ~/.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)
将以上python代码保存到本地,命名为ssh-copy-id.py,而后cmder执行python ssh-copy-id root@xx.xx.xx.xx,其中root为登录用户名,xx.xx.xx.xx为IP
随后会提示输入远程服务器密码,密码正确则自动登录服务器并把公钥文件复制到Linux服务器。再次尝试登录服务器会发现已经不须要密码了。安全