linux如何查询ip和端口号
- Linux
- 2025-07-17
- 2307
ifconfig或
 ip addr查看IP地址,用`
Linux系统中,查询IP地址和端口号是常见的网络管理任务,以下是几种常用的方法来查询IP地址和端口号:
使用 ifconfig 命令
 
ifconfig 是一个传统的网络配置命令,可以用来查看和配置网络接口的IP地址。
ifconfig
输出示例:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fea4:6f7b  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:a4:6f:7b  txqueuelen 1000  (Ethernet)
        RX packets 123456  bytes 12345678 (12.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 123456  bytes 12345678 (12.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0 
在这个例子中,eth0 接口的IP地址是 168.1.100。
使用 ip 命令
 
ip 命令是 ifconfig 的现代替代品,功能更强大,语法更统一。
ip addr show
输出示例:
1: 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
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 08:00:27:a4:6f:7b brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
       valid_lft 86399sec preferred_lft 86399sec
    inet6 fe80::20c:29ff:fea4:6f7b/64 scope link
       valid_lft forever preferred_lft forever 
在这个例子中,eth0 接口的IP地址是 168.1.100。
使用 hostname 命令
 
hostname 命令可以显示系统的主机名和IP地址。
hostname -I
输出示例:
168.1.100
使用 nmcli 命令
 
nmcli 是NetworkManager的命令行工具,可以用来管理网络连接。
nmcli device show | grep 'IP4.ADDRESS'
输出示例:
IP4.ADDRESS[1]: 192.168.1.100/24
查询端口号
使用 netstat 命令
 
netstat 命令可以显示网络连接、路由表、接口统计等信息。

netstat -tuln
输出示例:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0: LISTEN 1234/sshd tcp 0 0 127.0.0.1:631 0.0.0.0: LISTEN 5678/cupsd
在这个例子中,sshd 服务监听在端口 22,cupsd 服务监听在端口 631。
使用 ss 命令
 
ss 命令是 netstat 的现代替代品,功能更强大,性能更好。
ss -tuln
输出示例:
State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :22 : LISTEN 0 128 127.0.0.1:631 :
在这个例子中,sshd 服务监听在端口 22,cupsd 服务监听在端口 631。
使用 lsof 命令
 
lsof 命令可以列出打开的文件,包括网络连接。
lsof -i -n -P
输出示例:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1234 root 3u IPv4 12345 0t0 TCP :22 (LISTEN) cupsd 5678 root 4u IPv4 56789 0t0 TCP 127.0.0.1:631 (LISTEN)
在这个例子中,sshd 服务监听在端口 22,cupsd 服务监听在端口 631。
使用 nmap 命令
 
nmap 是一个网络扫描工具,可以用来扫描主机上的开放端口。

nmap localhost
输出示例:
Starting Nmap 7.80 ( https://nmap.org ) at 2023-10-01 12:34 PDT Nmap scan report for localhost (127.0.0.1) Host is up (0.00011s latency). Other addresses for localhost (not scanned): ::1 PORT STATE SERVICE 22/tcp open ssh 631/tcp open ipp
在这个例子中,localhost 上开放的端口有 22 和 631。
使用 iptables 命令
 
iptables 是Linux内核防火墙工具,可以用来查看和管理网络规则。
iptables -L -n -v
输出示例:
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp - any any anywhere anywhere tcp dpt:22 0 0 ACCEPT tcp - any any anywhere anywhere tcp dpt:631
在这个例子中,iptables 规则允许 tcp 协议的 22 和 631 端口。
使用 systemctl 命令
 
systemctl 是系统服务管理器,可以用来查看和管理服务状态。
systemctl status sshd
输出示例:
● sshd.service OpenBSD Secure Shell Server
   Loaded: loaded (/usr/sbin/sshd; enabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/sshd.service.d
   └─50-redhat.conf
   Active: active (running) since Mon 2023-10-01 12:34:56 PDT; 1h ago
 Main PID: 1234 (sshd)
    Tasks: 1 (limit: 4915)
   Memory: 2.3M
   CGroup: /system.slice/sshd.service
           ├─1234 /usr/sbin/sshd -D
           └─2345 /usr/sbin/sshd -D (sshd-keygen) 
在这个例子中,sshd 服务正在运行,监听在默认端口 22。
使用 firewall-cmd 命令
 
firewall-cmd 是Firewalld的命令行工具,可以用来管理防火墙规则。

firewall-cmd --list-all
输出示例:
public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: ssh dhcpv6-client ports: 22/tcp 631/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
在这个例子中,public 区域允许 ssh 和 dhcpv6-client 服务,分别对应端口 22 和 631。
相关问答FAQs
Q1: 如何在Linux中查看所有网络接口的IP地址?
A1: 可以使用 ip addr show 或 ifconfig 命令来查看所有网络接口的IP地址。
ip addr show
或
ifconfig
Q2: 如何在Linux中查找特定服务的监听端口?
A2: 可以使用 netstat, ss, 或 lsof 命令来查找特定服务的监听端口,查找 sshd 服务的监听端口:
netstat -tuln | grep sshd
或
ss -tuln | grep sshd
或
 
  
			