大网数据广告活动
服务器运维问答

On linux, is there a way to record the ports a program tries to communicate over?

· 52CE 我爱测

Problem : I am trying to determine which ports to open for specific programs I have a number of programs that need to access services on remote hosts, but I don't know which ports they are trying to use for the purpose. Rather th…

问题原文

原帖链接 · Steen · CC BY-SA 4.0

Problem: I am trying to determine which ports to open for specific programs

I have a number of programs that need to access services on remote hosts, but I don't know which ports they are trying to use for the purpose. Rather than opening the firewall up to 0-1024 (or 0-65k), I would like to identify which ports a program uses while it runs.

The first program to check is kinit. From the documentation I gather that I need to open port 88 for inbound udp traffic and port 88 for outbound tcp and udp traffic. But this does not seem to be enough, as the program responds with

kinit: Resource temporarily unavailable while getting initial credentials

Opening up all ports results in a correct authentication flow for kinit:

Using default cache: /tmp/krb5cc_16381
Using principal: *****@******.******.***
Password for *****@******.******.***:
Authenticated to Kerberos v5

I know about how to use lsof, netstat and ss for retrieving the open ports for processes that bind for longer duration of periods to ports, but especially kinit seems to elude this listing, even when used together with a tool such as watch.

Edit: The accepted answer immediately showed me the culprit: DNS;

strace -e trace=connect kinit <user>@<kdc>
connect(3, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("<kdc_ip>")}, 16) = 0

Opening this port fixed the problem with Kerberos

已采纳解答

原帖链接 · ilkhd · CC BY-SA 4.0

You could try strace:

strace -e trace=connect your_program with arguments

Here is an example output:

$ strace -e trace=connect ssh somehost -p 8081
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("127.0.1.1")}, 16) = 0
connect(3, {sa_family=AF_INET, sin_port=htons(8081), sin_addr=inet_addr(<host ip address>)}, 16) = 0
connect(4, {sa_family=AF_LOCAL, sun_path="/run/user/1000/keyring/ssh"}, 110) = 0

The lines you need are those with the sa_family=AF_INET.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-08-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。