在windows中安装OpenSSH,无密码登录,永远不断线
到这里下载最新安装包:https://github.com/PowerShell/Win32-OpenSSH/releases
下载下来解压,然后用管理员打开服务器自带的PowerShell,运行下列命令:
cd C:\OpenSSH-Win64\OpenSSH-Win64
Set-ExecutionPolicy unrestricted
.\install-sshd.ps1
.\ssh-keygen.exe -A
假定host1是本地主机,host2是远程主机。由于种种原因,这两台主机之间无法连通。但是,另外还有一台host3,可以同时连通前面两台主机。因此,很自然的想法就是,通过host3,将host1连上host2。
我们在host1执行下面的命令:
"本地端口:目标主机:目标主机端口"
$ ssh -L 2121:host2:21 host3
监听本地2121端口,当访问本地2121端口时,数据将通过host3 转发到host2 的21 端口。
$ ssh -N -L 0.0.0.0:80:localhost:80 pi@host3
监听本地80端口,当访问本地80 端口时会 转发到host3的80端口, 这里的localhost是指的是host3的“本地”
另一个例子是通过host3的端口转发,ssh登录host2。
$ ssh -L 9001:host2:22 host3
这时,只要ssh登录本机的9001端口,就相当于登录host2了。-p参数表示指定登录端。
$ ssh -p 9001 localhost
还是接着看上面那个例子,host1与host2之间无法连通,必须借助host3转发。但是,特殊情况出现了,host3是一台内网机器,它可以连接外网的host1,但是反过来就不行,外网的host1连不上内网的host3。这时,"本地端口转发"就不能用了,怎么办?
解决办法是,既然host3可以连host1,那么就从host3上建立与host1的SSH连接,然后在host1上使用这条连接就可以了。
我们在host3执行下面的命令:
$ ssh -R 2121:host2:21 host1
R参数也是接受三个值,分别是"远程主机端口:目标主机:目标主机端口"。这条命令的意思,就是让host1监听它自己的2121端口,然后将所有数据经由host3,转发到host2的21端口。由于对于host3来说,host1是远程主机,所以这种情况就被称为"远程端口绑定"。
绑定之后,我们在host1就可以连接host2了:
$ ftp localhost:2121
做代理
ssh -D 8081 xxx@host
在host上建立8081端口的代理。
设置SSH免密登陆:
(这一步可以忽略) [服务段]:通常linux下会修改ssh_config文件来修改ssh配置,但在安装目录并没有发现这个文件,查阅官方wiki后发现,原来是在C:\ProgramData\ssh目录下(此目录为隐藏目录)
端口号:Port 22
密钥访问:PubkeyAuthentication yes
密码访问:PasswordAuthentication no
空密码:PermitEmptyPasswords no
[客户段] 生成公私钥匙。 输入命令,不要使用保护密码,设置保护密码为空。
ssh-keygen -t rsa
生成的密钥,在C:\Users\[账户名]\.ssh 下。将公钥 id_rsa.pub 发送到[服务端] 并放在C:\Users\[账户名]\.ssh,并重新命名为authorized_keys(也可在ssh_config修改路径)
设置完成后重启sshd服务。
[客户端] 使用ssh连接,需要加上私钥id_ssh,举例子如下:
ssh -i id_rsa -N -L 0.0.0.0:3306:localhost:3306 administrator@10.168.1.154
这时候会遇到Permissions for 'id_rsa' are too open. 的问题。
需要把其他人的权限全部去掉,只保留自己能够访问,不用父级目录继承权限。这样就可以自动登陆了,但是第一次需要输入yes。以后就可以无人值守了。如果发现仍然要输入密码,那么进入[服务端]运行 FixHostFilePermissions.ps1 和 FixUserFilePermissions.ps1 的powershell脚本就可真正的免密登录了。


