0Pricing
Linux Networking & TCP/IP for Developers · 课时

管理网络接口

使用 `ip` 等工具配置和管理网络接口,并了解持久化网络配置文件

管理网络接口 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are Network Interfaces?

Think of your computer's network interface as its "ID card" for communicating on a network. It's the hardware or software component that allows your device to connect to a network, whether wired (Ethernet) or wireless (Wi-Fi).

Every network interaction, like browsing a website or sending an email, goes through one of these interfaces.

Discovering Your Interfaces

The first step in managing interfaces is knowing which ones you have! The ip command is your primary tool in modern Linux systems. Use ip link show to list all network interfaces.

You'll often see interfaces like lo (loopback), eth0 (Ethernet), or wlan0 (Wi-Fi).

ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:00:00:01 brd ff:ff:ff:ff:ff:ff

Decoding Interface Details

Let's break down the ip link show output:

  • lo: The loopback interface, used for internal communication.
  • eth0: A common name for the first Ethernet adapter.
  • UP: Indicates the interface is active.
  • mtu: Maximum Transmission Unit, the largest packet size it can handle.
  • link/ether: Shows the MAC address, a unique hardware identifier.

Activating & Deactivating Interfaces

You can enable or disable network interfaces. Disabling an interface effectively takes it offline, preventing any network traffic through it. This is useful for troubleshooting or security.

Use ip link set dev [interface_name] up to enable and down to disable.

# Bring eth0 down
ip link set dev eth0 down

# Bring eth0 up
ip link set dev eth0 up

Checking Interface IP Addresses

While ip link show gives you physical layer info, you need ip addr show (or ip a for short) to see IP addresses assigned to interfaces. This command reveals Layer 3 (Network Layer) details.

An IP address allows your device to be located and communicate with other devices on a network.

ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:00:00:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
       valid_lft 86290sec preferred_lft 86290sec
    inet6 fe80::a00:27ff:fe00:1%eth0/64 scope link
       valid_lft forever preferred_lft forever

Assigning IP Addresses Temporarily

You can assign an IP address to an interface on the fly using ip addr add. This change is temporary and will be lost upon reboot or network service restart.

The format is ip addr add [IP_address]/[CIDR_prefix] dev [interface_name].

# Add IP 192.168.2.10/24 to eth0
ip addr add 192.168.2.10/24 dev eth0

# Verify it's added
ip addr show eth0

Removing Temporary IP Addresses

Just as you can add an IP address, you can also remove it. This is useful if you've made a mistake or need to reconfigure an interface's addressing.

Use ip addr del [IP_address]/[CIDR_prefix] dev [interface_name] to remove an address.

# Remove IP 192.168.2.10/24 from eth0
ip addr del 192.168.2.10/24 dev eth0

# Verify it's gone
ip addr show eth0

The Need for Permanent Settings

The ip commands we've used so far apply changes immediately, but these changes are not persistent. This means they will be lost when the system reboots or the network service restarts.

For a stable network setup, you need to save your configurations in specific files or through a network management service.

Making Changes Permanent

Linux distributions typically use configuration files to store network settings persistently. The exact location and format can vary, but common approaches include:

  • Debian/Ubuntu: /etc/network/interfaces
  • CentOS/RHEL: Files in /etc/sysconfig/network-scripts/
  • Modern systems: NetworkManager or Netplan configurations (e.g., .yaml files in /etc/netplan/)

These files are read at boot time or when network services are restarted.

Interface Management Check

You've learned how to inspect and temporarily modify network interfaces using the ip command. Let's test your understanding.

Recap: Managing Network Interfaces

In this lesson, you learned how to manage Linux network interfaces using the powerful ip command. We covered:

  • Listing interfaces with ip link show.
  • Activating/deactivating interfaces with ip link set.
  • Viewing and assigning temporary IP addresses with ip addr.
  • The importance of persistent configuration files for permanent changes.

Mastering these commands is crucial for any Linux network administrator or developer!

常见问题解答

「管理网络接口」课时是免费的吗?

是的 — 「管理网络接口」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

「管理网络接口」这节课中我会学到什么?

使用 `ip` 等工具配置和管理网络接口,并了解持久化网络配置文件 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Networking & TCP/IP for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「管理网络接口」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?

能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 管理网络接口
  2. 路由表与网关
  3. DNS 配置与解析
  4. 使用 nftables 配置 Linux 防火墙
← 返回 Linux Networking & TCP/IP for Developers