网站搜索

如何在路由器上进行端口转发


摘要:要转发路由器上的端口,请登录路由器,找到“端口转发”部分,然后创建适用于您用来托管的设备的规则。您还应该为主机分配一个静态 IP 地址。

虽然现代 Wi-Fi 路由器会自动处理大部分功能,但某些应用程序会要求您在路由器设置中手动转发端口。幸运的是,如果您知道在哪里查找,转发路由器上的端口非常简单

什么是端口转发?

端口转发(或端口映射)允许来自互联网的外部流量连接到专用网络上的设备,例如计算机。

假设您想在您的计算机上为您的朋友托管一个 Minecraft 服务器。当他们尝试连接时,他们的流量必须发送到您网络上的正确计算机,并且他们的连接必须得到您的路由器的允许。您的路由器使用端口转发规则来确定应向哪台计算机发送与 Minecraft 服务器相关的流量。当然,不仅仅是游戏服务器——如果涉及互联网流量,就涉及到端口。

让我们看看它是如何发生的细节。

您的路由器如何处理请求和使用端口

这是一张简单的家庭网络图。云图标代表更大的互联网和您的公共或前向互联网协议 (IP) 地址。这个 IP 地址代表您来自外部世界的整个家庭——在某种程度上就像一个街道地址。

红色地址 192.1.168.1 是您网络中的路由器地址。其他地址都属于图像底部显示的计算机。如果您的公共 IP 地址类似于街道地址,请将内部 IP 地址想象成该街道地址的公寓号码。

该图提出了一个有趣的问题,您以前可能没有想过。来自互联网的所有信息如何到达网络内的正确设备?如果您在笔记本电脑上访问 windows-office.net,如果所有设备的面向公众的 IP 地址都相同,它如何最终出现在您的笔记本电脑上而不是您儿子的台式机上?

这要归功于称为网络地址转换 (NAT) 的神奇路由魔法。此功能发生在路由器级别,其中 NAT 充当交通警察,引导网络流量通过路由器,以便路由器后面的所有设备共享一个公共 IP 地址。由于 NAT,您家中的每个人都可以同时请求网站和其他 Internet 内容,并且这些内容都将传送到正确的设备。

那么端口从哪里进入这个过程呢?端口是早期网络计算的古老但有用的遗留物。过去,当计算机一次只能运行一个应用程序时,您所要做的就是将一台计算机指向网络上的另一台计算机以将它们连接起来,因为它们将运行相同的应用程序。一旦计算机变得复杂到可以运行多个应用程序,早期的计算机科学家就不得不努力解决确保应用程序连接到正确应用程序的问题。因此,端口诞生了。

一些端口具有特定的应用程序,这些应用程序是整个计算行业的标准。例如,当您获取网页时,它使用端口 80。接收计算机的软件知道端口 80 用于提供 http 文档,因此它在那里侦听并做出相应的响应。如果你通过不同的端口发送一个 http 请求——比如 143——Web 服务器将不会识别它,因为它没有在那里监听(尽管可能是其他东西,比如传统上使用该端口的 IMAP 电子邮件服务器)。

其他端口没有预先分配的用途,您可以随意使用它们。为避免干扰其他符合标准的应用程序,最好为这些备用配置使用更大的数字。例如,Plex Media Server 使用端口 32400,而 Minecraft 服务器使用 25565——这两个数字都属于这个“公平游戏”领域。

每个端口都可以通过 TCP 或 UDP 使用。 TCP,即传输控制协议,是最常用的协议。 UDP 或用户数据报协议在家庭应用程序中使用较少,但有一个主要例外:BitTorrent。根据正在监听的内容,它会期望以这些协议中的一个或另一个发出请求。

为什么需要转发端口

那么,为什么您需要转发端口呢?虽然一些应用程序利用 NAT 来设置自己的端口并为您处理所有配置,但仍有很多应用程序不这样做,在连接服务和应用程序时,您需要向路由器伸出援助之手.

在下图中,我们从一个简单的前提开始。您在世界某个地方使用笔记本电脑(IP 地址为 987.76.54.123),并且想要连接到您的家庭网络以访问一些文件。如果您只是将您的家庭 IP 地址 (123.45.67.891) 插入您正在使用的任何工具(例如 FTP 客户端或远程桌面应用程序),并且该工具没有利用我们刚才提到的那些高级路由器功能,你倒霉了。它不知道将您的请求发送到哪里,并且什么也不会发生。

