使用的机器:

A机器  IP  172.17.41.246

B机器  IP  172.17.41.245

C机器  IP  172.17.41.242

需求:C机器只允许B机器ssh登录,不允许其他任何机器ssh登录;现在想要A机器能ssh登录C机器,可以通过B机器建立隧道进行登录。


首先,在C机器上设置iptables只允许 B机器ssh

iptables -A INPUT -s 172.17.41.245 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP


在A机器上执行命令建立隧道,需要输入B机器也就是172.17.41.245的密码

ssh -CfNg -L 5550:172.17.41.242:22 souche@172.17.41.245

此处如果使用sftp不能登录时,可以使用下面命令替代上面命令建立隧道,比上面命令多个-n选项

ssh -CnfNg -L 5550:172.17.41.242:22 souche@172.17.41.245


然后在A机器上开始登录C机器,输入C机器的root用户的密码后即可登录。

ssh root@127.0.0.1 -p 5550

登录后查看IP

[root@localhost ~]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:50:56:93:1F:D6
          inet addr:172.17.41.242  Bcast:172.17.41.255  Mask:255.255.254.0
          inet6 addr: fe80::250:56ff:fe93:1fd6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1088423 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1128560 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:153778919 (146.6 MiB)  TX bytes:631865288 (602.5 MiB)


参考:

http://www.cnblogs.com/youxin/p/5220916.html

http://blog.creke.net/722.html

http://blog.csdn.net/junmuzi/article/details/52241601