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
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4