Linux_10 Useful “IP” Commands to Configure Network Interfaces

本文详细介绍如何在 Linux 系统中使用 ip 命令配置静态 IP 地址、默认网关及静态路由等网络参数。文章涵盖临时配置与持久化设置方法,并提供针对不同发行版的具体操作步骤。

via: http://www.tecmint.com/ip-command-examples/

 

In this post, we are going to review how we can assign Static IP AddressStatic RouteDefault Gateway etc.  Assigning IP Address on demand using IP command. IFCONFIG command is deprecated and replaced by IP command in Linux. However, IFCONFIG command is still works and available for most of the Linux distributions.

 

Note: Please take configuration file backup before doing any changes.

How do i Configure Static IP Address Internet Protocol (IPv4)

To configure static IP Address, you need to update or edit network configuration file to assign an Static IP Address to a system. You must be superuser with su (switch user) command from terminal or command prompt.

For RHEL/CentOS/Fedora

Open and edit network configuration file for (eth0 or eth1) using your favorite editor. For example, to assigning IP Address to eth0 interface as follows.

[root@tecmint ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
Simple output:
DEVICE="eth0"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=192.168.50.2
NAME="System eth0"
HWADDR=00:0C:29:28:FD:4C
GATEWAY=192.168.50.1
For Ubuntu/Debian/Linux Mint

Assign Static IP Address to eth0 interface editing configuration file /etc/network/interfaces to make permanent changes as shown below.

auto eth0
iface eth0 inet static
address 192.168.50.2
netmask 255.255.255.0
gateway 192.168.50.1

Next, restart network services after entering all the details using the following command.

# /etc/init.d/networking restart
$ sudo /etc/init.d/networking restart

1. How to Assign a IP Address to Specific Interface

The following command used to assign IP Address to a specific interface (eth1) on the fly.

# ip addr add 192.168.50.5 dev eth1
$ sudo ip addr add 192.168.50.5 dev eth1

Note: Unfortunately all these settings will be lost after a system restart.

2. How to Check an IP Address

To get the depth information of your network interfaces like IP Address, MAC Address information, use the following command as shown below.

# ip addr show
$ sudo ip addr show
Sample Output
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:0c:29:28:fd:4c brd ff:ff:ff:ff:ff:ff
    inet 192.168.50.2/24 brd 192.168.50.255 scope global eth0
    inet6 fe80::20c:29ff:fe28:fd4c/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:0c:29:28:fd:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.50.5/24 scope global eth1
    inet6 fe80::20c:29ff:fe28:fd56/64 scope link
       valid_lft forever preferred_lft forever

3. How to Remove an IP Address

The following command will remove an assigned IP address from the given interface (eth1).

# ip addr del 192.168.50.5/24 dev eth1
$ sudo ip addr del 192.168.50.5/24 dev eth1

4. How to Enable Network Interface

The “up” flag with interface name (eth1) enables a network interface. For example, the following command will activates the eth1 network interface.

# ip link set eth1 up
$ sudo ip link set eth1 up

5. How to Disable Network Interface

The “down” flag with interface name (eth1) disables a network interface. For example, the following command will De-activates the eth1 network interface.

# ip link set eth1 down
$ sudo ip link set eth1 down

6. How do I Check Route Table?

Type the following command to check the routing table information of system.

# ip route show
$ sudo ip route show
Sample Output
10.10.20.0/24 via 192.168.50.100 dev eth0
192.168.160.0/24 dev eth1  proto kernel  scope link  src 192.168.160.130  metric 1
192.168.50.0/24 dev eth0  proto kernel  scope link  src 192.168.50.2
169.254.0.0/16 dev eth0  scope link  metric 1002
default via 192.168.50.1 dev eth0  proto static

7. How do I Add Static Route

Why you need to add Static routes or Manual routes, because that the traffic must not pass through the default gateway. We need to add Static routes to pass traffic from best way to reach the destination.

# ip route add 10.10.20.0/24 via 192.168.50.100 dev eth0
$ sudo ip route add 10.10.20.0/24 via 192.168.50.100 dev eth0

8. How to Remove Static Route

To remove assigned static route, simply type the following command.

# ip route del 10.10.20.0/24
$ sudo ip route del 10.10.20.0/24

9. How do I Add Persistence Static Routes

All the above route will be lost after a system restart. To add permanent Static route, edit file/etc/sysconfig/network-scripts/route-eth0 (We are storing static route for (eth0) and add the following lines and save and exist. By default route-eth0 file will not be there, need to be created.

For RHEL/CentOS/Fedora
# vi /etc/sysconfig/network-scripts/route-eth0
10.10.20.0/24 via 192.168.50.100 dev eth0
For Ubuntu/Debian/Linux Mint

Open the file /etc/network/interfaces and at the end add the persistence Static routes. IP Addresses may differ in your environment.

$ sudo vi /etc/network/interfaces
auto eth0
iface eth0 inet static
address 192.168.50.2
netmask 255.255.255.0
gateway 192.168.50.100
#########{Static Route}###########
up ip route add 10.10.20.0/24 via 192.168.50.100 dev eth0

Next, restart network services after entering all the details using the following command.

# /etc/init.d/network restart
$ sudo /etc/init.d/network restart

10. How do I Add Default Gateway

Default gateway can be specified globally or for in interface-specific config file. Advantage of default gateway is If we have more than one NIC is present in the system. You can add default gateway on the fly as shown below command.

# ip route add default via 192.168.50.100
$ sudo ip route add default via 192.168.50.100

Kindly correct me if i missed out. Please refer manual page doing man ip from terminal/command prompt to know more about IP Command.

<think>我们正在处理一个关于在Linux系统中配置或增加回环网络接口(lo)的问题。 根据Linux知识,回环接口(lo)在Linux系统中默认已经存在,通常不需要手动添加。但用户可能想要配置它(如设置IP地址等)。 因此,我们的回答应该集中在配置回环接口上,而不是添加(因为默认存在)。如果用户确实需要“添加”,我们可以解释它已经存在,但可以配置多个IP地址。 步骤: 1. 检查现有的回环接口:使用命令`ifconfig`或`ip addr show lo` 2. 配置回环接口:通常可以通过修改网络配置文件或使用临时命令(重启后失效)来实现。 临时配置(重启后失效): - 使用ifconfig(较老,可能在一些系统中已被弃用): sudo ifconfig lo:0 127.0.0.2 netmask 255.0.0.0 up - 使用ip命令(推荐): sudo ip addr add 127.0.0.2/8 dev lo label lo:0 永久配置(不同Linux发行版配置文件可能不同): - 在Debian/Ubuntu系统中,可以编辑/etc/network/interfaces文件,添加: auto lo:0 iface lo:0 inet static address 127.0.0.2 netmask 255.0.0.0 - 在RedHat/CentOS系统中,可以创建文件/etc/sysconfig/network-scripts/ifcfg-lo:0,内容为: DEVICE=lo:0 IPADDR=127.0.0.2 NETMASK=255.0.0.0 ONBOOT=yes 注意:回环接口的地址通常以127开头,并且子网掩码为255.0.0.0(即/8)。127.0.0.1是默认的,我们可以添加多个。 另外,我们也可以使用回环接口的别名(如lo:0, lo:1等)来添加多个IP地址。 但是,用户可能只是需要配置基本的回环接口,所以我们可以从基本配置开始。 同时,我们需要注意,回环接口是系统自动创建的,用户不能通过常规方式“添加”一个名为lo的接口(因为已经存在),但可以添加别名。 因此,回答将围绕如何配置回环接口(包括添加额外的IP地址)展开。 另外,根据引用[1]中提到了ifconfig命令,我们可以使用ifconfig来临时配置。但引用[1]是一篇关于ifconfig命令的文章,我们可以参考它。 但是,引用[2]是关于HSRP(思科路由器)的配置,与当前问题无关,因此忽略。 所以,我们的回答将包括: 1. 检查回环接口状态 2. 临时配置(添加IP地址) 3. 永久配置 最后,生成相关问题。 注意:使用中文回答。</think>### 在 Linux 中配置回环接口 (lo) 回环接口 (lo) 是 Linux 系统默认创建的逻辑网络接口,用于本地通信(如访问本机服务)。它通常已自动配置,但可通过以下方式管理: --- #### 1. **查看当前配置** ```bash ip addr show lo # 推荐(现代命令) ifconfig lo # 传统命令(部分系统需安装 net-tools) ``` 输出示例: ``` 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever ``` --- #### 2. **临时添加额外 IP 地址** ```bash sudo ip addr add 127.0.0.2/8 dev lo # 添加新地址 sudo ip addr add 127.0.0.3/8 dev lo # 可添加多个 ``` 验证: ```bash ping 127.0.0.2 # 测试新地址 ``` --- #### 3. **永久配置(不同发行版)** **Debian/Ubuntu** 编辑 `/etc/network/interfaces`: ```bash auto lo iface lo inet loopback up ip addr add 127.0.0.2/8 dev lo # 添加永久地址 ``` **RHEL/CentOS** 创建配置文件 `/etc/sysconfig/network-scripts/ifcfg-lo:0`: ```bash DEVICE=lo:0 IPADDR=127.0.0.2 NETMASK=255.0.0.0 ONBOOT=yes ``` 重启网络服务生效: ```bash sudo systemctl restart NetworkManager # 或 network.service ``` --- #### 4. **关键注意事项** - 回环接口默认已存在,无需手动"添加"物理接口。 - 地址范围:`127.0.0.0/8`(127.x.x.x)。 - 永久修改需编辑配置文件,临时命令重启失效。 - 通过 `ip` 命令管理是当前最佳实践[^1]。 --- #### **相关问题** 1. 如何测试回环接口的网络连通性? 2. Linux 中回环接口与物理接口有何本质区别? 3. 如何用 `netstat` 命令监控回环接口的流量? [^1]: Useful "ifconfig" Commands to Configure Network Interface in Linux. This article covers "15 Useful 'ifconfig' Commands" with their practical examples. [^2]: R2-Inside#sh standby brie. P indicates configured to preempt. | Interface Grp Prio P State Active Standby Virtual IP.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值