借助SSH反向代理实现在家里远程登录公司电脑(一)

来源:互联网 发布:域名在线生成器 编辑:程序博客网 时间:2024/04/29 14:53

         针对这个问题首先要对现实场景做下简要介绍,公司里的电脑可以访问外部网络,但是公司又没开通VPN原则上未提供在家里接入公司电脑的途径,但偶尔会需要在家里接入公司电脑。 本文就是针对这个问题,借助SSH的强大功能来实现对公司电脑的远程管理。有人说直接用teamviewer就可以,其实我也用过,但是对私人版的性能不满意,尤其是在Ubuntu上很难用。

         下面就介绍下我的网络环境,

1. 家里电脑安装Ubuntu 12.04 LTS  32bit版本 (电脑名 homeU)

2.家用路由器 水星的,支持动态DNS,我申请的www.oray.com的免费域名

3.公司办公电脑windows(Named:pcW),公司工作电脑Ubuntu(Named: pcU)


接下来就要开始网络的配置了,在此还想介绍下强大的SSH功能,

不熟悉SSH是什么的请参考wiki

ssh 本文中要用到的关键参数

ssh -NfR 远端主机listen port :远端连回时导向的主机 :远端连回本地主机时导向主机的port  帐号@远端主机

ssh参数解释:
-N:不执行任何指令
-f:背景执行
-R:主要建立reverse tunnel的参数


作为SSH的反向隧道,关键是运用了ssh的-R参数,下面是man ssh时候给出的官方介绍:

 -R [bind_address:]port:host:hostport
             Specifies that the given port on the remote (server) host is to
             be forwarded to the given host and port on the local side.  This
             works by allocating a socket to listen to port on the remote
             side, and whenever a connection is made to this port, the connec‐
             tion is forwarded over the secure channel, and a connection is
             made to host port hostport from the local machine.

             Port forwardings can also be specified in the configuration file.
             Privileged ports can be forwarded only when logging in as root on
             the remote machine.  IPv6 addresses can be specified by enclosing
             the address in square braces.

             By default, the listening socket on the server will be bound to
             the loopback interface only.  This may be overridden by specify‐
             ing a bind_address.  An empty bind_address, or the address ‘*’,
             indicates that the remote socket should listen on all interfaces.
             Specifying a remote bind_address will only succeed if the
             server's GatewayPorts option is enabled (see sshd_config(5)).

             If the port argument is ‘0’, the listen port will be dynamically
             allocated on the server and reported to the client at run time.
             When used together with -O forward the allocated port will be
             printed to the standard output.

        接下来是准备工作介绍,

1.路由器准备工作: 首先为家里的路由器申请域名,直接进入www.oray.com可以获取免费域名,我申请的是(mypcname.vicp.cc)因为跟本文关系不大,就不详细说了吧!

2. 路由器准备工作: 开启DMZ功能,将家用电脑homeU的暴露给公网,这样外部访问mypcname.vicp.cc时候就是跟我局域网内的电脑homeU交互了

3. 家用电脑homeU准备: 安装ssh server(sudo apt-get install openssh-server)应用,这样外面的电脑才能ssh方式登录到我的homeU


在完成上面的准备工作后,终于可以动手配置了。

1. 首先在公司电脑上ping 自己申请到的域名mypcname.vicp.cc,能ping通后就准备开工了。

2. 在公司电脑pcU的shell里开启ssh 登录到家里电脑 ssh -NfR 2284:localhost:22myuser@mypcname.vicp.cc

(注:myuser是我家里电脑homeU的用户名,接下来会要求输入密码,此时输入homeU的myuser 账户密码)

在成功登录后,就实现了ssh反向链接了,此时访问homeU:2284就等效于访问pcU的22 端口了

3. 在homeU应该可以成功登录 pcU,命令 myuser@homeU$ ssh user@localhost -p 2284

(注:此时是为在家里借助已经建立的ssh登录公司电脑,所以 命令中user是公司电脑 pcU的用户,需要输入user账户的密码)


以上操作如果都能顺利成功,说明这项工作已经成功一大半了,打字手都累了,

下篇再介绍家里电脑远程登录公司其他windows 电脑的方法吧


参考文章:

http://www.ruanyifeng.com/blog/2011/12/ssh_port_forwarding.html

blog.csdn.net/zokie/article/details/7190195

https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/