
確認した環境
- Windows
- Windows 11 Home 24H2
- macOS
- macOS Sequoia 15.4
- Linux
- Debian 12.10
コマンド比較表
操作 | Windows | macOS | Linux |
---|---|---|---|
TCPだけ | - 1 | lsof -nP -iTCP -sTCP:LISTEN | ss -lnt |
UDPだけ | netstat -anp UDP | findstr *:* | lsof -nP -iUDP | grep -v ‘\->’ | ss -lnu |
TCPでプロセスも込み | netstat -anop TCP | findstr LISTEN 2 | lsof -nP -iTCP -sTCP:LISTEN 3 | sudo ss -lntup |
特定ポートのみ | netstat -anp TCP | findstr LISTEN | findstr :1234 | lsof -nP -iTCP:11434 -sTCP:LISTEN | ss -ln | grep ‘:22 ' |
IPv4のTCPだけ | netstat -anp TCP | findstr LISTEN | lsof -nP -i4TCP -sTCP:LISTEN | ss -ln4t |
IPv6のTCPだけ | netstat -anp TCPv6 | findstr LISTEN | lsof -nP -i6TCP -sTCP:LISTEN | ss -ln6t |
コマンドの実行例
Windows
netstatコマンドオプション
- -a … LISTENポートを含む全てのポートを表示する
- -n … アドレスとポート番号を数値形式で表示する
- -o … プロセスIDも表示する
- -p … プロトコルを指定する
mDNSはリッスンしているかな?おや?プロセスが2つあるけど、何と何だろう?
PS C:\Users\nissy> netstat -anop UDP | findstr *:* | findstr :5353
UDP 0.0.0.0:5353 *:* 3268
UDP 0.0.0.0:5353 *:* 5308
UDP 0.0.0.0:5353 *:* 5308
UDP 0.0.0.0:5353 *:* 5308
UDP 0.0.0.0:5353 *:* 5308
1つはsvchost、DNS Clientサービスだな。
PS C:\Users\nissy> tasklist /FI "PID eq 3268"
イメージ名 PID セッション名 セッション# メモリ使用量
========================= ======== ================ =========== ============
svchost.exe 3268 Services 0 7,000 K
もう1つは… Google ChromeもmDNSポートをリッスンするのか。
PS C:\Users\nissy> tasklist /FI "PID eq 5308"
イメージ名 PID セッション名 セッション# メモリ使用量
========================= ======== ================ =========== ============
chrome.exe 5308 Console 1 261,556 K
macOS
lsofコマンドオプション
- -n … ホスト名を数値形式で表示する
- -P … ポート名を数値形式で表示する
- -i … IPバージョンやプロトコル、ポートを指定する
- -s … ‘:‘で区切ってプロトコルと状態を指定する
IPv4とIPv6のTCPでリッスンしているポートは何があるかな?
nissy@MacBook-Air ~ % lsof -nP -iTCP -sTCP:LISTEN
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rapportd 869 nissy 11u IPv4 0xd26f3e8651df967c 0t0 TCP *:59557 (LISTEN)
rapportd 869 nissy 12u IPv6 0xdcb54368dfe6bb8a 0t0 TCP *:59557 (LISTEN)
ControlCe 1091 nissy 8u IPv4 0xfcbc3d5c02aef903 0t0 TCP *:7000 (LISTEN)
ControlCe 1091 nissy 9u IPv6 0xa28da80bc315e9e1 0t0 TCP *:7000 (LISTEN)
ControlCe 1091 nissy 10u IPv4 0xfd5a49f5ed6f20c3 0t0 TCP *:5000 (LISTEN)
ControlCe 1091 nissy 11u IPv6 0xe4c2bdb39e1e5a7d 0t0 TCP *:5000 (LISTEN)
ollama 1752 nissy 3u IPv4 0x4fec4852fe4df559 0t0 TCP 127.0.0.1:11434 (LISTEN)
Google 40215 nissy 45u IPv6 0xfaecdcbae5652941 0t0 TCP [::1]:7679 (LISTEN)
ollama 52368 nissy 3u IPv4 0x4e8c6ab7e55f0e68 0t0 TCP 127.0.0.1:59619 (LISTEN)
PID 40215のGoogleって何だろ?Chromeは起動してないけど…
nissy@MacBook-Air ~ % ps -o comm -p 40215
COMM
/Applications/Google Drive.app/Contents/MacOS/Google Drive
なるほど、TCP:7679でリッスンしているのはGoogle Driveか。
Debian
ssコマンドオプション
- -l … リッスンポートに絞る
- -n … サービス名を数値形式で表示する
- -t … TCPに絞る
- -u … UDPに絞る
- -p … プロセスを表示する
IPv4とIPv6のUDPとTCPでリッスンしているヤツ全部表示
nissy@deb12:~$ ss -lntu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 0.0.0.0:59848 0.0.0.0:*
udp UNCONN 0 0 0.0.0.0:21027 0.0.0.0:*
udp UNCONN 0 0 0.0.0.0:46175 0.0.0.0:*
udp UNCONN 0 0 0.0.0.0:5353 0.0.0.0:*
udp UNCONN 0 0 0.0.0.0:111 0.0.0.0:*
udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:*
udp UNCONN 0 0 [::]:43155 [::]:*
udp UNCONN 0 0 [::]:21027 [::]:*
udp UNCONN 0 0 [::]:5353 [::]:*
udp UNCONN 0 0 *:22000 *:*
udp UNCONN 0 0 [::]:111 [::]:*
udp UNCONN 0 0 [::]:57534 [::]:*
udp UNCONN 0 0 [::1]:323 [::]:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 4096 0.0.0.0:111 0.0.0.0:*
tcp LISTEN 0 4096 0.0.0.0:3000 0.0.0.0:*
tcp LISTEN 0 4096 127.0.0.1:8384 0.0.0.0:*
tcp LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
tcp LISTEN 0 4096 127.0.0.1:85 0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*
tcp LISTEN 0 4096 [::]:111 [::]:*
tcp LISTEN 0 4096 *:3128 *:*
tcp LISTEN 0 4096 [::]:3000 [::]:*
tcp LISTEN 0 100 [::1]:25 [::]:*
tcp LISTEN 0 4096 *:22000 *:*
tcp LISTEN 0 4096 *:8006 *:*