网络地址转换(NAT)
探索不同类型的 NAT,了解其在节省 IPv4 地址和提供网络安全方面的作用
网络地址转换(NAT) 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Network Address Translation?
Welcome to the lesson on Network Address Translation (NAT)! NAT is a crucial technique used in computer networking, especially with IPv4.
Think of NAT as a clever translator for network addresses. It allows devices on a private network to communicate with the internet using a single public IP address.
Why NAT is Essential for IPv4
The main reason NAT became so important is the limited supply of IPv4 addresses. There simply aren't enough unique public IPv4 addresses for every device in the world.
- IPv4 Conservation: NAT helps conserve public IPv4 addresses by allowing many private devices to share one.
- Network Isolation: It also adds a layer of security by hiding the internal network structure from the outside world.
Private vs. Public IP Addresses
To understand NAT, you need to know about private and public IP addresses:
- Private IPs: Used within a local network (e.g., your home or office). They are not routable on the internet. Examples:
192.168.1.10,10.0.0.5. - Public IPs: Unique, globally routable addresses used on the internet. Your home router gets one from your Internet Service Provider (ISP).
NAT bridges these two worlds.
How NAT Works: The Core Idea
Imagine your router as a post office. When a letter (data packet) leaves your house (private network) for the outside world (internet), the post office changes the return address to its own (public IP).
When a reply comes back, the post office knows which house (private IP) to send it to. This is the basic idea behind NAT: translating addresses at the network boundary.
Source NAT (SNAT): Outbound Traffic
Source NAT (SNAT) is used for outbound connections, meaning when devices inside your private network want to reach the internet.
When a packet leaves your private network, the router changes the packet's source IP address from a private one to the router's public IP address. This is often called IP Masquerading.
SNAT Example: Accessing the Web
Here's how SNAT works when your laptop browses a website:
- Your laptop (
192.168.1.10) sends a request to a website. - Your router intercepts it, changes the source IP to its public IP (e.g.,
203.0.113.5), and sends it to the internet. - The website replies to
203.0.113.5. - Your router receives the reply and translates the destination IP back to
192.168.1.10, forwarding it to your laptop.
Destination NAT (DNAT): Inbound Traffic
Destination NAT (DNAT), also known as Port Forwarding, is used for inbound connections. It allows external devices to connect to specific services running on a private device.
When a packet arrives from the internet, the router changes the packet's destination IP address from its public IP to a specific private IP and port within the network.
DNAT Example: Hosting a Game Server
If you host a game server on your PC (192.168.1.100) and want friends to connect:
- Your friend connects to your router's public IP (e.g.,
203.0.113.5) on a specific port (e.g.,25565). - Your router has a DNAT rule: any traffic to
203.0.113.5:25565should be forwarded to192.168.1.100:25565. - The router translates the destination and sends the packet to your game server.
- The server's replies are then SNAT'd back out to your friend.
PAT: Port Address Translation
Most home routers use a type of NAT called Port Address Translation (PAT), also known as NAPT or IP Masquerading.
PAT allows multiple devices on a private network to share a single public IP address simultaneously. It does this by using different source port numbers for each outgoing connection to keep track of which internal device gets which incoming reply.
NAT's Security and Limitations
While NAT is great for address conservation and offers a basic security layer by hiding internal IPs, it's not a firewall:
- Security: Hides internal network topology, making direct attacks harder.
- Limitations: Can complicate peer-to-peer applications and some VPNs, as it breaks end-to-end connectivity. IPv6 aims to solve address scarcity without NAT.
Quick Check: Understanding NAT
Which type of NAT is primarily used to allow multiple internal devices to share a single public IP address when accessing the internet, often by modifying port numbers?
Recap: NAT's Role in Networking
We've explored Network Address Translation (NAT) and its vital role in modern networking. You now understand:
- Why NAT is crucial for conserving IPv4 addresses.
- The difference between private and public IP addresses.
- How Source NAT (SNAT) handles outbound traffic.
- How Destination NAT (DNAT) enables inbound access (port forwarding).
- The common use of Port Address Translation (PAT) to share a single public IP.
NAT is a fundamental concept that enables the internet as we know it today!
常见问题解答
「网络地址转换(NAT)」课时是免费的吗?
是的 — 「网络地址转换(NAT)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。
「网络地址转换(NAT)」这节课中我会学到什么?
探索不同类型的 NAT,了解其在节省 IPv4 地址和提供网络安全方面的作用 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Networking & TCP/IP for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「网络地址转换(NAT)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?
能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- IPv6 编址与概念
- 高级路由协议(RIP/OSPF)
- 网络地址转换(NAT)
- 深入理解子网划分与 CIDR