快速入门指南:如何在Linux中查找IP地址

这篇文章是关于如何在Linux中获取IP地址,子网和相关的网络信息。

Ifconfig-Linux命令:

在Linux上,要检查或找出IP地址,必须使用“ / sbin / ifconfig”命令。 MAC地址,子网掩码和其他信息也会显示。

输出例子

[[email protected] ~]# /sbin/ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500                                                                                                    
        inet 213.159.209.228  netmask 255.255.254.0  broadcast 213.159.209.255                                                                                
        inet6 fe80::5054:ff:fe80:19a4  prefixlen 64  scopeid 0x20<link>                                                                                       
        ether 52:54:00:80:19:a4  txqueuelen 1000  (Ethernet)                                                                                                  
        RX packets 306877603  bytes 20085789908 (18.7 GiB)                                                                                                    
        RX errors 0  dropped 0  overruns 0  frame 0                                                                                                           
        TX packets 14979556  bytes 128723797704 (119.8 GiB)                                                                                                   
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0                                                                                            
                                                                                                                                                              
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536                                                                                                                  
        inet 127.0.0.1  netmask 255.0.0.0                                                                                                                     
        inet6 ::1  prefixlen 128  scopeid 0x10<host>                                                                                                          
        loop  txqueuelen 1  (Local Loopback)                                                                                                                  
        RX packets 10034610  bytes 35020478823 (32.6 GiB)                                                                                                     
        RX errors 0  dropped 0  overruns 0  frame 0                                                                                                           
        TX packets 10034610  bytes 35020478823 (32.6 GiB)                                                                                                     
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0 
[[email protected] ~]#

仅获取IP地址:

[[email protected] ~]# /sbin/ifconfig|grep -v "127.0.0.1"|grep -i "inet addr"
          inet addr:213.159.209.228  Bcast:213.159.209.255  Mask:255.255.255.0
[[email protected] ~]#

根据以上结论,“192.168.49.180“是当前分配给该接口的Linux系统的IP地址 eth0

如果您有多个接口卡或以太网卡,例如eth0,eth1…ETHN。 然后,要获取特定以太网的IP地址,您需要在IFCONFIG命令中指定,如下所示。

[[email protected] ~]# /sbin/ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500                                                                                                    
        inet 213.159.209.228  netmask 255.255.254.0  broadcast 213.159.209.255                                                                                
        inet6 fe80::5054:ff:fe80:19a4  prefixlen 64  scopeid 0x20<link>                                                                                       
        ether 52:54:00:80:19:a4  txqueuelen 1000  (Ethernet)                                                                                                  
        RX packets 306923321  bytes 20088708252 (18.7 GiB)                                                                                                    
        RX errors 0  dropped 0  overruns 0  frame 0                                                                                                           
        TX packets 14980805  bytes 128725514772 (119.8 GiB)                                                                                                   
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[[email protected] ~]#

ip addr show-Linux中的命令以获取IP地址:

根据建议,我们应该在Linux中使用ip addr show获取IP地址。

[[email protected] ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:50:5b:ab:70:ba brd ff:ff:ff:ff:ff:ff
    inet 192.168.49.180/22 brd 192.168.51.255 scope global eth0

ip rl-Linux中的命令以获取IP地址:

Linux中有一个易于使用的命令来获取或查找IP地址,如下所示:

[[email protected] ~]# ip r l
213.159.209.0/22 dev eth0  proto kernel  scope link  src 213.159.209.228
default via 213.159.209.1 dev eth0
[[email protected] ~]#

Sidebar