如何从家庭网络外部访问我的 SSH linux 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18939285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:54:24  来源:igfitidea点击:

How to access my SSH linux server from outside my home network

linuxubuntussh

提问by Greg

So I've been scouring the internet for days trying to figure this out and can't figure it out.

所以我已经在互联网上搜索了几天试图弄清楚这一点,但无法弄清楚。

I know its going to be an easy one.

我知道这将是一件容易的事。

So I recently took an old desktop and loaded ubuntu server 12.04 on it. My goal with this project is to create an area to host a few git repositories and maybe a simple webpage.

所以我最近拿了一个旧桌面,在上面加载了 ubuntu server 12.04。我对这个项目的目标是创建一个区域来托管一些 git 存储库和一个简单的网页。

I've installed both the SSH and Apache2 packages and tried to configure them. Right now I can log in from my laptop using ssh [email protected] but only while I am on the same network. Once I leave the house I get a timeout error.

我已经安装了 SSH 和 Apache2 包并尝试配置它们。现在我可以使用 ssh [email protected] 从我的笔记本电脑登录,但只有当我在同一网络上时。一旦我离开房子,我就会收到超时错误。

So 2 things I would like to get at right now.

所以我现在想知道两件事。

  1. How to configure the SSH to allow me to access the server from outside the network.
  2. Get my host name on the server so logins will be greg@hostname instead of [email protected]

    (I've done sudo hostname and changed the etc/hostname file and no results)

  1. 如何配置 SSH 以允许我从网络外部访问服务器。
  2. 在服务器上获取我的主机名,因此登录名将是 greg@hostname 而不是 [email protected]

    (我已经完成了 sudo hostname 并更改了 etc/hostname 文件,但没有结果)

Thanks guys.

谢谢你们。

采纳答案by Ben

The timeout external to your home network occurs because the IP you specified will be routed elsewhere on external networks.

发生在您的家庭网络外部的超时是因为您指定的 IP 将被路由到外部网络的其他地方。

As others have indicated, you need to configure port forwarding on your router (external interface) to the SSH server. You can either use the standard port (22) or any alternative port (something above 1024). For the webserver you will need to set the port forwarding from port 80 on the external interface to your server and maybe port 443 if you want to include SSL/TLS connections.

正如其他人所指出的,您需要在路由器(外部接口)上配置端口转发到 SSH 服务器。您可以使用标准端口 (22) 或任何替代端口(1024 以上的端口)。对于网络服务器,您需要设置从外部接口上的端口 80 到您的服务器的端口转发,如果您想包括 SSL/TLS 连接,则可能需要设置端口 443。

I also recommend using the SSH config file (~/.ssh/config) to make it easier when making either an internal or external connection. Add something like this:

我还建议使用 SSH 配置文件 (~/.ssh/config) 以便在进行内部或外部连接时更容易。添加如下内容:

Host serverext
Hostname 1.2.3.4
User greg
Port 22

Host serverint
Hostname 192.168.1.10
User greg
Port 22

Change the Hostname for the serverext config to the IP address of your Internet connection. If you use a non-standard port for the external SSH connections then change the port field for serverext to match that.

将 serverext 配置的主机名更改为 Internet 连接的 IP 地址。如果您对外部 SSH 连接使用非标准端口,则更改 serverext 的端口字段以匹配该端口。

Change the Hostname for the serverint config to the internal IP address for that server on your network.

将 serverint 配置的主机名更改为网络上该服务器的内部 IP 地址。

Once this is configured, in conjunction with the port forwarding you will be able to use "ssh serverint" to connect to your server when you're at home and "ssh serverext" to connect to it when you're somewhere else.

配置完成后,结合端口转发,您在家时将能够使用“ssh serverint”连接到您的服务器,而当您在其他地方时使用“ssh serverext”连接到它。

It is possible to include advanced configuration options through the SSH configuration file, such as port tunneling and websocket connections. In the past I've done things like that and redirecting SSH connections through SOCKS proxy servers. I've even had it run through Tor, but it is very slow.

可以通过 SSH 配置文件包含高级配置选项,例如端口隧道和 websocket 连接。过去我做过类似的事情,并通过 SOCKS 代理服务器重定向 SSH 连接。我什至让它通过 Tor 运行,但它非常慢。