13

I want to know which ports are used by which processes in embedded-linux. Since it is simple embedded-linux, there are no network command lines such as netstat, lsof. (only basic command lines such as cat, cp, echo, etc exist).

A partial solution seems to be to use "cat /proc/net/tcp" and "cat /proc/net/udp" command lines. However, I am not sure the printed list from those command lines shows all ports in use, and the list does not show which process is binded to certain port.

Any comments would be appreciated.

1
  • I think this question is better suited to Unix & Linux
    – user591
    Commented May 20, 2014 at 7:45

2 Answers 2

19

You should be able to find all open ports in /proc/net/tcp and /proc/net/udp. Each of those files have an inode column, which can be used to find the process owning that socket.

Once you have an inode number, you can run an ls command such as ls -l /proc/*/fd/* | grep socket:.$INODE to find the processes using that socket. In case a process has been set up with different file descriptors for different threads, you may need to extend the command to ls -l /proc/*/task/*/fd/* | grep socket:.$INODE in order to find them all.

2
  • 1
    find /proc -lname "socket:\[$INODE\]" 2> /dev/null
    – Sammitch
    Commented May 21, 2014 at 23:14
  • worth noting ports can show up in HEX, eg: 2382 for 9090, 1F90 for 8080. Commented Oct 27, 2022 at 22:34
0

To find the INODES for any port execute below command

PORT=8080;cat /proc/net/* | awk -F " " '{print $2 ":" $10 }' | grep -i `printf "%x:" $PORT` | awk -F ":" '{print "PORT=" $2 ", INODE=" $3 }'

Use any INODE from above command output to find associated PID as below

find /proc -lname "socket:\[$INODE\]" 2> /dev/null | head -n 1 | awk -F "/" '{print "PID="$3}'

here $INODE is the value of any INODE

In a single line command, we can check, if any port is open and associated to any PID as below

PORT=8080;find /proc -lname "socket:\[$(cat /proc/net/* | awk -F " " '{print $2 ":" $10 }' | grep -i `printf "%x:" $PORT` | head -n 1 | awk -F ":" '{print $3}')\]" 2> /dev/null | head -n 1 | awk -F "/" '{print "PID="$3}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .