Chinaunix首页 | 论坛 | 博客
  • 博客访问: 690331
  • 博文数量: 214
  • 博客积分: 5015
  • 博客等级: 大校
  • 技术积分: 2285
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-18 17:02
文章分类

全部博文(214)

文章存档

2008年(43)

2007年(171)

我的朋友

分类:

2007-09-06 10:45:36

内网的朋友苦于没有合法 ip,不能对外提供 internet 服务。解决方案很多,可以通过在网关做端口映射,或其他的辅助软件等。

本文介绍两种比较简单实用的方法,利用 ssh 这个强大的工具。
(以下方法不分平台,都适用)

案例一、
内网主机 A ,开了 http,ftp ,http ,vnc,sshd,socks5,cvs 等服务。无合法 ip 地址。
外网主机 B ,开了 sshd 服务。有合法 ip : 218.xxx.xxx.xxx
我们的目的是让 B 能访问 A 上的各种服务。

步骤:
1、A 知道 B ip 后,先用 ssh client 连上 B,命令如下:
ssh -R 1234:localhost:21 -l root 218.xxx.xxx.xxx

解释:
关于 ssh 的参数,请看 ssh --help
-L listen-port:host:port Forward local port to remote address
-R listen-port:host:port Forward remote port to local address
-L local (本地) -R :remote (远程)

-R 1234:localhost:21 其实做了个“端口转发(forward)"。
意思是主机 A 把本地的 21端口(对应ftp服务)映射为 B 的1234 端口(任意未被占用),同时 A 监听 B 的1234 端口。
在 B 上用 netstat -al | grep 1234 ,你能看到这个监听连接。
任何发送到 B 1234 端口的请求将被传送到 A的 21 端口。

2、B 用 ftp 工具(任意,如gftp) 连本地的 1234 端口,输入 A 的 ftp 用户和密码。
ftp localhost 1234

千万不要觉的奇怪,为什么连的是本地的地址。
举个不恰当例子,相当于 A 在 B 的房间里装了个窃听器(监听端口),那么 B 在房间里说的话就通过窃听器传送到了 A。

3、推广:
如果 B 没占用 21 端口的话,那么可以写成:
A使用: ssh -R 21:localhost:21 -l root 218.xxx.xxx.xxx
B使用: ftp localhost

如果你想使用 A 上的 http 或其他服务,只需改变服务端口:
http服务 :
A使用:ssh -R 1234:localhost:80 -l root 218.xxx.xxx.xxx
B使用:w3m

sshd服务:
A使用:ssh -R 1234:localhost:22 -l root 218.xxx.xxx.xxx
B使用:ssh localhost -p 1234

vnc 服务:
A使用:ssh -R 1234:localhost:5901(其他) -l root 218.xxx.xxx.xxx
B使用:vncviewer localhost:1

socks5服务:
A使用:ssh -R 1234:localhost:1080 -l root 218.xxx.xxx.xxx
B 略

cvs 服务:
A使用:ssh -R 1234:localhost:2401 -l root 218.xxx.xxx.xxx
B使用:cvs -d :pserver:root@localhost:1234/home/cvsroot login

这里是否一定要用 root ,涉及到权限问题,具体还得靠大家来总结经验。

案例二、
部分朋友会问了,这样的话只是两台机器的互相通讯,如何让广域网的人都能访问呢?
聪明的你,这时候可能已经有了答案。

内网主机 A ,开了 http,ftp ,http ,vnc,sshd,socks5,cvs等服务。无合法 ip 地址。
外网主机 B ,开了 sshd 服务。有合法 ip : 218.xxx.xxx.xxx
我们的目的是让 internet 上的任何主机能访问 A 上的各种服务。

步骤:
1、首先,B 的sshd 服务端做点小小的设置:
vi /etc/ssh/sshd.config
加入 GatewayPorts yes
然后重启 sshd 服务: /etc /init.d/ssh restart 或 /etc/init.d/sshd restart
(解释:
不加,默认会把监听端口绑定在 localhost 或 lo(127.0.0.1),这样除了 B自身别人是没法访问监听端口的。
加入 GatewayPorts yes,把监听端口绑定到 0.0.0.0 ,这样外部的所有机器都能访问到这个监听端口。
主要是考虑安全性问题,默认情况,只允许本地访问。

这里才是真正的难点,实验了一个晚上,累人呀!给点鼓励吧 :)

2、A 知道 B ip 后,先用 ssh client 连上 B,命令如下:
ssh -R 21:localhost:21 -l root 218.xxx.xxx.xxx
(事先确定 B 的21 端口未被占用)

3、分布在 internet 的其它客户机使用 ftp 工具(任意),连 B 21端口。

ftp 218.xxx.xxx.xxx 21

你会发现自己连上了内网 A 的ftp 服务。

此法和案例一完全一样。

internet --------->> B 21 端口----------->>A 21端口
可以叫做端口转发,或隧道技术,也可以称之为跳板(B),或反弹 。呵呵,我瞎说的。。。

可能遇到的问题:

Country:/etc# ssh localhost -p 1234
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
be:5f:d2:45:66:4d:0c:9e:2b:6b:45:65:a7:b2:85:28.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:11
RSA host key for localhost has changed and you have requested strict checking.
Host key verification failed.
Country:/etc# ssh localhost -p 1234
root@localhost's password:
Last login: Mon May 5 02:39:53 2003 from localhost
localhost root #

如上问题,请删除 ~/.ssh/known_hosts,然后再试。

点评:
当然 ssh 还有很多的功能没有用,如先用 ssh 连接 上去后,可以用 scp命令来存取文件,等等。
scp -P xxx user@host:path/file

其它突破网关传送文件的方式也千变万化。

优点是:
可以突破网关,一般情况下,向网管要求在网关上给你做端口映射是不现实的,但用此法你可以让要好的朋友给你做。
使用方案一:比较点对点传送文件比较方便,或使用ssh进行远程控制内网。
ssh本身是加密,保证安全可靠。

缺点也不少:
使用 ssh 加密,势必影响性能,可以用 -C 选项调节压缩率。
如果象方案二使用额外的服务器,数据都要服务器中转(我是这样认为,没跟踪过),势必影响速度。
公网的服务器不好找。
阅读(495) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~