顺便说一句,这是一项很棒的安全功能。如果有人连接到您的家庭网络但未连接到有效端口,您希望连接被拒绝。这就是路由器的防火墙元素在执行它的工作:拒绝不受欢迎的请求。然而,如果敲你的虚拟门的人是你,那么拒绝就不受欢迎了,我们需要做一些调整。

为了解决这个问题,你想告诉你的路由器“嘿:当我用这个程序访问你时,你需要将它发送到这个端口的这个设备”。有了这些说明,您的路由器将确保您可以访问家庭网络上正确的计算机和应用程序。

因此,在此示例中,当您外出使用笔记本电脑时,您可以使用不同的端口来发出请求。当您使用端口 22 访问家庭网络的 IP 地址时,您家中的路由器知道这应该转到网络内部的 192.168.1.100。然后,Linux 安装上的 SSH 守护程序将响应。同时,您可以通过端口 80 发出请求,您的路由器会将其发送到您在 192.168.1.150 上运行的网络服务器。或者,你可以尝试用 VNC 远程控制你妹妹的笔记本电脑,你的路由器会把你连接到你的笔记本电脑,地址是 192.168.1.200。通过这种方式,您可以轻松连接到您已为其设置端口转发规则的所有设备。

端口转发的用处还不止于此!为了清晰和方便,您甚至可以使用端口转发来更改现有服务的端口号。例如,假设您的家庭网络上运行着两台 Web 服务器,您希望其中一台易于访问(例如,您希望人们能够轻松找到它的天气服务器),而另一台 Web 服务器用于个人项目。

当您从面向公众的端口 80 访问您的家庭网络时,您可以告诉您的路由器将它发送到位于 192.168.1.150 的天气服务器上的端口 80,它将在端口 80 监听。但是,您可以告诉您的路由器当您通过端口 10,000 访问它时,它应该转到您的个人服务器 192.168.1.250 上的端口 80。这样,第二台计算机不必重新配置为使用不同的端口,但您仍然可以有效地管理流量 — 同时,通过将第一台 Web 服务器链接到端口 80,您可以让人们更轻松地访问您的前面提到的天气服务器项目。

现在我们知道端口转发是什么以及为什么我们可能想要使用它,让我们在深入实际配置之前先看一下有关端口转发的一些小注意事项。

配置路由器前的注意事项

在坐下来配置您的路由器之前,有几件事要牢记在心,并且提前运行它们可以保证减少挫败感。

为您的设备设置静态 IP 地址

首先,如果您将所有端口转发规则分配给具有由路由器的 DHCP 服务分配的动态 IP 地址的设备,那么您的所有端口转发规则都会崩溃。我们在这篇关于 DHCP 与静态 IP 地址分配的文章中深入探讨了 DHCP 是什么的细节,但我们将在此处为您提供快速摘要。

您的路由器有一个地址池,它保留这些地址仅用于在设备加入和离开网络时分发给它们。把它想象成当你到达一家餐馆时得到一个号码——你的笔记本电脑加入,砰,它获得 IP 地址 192.168.1.98。你的 iPhone 加入,砰,它获得地址 192.168.1.99。如果您将这些设备脱机一段时间或路由器重新启动,那么整个 IP 地址抽签会重新发生。

在正常情况下,这很好。你的 iPhone 不关心它有哪个内部 IP 地址。但是,如果您创建了一个端口转发规则,表明您的游戏服务器位于某个 IP 地址,然后路由器为其提供了一个新 IP 地址,则该规则将不起作用,并且没有人能够连接到您的游戏服务器。为了避免这种情况,您需要为要分配端口转发规则的每个网络设备分配一个静态 IP 地址。最好的方法是通过路由器。

了解您的 IP 地址(并设置动态 DNS 地址)

除了为网络内的相关设备使用静态 IP 分配外,您还想知道自己的外部 IP 地址——您可以在家庭网络上访问 whatismyip.com 找到它。尽管您可能会在几个月甚至一年多的时间内使用相同的公共 IP 地址,但您的公共 IP 地址可能会发生变化(除非您的互联网服务提供商明确为您提供了一个面向公众的静态 IP 地址)。换句话说,您不能依赖于在您使用的任何远程工具中输入您的数字 IP 地址(并且您不能依赖于将该 IP 地址提供给朋友)。

