在Linux系统中,查看IP地址的命令有很多,这里我们介绍几个常用的命令:
1、ifconfig
2、ip addr
3、hostname -I
4、ifupdown
5、netstat -tunlp
6、route -n
7、nslookup
8、dig
9、arp -a
10、getent hosts
接下来,我们将详细介绍这些命令的用法和注意事项。
ifconfig
(interface configuration)是一个用于显示和配置网络接口的命令行工具,在较新的Linux发行版中,这个命令已经被弃用,建议使用ip
命令替代,但是在一些旧版本的Linux系统中,仍然可以使用ifconfig
命令查看IP地址,使用方法如下:
ifconfig
输出结果示例:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::a00:27ff:fe3f:8c6d prefixlen 64 scopeid 0x20<link> ether 08:00:27:3f:8c:6d txqueuelen 1000 (Ethernet) RX packets 123456 bytes 123456789 (123.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 987654 bytes 987654567 (987.6 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
在这个例子中,可以看到网络接口eth0
的IP地址是`192.168.1.2`。
ip
(internet protocol)是一个用于显示和操作IPv4和IPv6地址、路由、设备、策略等网络相关信息的命令行工具,它是取代了过时的ifconfig
和route
命令的新工具,使用方法如下:
ip addr show
2: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3: enp2s0f0@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether ac:bc:de:ef:gh:ij brd ff:ff:ff:ff:ff:ff 4: enp3s0f0@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether ac:bc:de:ef:gh:kl brd ff:ff:ff:ff:ff:ff
在这个例子中,可以看到网络接口的IP地址分别是`127.0.0.1`(回环地址)、`192.168.1.2`(enp2s0f0@if3)、`192.168.4.2`(enp3s0f0@if4),需要注意的是,这里的IP地址并不是实际的网络IP地址,而是本地回环地址或者虚拟接口的IP地址,如果要查看实际的网络IP地址,可以使用以下命令:
ip route show | grep default | grep via | awk '{print $5}'
这个命令用于显示主机名对应的IP地址,使用方法如下:
hostname -I
输出结果示例(具体结果可能因系统而异):
```bash
192.168.1.2 enp2s0f0 @ INET[192.168.1.2] iface enp2s0f0 inet static address 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255 metric 10 preferred_metric 10 dynamic host enp2s0f0 version V4L/IFINFO_LINUX_ANY version V4L/IFINFO_LINUX (linux) device enp2s0f0 macaddress ac:bc:de:ef:gh:ij type LinuxCNC model unknown uptime about a week since Wed Jun 3rd at around noon last reboot booted from local disk driver loaded version v4l/vfw/videobuf module parameters none servername none user root group root tools none options resolvconf/mdns enabled protocols all interfaces autostart off source network source list file=/etc/network/interfaces description Unnamed interface connected to the Internet and the local network using DHCP or static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IP addresses description Unnamed interface connected to the local network using static IPAddresses
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/108636.html