Skip to content
Advertisement

Display socket options

How I can see from shell what socket options are set? In particular I’m interesting to know if SO_BROADCAST is set?

Advertisement

Answer

You can use lsof(8). If PID is the process ID and FD is the file descriptor number of the socket you’re interested in, you can do this:

lsof -a -p PID -d FD -T f

To list all IPv4 sockets of a process:

lsof -a -p PID -i 4 -T f

This will print out the socket options with a SO=, among other information. Note that if no options are set, you’ll get the empty string, so you’ll see something like SO=PQLEN=0 etc. To test for SO_BROADCAST, just grep for the string SO_BROADCAST after the SO=, e.g.

if lsof -a -p PID -d FD -T f | grep -q 'SO=[^=]*SO_BROADCAST'; then
    # socket has SO_BROADCAST
else
    # it doesn't
fi
Advertisement