现在,虽然您每次离开家并打算离家工作时(或每次您的朋友要连接到您的 Minecraft 服务器等时)都要经历手动检查 IP 地址的麻烦,但这是一个很大的问题头痛。相反,我们强烈建议您设置一个动态 DNS 服务,它将允许您将(更改的)家庭 IP 地址链接到一个令人难忘的地址,例如 mysuperawesomeshomeserver.dynu.net。

注意本地防火墙

在路由器级别设置端口转发后,您可能还需要调整计算机上的防火墙规则。例如,多年来,我们收到了很多来自沮丧的父母的电子邮件,这些父母设置了端口转发,以便他们的孩子可以和他们的朋友一起玩 Minecraft。在几乎所有情况下,问题是尽管在路由器上正确设置了端口转发规则,但有人忽略了 Windows 防火墙请求,询问 Java 平台(运行 Minecraft)是否可以访问更大的互联网。

请注意,在运行本地防火墙和/或包含防火墙保护的防病毒软件的计算机上,您可能需要确认您设置的连接是否正常。

如何在路由器上设置端口转发

您可以在路由器上配置端口转发。现在您了解了基础知识,这很简单。

尽管我们很乐意为您拥有的路由器的精确型号提供准确的说明,但事实是每个路由器制造商都有自己的软件,而且该软件的外观甚至会因路由器型号而异。

通常,您会寻找一种叫做——您猜对了——“端口转发”的东西。您可能需要浏览不同的类别才能找到它,但如果您的路由器有任何好处,它应该在那里。除了它们拥有的任何桌面软件或界面之外,大多数路由器还提供应用程序。

第一步:找到路由器上的端口转发规则

我们不会试图捕捉所有变化,而是突出显示一些变化,让您了解菜单的外观,并鼓励您查找特定路由器的手册或在线帮助文件以查找细节。

为了进行比较,Xfinity 应用程序中 xFi 网关的端口转发菜单如下所示:

这是运行流行的第三方 DD-WRT 固件的 D-Link DIR-890L 上的端口转发菜单的样子:

如您所见,两个视图之间的复杂性差异很大。此外,菜单中的位置完全不同。因此,如果您使用手册或搜索查询查找设备的确切说明,它会非常有用。

找到菜单后,就可以设置实际规则了。

第二步:创建端口转发规则

在学习了所有关于端口转发、为您的家庭 IP 地址设置动态 DNS 以及所有其他相关工作之后,重要的一步 — 创建实际规则 — 几乎就像在公园里散步一样。在路由器的端口转发菜单中,我们将创建两个新的端口转发规则:一个用于 Subsonic 音乐服务器,一个用于我们刚刚设置的新 Minecraft 服务器。

尽管不同路由器软件的位置不同,但一般输入是相同的。几乎普遍地,您将命名端口转发规则。最好简单地命名服务器或服务是什么,然后在需要时附加它以清楚起见(例如,“Webserver”或“Webserver-Weather”,如果有多个)。还记得我们一开始讲的TCP/UDP协议吗?您还需要指定 TCP、UDP 或两者。有些人非常热衷于找出每个应用程序和服务使用的协议,并出于安全目的将它们完美匹配。我们将是第一个承认我们在这方面很懒惰的人,我们几乎总是只选择“两者”以节省时间。

一些路由器固件,包括我们在上面的屏幕截图中使用的更高级的 DD-WRT,将允许您指定一个“源”值,该值是出于安全目的限制端口转发到的 IP 地址列表。如果愿意,您可以使用此功能,但请注意,它会带来一系列令人头疼的问题,因为它假定远程用户(包括您不在家时和正在连接的朋友)拥有静态 IP 地址。

接下来,您需要输入外部端口。这是将在路由器上打开并面向互联网的端口。您可以在此处使用 1 到 65353 之间的任何数字,但实际上大多数较低的数字都被标准服务(如电子邮件和 Web 服务器)占用,而许多较高的数字分配给相当常见的应用程序。考虑到这一点,我们建议选择一个大于 5,000 的数字,并且为了更加安全,使用 Ctrl+F 搜索这一长长的 TCP/UDP 端口号列表,以确保您没有选择与您已经在使用的现有服务。

最后,输入设备的内部 IP 地址、您在该设备上的端口,并(如果适用)打开规则。不要忘记保存设置。

第三步:测试您的端口转发规则

检查端口转发是否有效的最明显方法是使用端口的例程进行连接(例如,让您的朋友将他们的 Minecraft 客户端连接到您的家庭服务器),但如果您不在家,这并不总是立即可用的解决方案从家里。

值得庆幸的是,YouGetSignal.com 上提供了一个方便的小端口检查器。我们可以通过让端口测试器尝试连接到它来测试我们的 Minecraft 服务器端口是否转发。插入您的 IP 地址和端口号,然后单击“检查”。

如上所示,您应该会收到一条消息,例如“端口 X 在 [您的 IP] 上打开”。如果报告端口已关闭,请仔细检查路由器端口转发菜单中的设置以及测试仪中的 IP 和端口数据。

使用 xFi 网关进行 Xfinity 端口转发

不幸的是,如果你有一个 xFi 网关,你就不能再在一个地方做所有事情了。 Xfinity 已将端口转发规则移至 Xfinity 应用程序,但您必须使用 Web 界面分配静态 IP 地址。
在网络浏览器中输入网关地址,登录到您的 xFi 网关。通常,地址将是 10.0.0.1 或 192.168.0.1,但不能保证。如果这两个地址之一不起作用,您始终可以手动找到调制解调器或路由器的 IP 地址。

登录后,前往连接的设备,在列表中查找您的服务器,然后单击“编辑”。

勾选“保留 IP”,然后单击“保存”。

选择您要为其制定规则的设备或本地 IP,然后选择一个端口并在 TCP、UDP 或 TCP/UDP 之间进行选择。然后点击“下一步”以完成端口转发规则。

端口转发的常见应用

端口转发的应用程序与端口一样多,但大多数情况下,您将使用它来设置远程访问、游戏服务器或媒体服务器。许多人需要为 Minecraft 服务器进行端口转发,或者设置 SSH 端口转发。这是这些类别中一些最流行的应用程序的快速参考图表。

Application Ports Protocol
Minecraft (Java) 25565 TCP/UDP
Minecraft (Bedrock) 19132-19133 TCP/UDP
Project Zomboid (PZ) 16261-16262 TCP/UDP
VNC 5900 TCP
SSH 22 TCP
Plex Media Server 32400 TCP

It is important to note that SSH uses port 22, and that port is specifically reserved for that use. Other applications (like Minecraft) have staked out such a strong claim they have functionally reserved their ports, though there isn’t anything officially requiring it. You may occasionally find that you have multiple things trying to use the same port. Remember, there are literally tens of thousands of different ports freely available to use, so just pick another one and use that instead.

But before you go opening up all sorts of ports, hosting every single service you can imagine, take some time to review your security practices. Most are fairly simple to get started with, and they can save you a huge headache later.

Security Precautions for Port Forwarding

If you’re port forwarding, you obviously intend for something to be accessible from the Internet. Any time you open up a port you increase your “attack surface.” It is always best to take some preventative measures to mitigate your risk. This isn’t an exhaustive list of things you can do to protect yourself — for that we’d need to write multiple novels — but it is a place to start.

Don’t Run Servers As Admin or Root

It doesn’t matter if you’re hosting a server on Windows, Linux, or any other operating system. Do not use the administrator or root account to host things that are exposed to the Internet. The administrative or root accounts have few (if any) restrictions placed on them. They can perform any operation on your system.

If there is some problem with the service you’re running — like a misconfiguration, bug, or an exploit — administrative or root access dramatically increases the amount of damage that can be done by a malicious attacker. It may even allow someone to compromise other devices attached to your network.

If you use a regular account you’re much less vulnerable — any attacker that gains access to your system will probably also need some kind of privilege escalation exploit to really cause harm.

Disable Root Login Over SSH

If you’re hosting on Linux, you should completely disable root login over SSH. The root user has unlimited access to everything on the system, which makes it a tempting target for would-be evildoers.

Additionally, there is really nothing to be gained by using it, since sudo allows users to execute commands as if they were the root user. Sudo permissions can even be modified on a user-by-user basis, so if you wanted to create a more restricted sudo account to perform basic server administration you could.

You Can Change Your Ports, But Don’t Rely On It

You’ll sometimes encounter the suggestion that you shouldn’t use the default ports for anything you’re hosting. The idea behind this is simple: If someone is scanning IP blocks for specific open ports they want to target, changing the port might reduce the chances that someone will attempt to access your server.

