ตารางเส้นทางและเกตเวย์
เรียนรู้การตรวจสอบและปรับเปลี่ยนตารางเส้นทางของ Linux เพื่อควบคุมการส่งต่อทราฟฟิกเครือข่ายไปยังปลายทาง
ตารางเส้นทางและเกตเวย์ เป็นบทเรียน Linux Networking & TCP/IP for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Networking & TCP/IP for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Networking & TCP/IP for Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What are Routing Tables?
Imagine your computer needs to send data to another device on a different network. How does it know which path to take?
That's where the routing table comes in! It's like a map for your operating system, containing rules that tell it where to forward network traffic.
- Each rule specifies a destination network or host.
- It also points to the 'next hop' or interface to use.
Inspecting Your Routing Table
In Linux, the primary command to view the routing table is ip route show (or just ip route). Let's see what it looks like:
ip route showUnderstanding Route Output
When you run ip route show, you'll see entries that typically include:
- Destination: The network or host traffic is headed to.
- via: The gateway (next hop router) to send traffic through.
- dev: The local network interface to use (e.g., eth0, wlan0).
- src: The source IP address your system will use for outgoing packets.
- metric: A preference value; lower is generally preferred.
The Default Gateway
One crucial entry in the routing table is the default route. It's often shown as default via <IP> dev <interface>.
This is your network's 'gateway of last resort'. If no other specific route matches the destination IP, traffic is sent to this default gateway. This is usually your router that connects you to the internet.
ip route show defaultAdding a Static Route
You can manually add routes to the routing table. This is useful for directing traffic to specific networks that aren't reachable via the default route, or for creating isolated network segments.
Use ip route add to add a temporary static route. Remember, sudo is often required.
sudo ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
# This adds a route for 192.168.2.0/24
# via gateway 192.168.1.1 on eth0Verifying the New Route
After adding a route, it's good practice to verify that it's present in the routing table. Just run ip route show again and look for your newly added entry.
ip route showDeleting a Static Route
Just as you can add routes, you can also remove them. This is useful for correcting mistakes or cleaning up temporary configurations.
Use ip route del with the exact same parameters you used to add the route.
sudo ip route del 192.168.2.0/24 via 192.168.1.1 dev eth0
# Removes the route we just addedGateways: The Next Hop
A gateway is a network node that serves as an access point to another network. It's essentially a router that forwards packets between different networks.
When your computer sends data to a destination outside its immediate local network, it sends the data to its configured gateway, which then handles the forwarding.
Temporary vs. Permanent Routes
Routes added with the ip route command are temporary. This means they will be lost if your network service restarts or if the system reboots.
For permanent routes, you need to configure them in system-specific network configuration files. The exact location depends on your Linux distribution (e.g., files under /etc/netplan/, /etc/network/interfaces, or in network scripts).
Routing Table Challenge
You just learned how to inspect and manipulate routing tables. Let's test your understanding!
Recap: Routing Essentials
Great job! You've learned the basics of Linux routing tables and gateways.
- Routing tables guide network traffic.
- Use
ip route showto inspect them. - The default route is for unknown destinations.
- You can temporarily add and delete routes with
ip route addandip route del. - Gateways are next-hop devices for reaching other networks.
Understanding routing is key to troubleshooting network connectivity!
คำถามที่พบบ่อย
บทเรียน “ตารางเส้นทางและเกตเวย์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตารางเส้นทางและเกตเวย์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Networking & TCP/IP for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Networking & TCP/IP for Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตารางเส้นทางและเกตเวย์”
เรียนรู้การตรวจสอบและปรับเปลี่ยนตารางเส้นทางของ Linux เพื่อควบคุมการส่งต่อทราฟฟิกเครือข่ายไปยังปลายทาง คุณปฏิบัติ Linux Networking & TCP/IP for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Networking & TCP/IP for Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Networking & TCP/IP for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ตารางเส้นทางและเกตเวย์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Linux Networking & TCP/IP for Developers นี้ได้ไหม
ได้ บทเรียน Linux Networking & TCP/IP for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดการส่วนติดต่อเครือข่าย
- ตารางเส้นทางและเกตเวย์
- การกำหนดค่าและการแปลงชื่อ DNS
- การกำหนดค่าไฟร์วอลล์ Linux ด้วย nftables