When using ss with -p option, user/pid/fd column jumps underneath the particular line. For instance this is it what I’m actually seeing:
# ss -nulp4
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 *:20000 *:*
users:(("perl",pid=9316,fd=6))
UNCONN 0 0 *:10000 *:*
users:(("perl",pid=9277,fd=6))
UNCONN 0 0 192.168.100.10:53 *:*
users:(("named",pid=95,fd=517),("named",pid=95,fd=516))
UNCONN 0 0 127.0.0.1:53 *:*
users:(("named",pid=95,fd=515),("named",pid=95,fd=514))
Preferred output formatting:
# ss -nulp4
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 *:20000 *:* users:(("perl",pid=9316,fd=6))
UNCONN 0 0 *:10000 *:* users:(("perl",pid=9277,fd=6))
UNCONN 0 0 192.168.100.10:53 *:* users:(("named",pid=95,fd=517),("named",pid=95,fd=516))
UNCONN 0 0 127.0.0.1:53 *:* users:(("named",pid=95,fd=515),("named",pid=95,fd=514))
To confirm that there are no line breaks I’ve tried this:
# ss -nulp4 | cat -A
State Recv-Q Send-Q Local Address:Port Peer Address:Port $
UNCONN 0 0 *:20000 *:* users:(("perl",pid=9316,fd=6))$
UNCONN 0 0 *:10000 *:* users:(("perl",pid=9277,fd=6))$
UNCONN 0 0 192.168.100.10:53 *:* users:(("named",pid=95,fd=517),("named",pid=95,fd=516))$
UNCONN 0 0 127.0.0.1:53 *:* users:(("named",pid=95,fd=515),("named",pid=95,fd=514))$
And indeed you can see that there were none, but now, strangely enough, output format is the way I’ve wanted it to be. Could someone explain what’s going on here? How can I achieve my preferred formatting?
This is the only thing stopping me from migrating from netstat to ss.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Following one might be helpful to change output:
ss -ltunp | column -t
Method 2
As for why etc.
ss, part of the iproute2 utility collection in the Linux kernel, uses an ioctl() request to get the current width of terminal.
However; the entire width is used for the «other» fields and the process field get squeezed onto next line.
You can view this by for example (when having a limited with on terminal):
script ss.txt ss -nlup4 exit
Then widen your terminal window and cat ss.txt.
The reason why
ss -nulp4 | cat -A
«works» is because the utility recognizes if it writes to a tty or not:
if (isatty(STDOUT_FILENO)) {
}
As you can see from the prior line in the source code default width is set to 80. Thus if your terminal is at say 130 columns and you do:
ss -nulp4 | cat
it recognizes the output is not to a tty (but to a pipe) and the other fields are crammed into 80 columns, whilst the process field is written after these 80 columns. But as your terminal is wider then 80 columns and has room for the process entry it is displayed in one line.
The same goes for for example:
ss -nulp4 > ss.txt
As for how to «achieve my preferred formatting» one likely unsuitable way is to do something in the direction of (depending on terminal):
stty cols 100 ss -nlup4
Method 3
To get output format in order, you can try this
sudo ss -tulnp | sort -k7 | awk "{print substr($7,1,70),$1,$5}" | column -t
this will sort the output by progam name and in table format.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0