Open a cache of riches with lsof
Ssssuper
Incidentally, for comparison, another open port lookup tool succinctly called ss
(apparently, socket statistics) only took 0.054 seconds (Listing 2).
Listing 2
Open Port Lookup Tool ss Output
The frisky systems tool ss is super-lightweight but examining its innards with an autopsy are for another day, so I'll get back to lsof as promised.
Clearly, I have a bias toward lsof, but in the next example netstat isn't too far behind in terms of output. Figure 2 shows the output for netstat -a
, which shows both listening and non-listening socket information, piped through grep
.
Grepping with ssh outputs the service name. Adding the -n
switch to netstat would show non-translated services and IP addresses. If you then grep for port 22, the output is far noisier than lsof, as Figure 3 shows.
The noisy output in Figure 3 helps demonstrate why I still always find myself turning to lsof. Admittedly, a combination of switches, with which I am as yet unfamiliar, could probably make netstat much quieter.
The Proof Is in the Pudding
Don't get me wrong; as I've already said, there's little doubt that if you just run lsof without adding switches, it's a pretty noisy little utility, too. (Try running the lsof
command to see ALL the open files on your system if you want proof.) Having looked at netstat for a moment, I'll now look at some exceptionally useful crime-fighting functionality, courtesy of the friendly neighbourhood lsof.
In Figure 1, you saw the output after asking lsof to check TCP port 22. Now I'll reverse engineer that process and imagine that I'm troubleshooting an SSH daemon problem. Suppose you've discovered a suspicious instance of SSH running on a port that you definitely didn't set up and that might be the result of a system compromise. Also imagine that port 22 is really 22222, for example. In such a scenario, the first step would be spotting SSH in the process table, as you see in Figure 4 using the ever-faithful ps -ef
to show process information with just the right level of detail.
Now that you're armed with the magic number (494 is the PID in this case), you can ask lsof to tell you what it knows about everything on the filesystem relating to that PID. And, boy, can it tell you some things (Figure 5). You should ignore the .gvfs
hidden file error at the top, which apparently relates to a bug in FUSE not allowing even the superuser to view the mountpoint located at /home/chris/.gvfs
(Figure 6).
Not only can you see in Figure 5 the TCP ports (the SSH daemon has both an IPv6 and an IPv4 instance) at the bottom, but the executable that fired up the daemon is also mentioned (third significant line down with the name /usr/sbin/sshd
). Finally – and this will hopefully appear more relevant later as I explain further – you can see all the open files relating to that process and the significant libraries.
A more relaxed way of displaying this information can be achieved by showing all processes executing a command called ssh
:
# lsof -c ssh
In Figure 7, the output is relatively verbose, hence the tiny font. I hope you can still make out the difference between this output and that in Figure 5.
The additional content is the result of running the command on an Ubuntu desktop, which is why ssh-agent, usually associated with X sessions and less commonly found on servers, is listed.
Less Is More
The formidable lsof just keeps on giving, and at a controlled level of detailed output that limits eye strain. I'll look at a few of the other options available now.
So far, I've looked at ports, PIDs, and process names, but what about per user information, which is the same as owner or, more accurately, who spawned the process? I have chosen the user daemon that, on a desktop as well as a server, should give some useful output. In this case, /var/spool/cron/atjobs
and /usr/sbin/atd
show that the atd
command is dutifully run by the user daemon (see Figure 8).
With the mention of the ever-popular cron
, you would be correct in thinking that atd
does indeed schedule tasks to run, as the cron daemon does. The main difference between the two is apparently that atd jobs just run once, and to re-use them, you have to start them again. Cron however just keeps on trucking, repeatedly, until told to stop re-running its scheduled tasks.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.