网上不断线的教程很多,包括用了autossh,但是仍然不好使。最终找到下面的方法:
在使用本地连接L方式时候,可以设置循环,保证永不掉线。
@echo off
echo ***
:LOOP
echo [%HOST%] [%date% %time%] ssh running...
ssh -i id_rsa -N -L 0.0.0.0::localhost: administrator@10.168.1.154
timeout > NUL
goto LOOP
echo [%HOST%] [%date% %time%] exited
但是在使用远程连接R的时候,上述方法不一定好使, 因为网络断了连接不会自动终端,也就无法触发循环。可以尝试使用TCPkeepAlive=yes 还有配置ServerAliveInterval向服务器发送请求判断是否掉线。实测发现,即使网络断线后,一旦网络恢复稳定后,会自动重新连接。
ssh.exe -i id_rsa -o StrictHostKeyChecking=no -o TCPKeepAlive=yes -o ServerAliveInterval= -N -R :10.8.69.4:7999 administrator@10.168.1.154
踩过的坑:
命令行不识别空格时:C:\Program Files\用C:\Progra~1\替代
Windows Service2012R2即使配置了.ssh/authorized_keys公钥,连接时依然显示没有注册公钥。。。
查阅了官方wiki判断可能是权限问题:Fix SSH file permissions
进入C:\Program Files\OpenSSH(安装目录),右键 FixHostFilePermissions.ps1【使用PowerShell运行】,命令行提示全选是,重启sshd服务后密钥连接正常
附: 常见错误如下:
Error message: channel 3: open failed: connect failed: Connection refused
Change localhost to 127.0.0.1 in the ssh -L parameter.
Cannot listen on port X on local machine because of network policies.
Try to use another port locally. Ports such as 3306 (MySQL) may have been left open. These are good to use for SSH tunneling if you aren’t already running MySQL.
Error message: Privileged ports can only be forwarded by root.
Use a port above 1024, or try to set up the SSH tunnel as root.
Error message: bind: Address already in use, channel_setup_fwd_listener: cannot listen to port: xxxx, Could not request local forwarding.
Some local server process is already listening on the local port you’re trying to forward to. Pick a different local port and configure your program to connect to th at port instead. If your program cannot be configured to listen to a different port, try to find what server process is occupying that port (netstat -a on Linux or lsof -i -P on Mac OS X) and stop it. Retry setting up the tunnel.
I want other hosts on my network to be able to use the tunnel I established.
By default, only local clients can connect to SSH tunnels established this way.
Use the -g option when setting up the tunnel. Realize that this is insecure, but it may make sense in certain scenarios.
I don’t know what local port is available for me to use.
Linux: netstat -a | grep LISTEN
Mac OS X: lsof -i -P | grep LISTEN
will show you the ports that are in use. Generally, you can pick any that’s not already taken. To make sure you’re not breaking some other unknown protocol, check the IANA Well-known Port Numbers list and pick one that’s not taken.
If you’ve not been able to debug this so far, try passing the -v parameter to ssh to see verbose output. Add another -v for more verbose output.
在windows中安装OpenSSH,无密码登录,永远不断线的更多相关文章
- 怎样把windows中安装的程序列出来?
症状/问题我怎样把windows中安装的程序信息输出到一个文本文件中?解决方法使用 windows 操作系统中的命令:wmic就可以做到.下面的命令就可以把系统中安装的程序都输出到文件ProgramL ...
- windows中安装python
windows中安装python 在windows中安装python的步骤如下. 1.下载python的安装包 python的安装包地址为: https://www.python.org/ftp/py ...
- 在Windows中安装PostgreSQL
在Windows中安装PostgreSQL 虽然PostgreSQL是为类UNIX平台开发的,但它却是可以移植的.从7.1版本开始,PostgreSQL可以编译安装和作为一个PostgreSQL服务器 ...
- Windows中安装Scrapy
在linux中安装Scrapy只需要导入一些非python的支持包,在windows中安装Scrapy则是一波三折. 总之来说,主要分为以下几个步骤,可能由于系统问题(国内个人机子,甚至是小企业的机子 ...
- 在Windows中安装MinGW-w64(有图,一步一步)
在Windows中安装MinGW-w64 发表回复 如需配合Sublime Text 3编译C程序, 请参考本站文章: 使用Sublime Text 3与MinGW-w64编译C语言程序 MinGW, ...
- 下载文件时-修改文件名字 Redis在Windows中安装方法 SVN安装和使用(简单版) WinForm-SQL查询避免UI卡死 Asp.Net MVC Https设置
下载文件时-修改文件名字 1后台代码 /// <summary> /// 文件下载2 /// </summary> /// <param name="Fil ...
- Python原来这么好学-1.1节: 在windows中安装Python
这是一本教同学们彻底学通Python的高质量学习教程,认真地学习每一章节的内容,每天只需学好一节,帮助你成为一名卓越的Python程序员: 本教程面向的是零编程基础的同学,非科班人士,以及有一定编程水 ...
- 在Windows中安装PySpark环境
在Windows中安装PySpark环境 安装Python 可以选择安装官方版本的Python,或是Anaconda,对应的地址如下. 下载地址 Python:https://www.python.o ...
- windows下安装openssh服务并实现远程登录
需要准备的工具: winscp 点击下载 openssh 点击下载 步骤: 在远程计算机安装 1.首先安装openssh,双击并安装 2.指定用户的home directory为C:\ ...
随机推荐
- 欧拉函数 and 大数欧拉 (初步)
前两天总结了素数筛法,其中就有Eular筛法.现在他又来了→→ φ(n),一般被称为欧拉函数.其定义为:小于n的正整数中与n互质的数的个数. 毕竟是伟大的数学家,所以以他名字命名的东西很多辣. 对于φ ...
- HihoCoder1449 重复旋律6(后缀自动机)
描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数列. 现在小Hi想知道一部作品中所有长度为K的旋律中出现次数最多的旋律的出现次数.但是K不是固定的,小Hi想知道对 ...
- npm镜像安装
安装淘宝NPM镜像 https://npm.taobao.org/ npm install -g cnpm --registry=https://registry.npm.taobao.org 配置 ...
- hadoop-hbase学习笔记
create "t",{NAME=>"t_id"},{NAME=>"t_vl"} describe "t" ...
- puppeteer
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); ...
- 在winform下实现左右布局多窗口界面的方法(一)
在web页面上我们可以通过frameset,iframe嵌套框架很容易实现各种导航+内容的布局界面,而在winform.WPF中实现其实也很容易,通过本文给大家介绍在winform下实现左右布局多窗口 ...
- linux下查找命令两则
由于本人不会阅读man手册,因此需要几下两个常用的查找命令: (1)在某个目录及其子目录查找某个文件并打印完整路径: find ./ -name "assertions.h" -e ...
- What makes an inferred latch? how To avoid creating inferred latches? when do you know you need latches?
What makes an inferred latch?For combinatorial logic, the output of the circuit is a function of inp ...
- webrtc windows下的编译
mkdir webrtc-checkoutcd webrtc-checkout set DEPOT_TOOLS_WIN_TOOLCHAIN=0set GYP_GENERATORS=ninjaset G ...
- linux 将大文件分解为多个小文件
使用的命令为: split --bytes 500M --numeric-suffixes --suffix-length=3 foo foo_ 其中 --bytes 为小文件的大小, --suffi ...