For example, you could change the SSH port from 22 to something like 7281.

Is that effective? Only sorta — it’ll certainly reduce the number of automated hits you take from script kiddies (amateur would-be hackers that use prebuilt software or scripts), and there will subsequently be fewer things in your logs to review. However, it won’t do anything to deter a serious targeted attack by someone knowledgeable.

Security through obscurity isn’t a guarantee, and you should never rely on it to keep your system safe.

Install Fail2Ban on Linux Servers

Fail2Ban is software designed to help secure your server against brute-force attacks. Fail2Ban can be configured to automatically reject connection attempts from any IP address that has tried and failed to log in to your server a certain number of times. Attackers can’t attempt to guess passwords more than a few times without being banned.

Fail2Ban can be set up with more complex behaviors too, so it is well worth learning if you plan on hosting on Linux.

Use Security Keys For SSH Whenever Possible

You should always pick a strong password for your administrative or root account, and any other account that you’ll log in to remotely. Fail2Ban and any other security measures you might enact will try to stop brute-force attacks, but they could fail. Use the strongest password possible.

If you’re using SSH, consider using SSH keys instead of a password. SSH keys are an example of public key cryptography — keys are generated in pairs, one public, and one private. The public key is placed on the computer that you’ll be remotely connecting to. You keep the other member of the pair, the private key, on your computer. When you try to connect, your private key is checked against the key on the server to provide authorization.

Windows, Linux, and MacOS all support SSH keys, so there isn’t much reason not to use them. SSH keys are more secure, and — once setup — every bit as convenient as a password.

Only Allow Connections from Whitelisted Addresses

You can also improve your security by limiting the connections that are allowed to your server. There are two basic ways to do this: a whitelist and a blacklist. A blacklist prohibits connections from specific people or applications. For example, if you know a hacker was attacking your Minecraft server, you might add their IP to a blacklist so it is always rejected. Alternatively, you can use a whitelist, which works the opposite way. Whitelists only allow pre-approved connections, and they can often be restricted to only allow access to a specific application or a specific port.

On Linux, use Universal Firewall (UFW) or FirewallD to create an OS-level whitelist. You can use whichever you prefer, though Debian distros (like Ubuntu) typically come with UFW, and RHEL distros (like Fedora) typically come with FirewallD. On Windows, open Windows Firewall and go to the “Inbound Traffic” tab to create a whitelist.

Individual applications you might host also often come with built-in whitelist functionality, as well. For example, you can add an IP to a Minecraft Server’s whitelist by modifying whitelist.json in the main server directory. The process varies significantly between applications though, and you’ll need to check your application’s documentation for the details.

RELATED: The Best Linux Distributions for Beginners

Consider Separating Your Local Area Network With a VLANs

Your home local area network (LAN) is typically a bit of a free-for-all. There is much less security between devices on a LAN than between a device on the Internet and a device on the LAN. The general assumption is that devices attached to your LAN are trusted devices, and that they don’t pose much of a security risk.

If you’re hosting an internet-facing service, however, that is not a safe assumption. If there is a fault in the service you’re hosting, or your other security practices, it is possible that an attacker can compromise your server and through it gain access to other devices on your local area network. It is potentially a huge security breach.

One solution is a Virtual LAN, or VLAN. A VLAN is a separate virtual local area network that is isolated — through software — from the “real” LAN that all of your other devices are on. You can limit exactly what kind of traffic is allowed to pass between the VLAN containing your internet-facing server and the VLAN that all of your normal devices are on. This creates a pretty effective barrier between your server and your other devices should a malicious attacker compromise your server. Setting up a VLAN can be a bit complicated, and the details will vary depending on your hardware. Not all consumer routers support VLANs either, so if you don’t see it, it probably isn’t there.
If your router doesn’t support VLANs, you have a few options. You can buy a new router that does support them, or you can add a managed network switch. Managed network switches start at about $30, so they’re probably the least expensive way to set up a VLAN at home if your current hardware doesn’t support it.

It’s a wee bit of a hassle to set up port forwarding, but as long as you assign a static IP address to the target device and set up a dynamic DNS server for your home IP address, it’s a task you only need to visit once to enjoy hassle free access to your network in the future.

The Best Wi-Fi Routers of 2023

ASUS AX6000 (RT-AX88U)

版权所有。 © Windows-Office.net • 2019-2024