- lsof: command not found
- Frequently used lsof options
- lsof Command Examples
- Final Words
- Some more articles you might also be interested in …
- lsof command not found on CentOS 7 / RHEL 7 Server
- How to download and install lsof rpm on CentOS 7 / RHEL 7 server
- How to Install lsof Command on Ubuntu 21.04 Server
- Steps to download lsof.deb package on Ubuntu 21.04 Server
- How to Install lsof Command on CentOS/RHEL 7 Servers
- How to Install lsof Command on CentOS/RHEL 7 Servers
- How to download and install lsof rpm on CentOS 7 / RHEL 7 server
- Commands not found: netstat, nmap on CentOS 7
- EDIT
- Use repoquery
- Use yum search
- netstat’s deprecation
lsof: command not found
At times, we face situations where we cannot unmount a disk as it says that the files are being used, but we cannot understand which file it is referring to. In such situations, we can check which files are open by which process is running on the system.
This can be done using the lsof command, which means List Open Files. Since Linux considers everything, including directories, devices, sockets, and so on as files, we can use lsof to easily identify all open files.
If we just run lsof, it will list all of the open files that belong to any active process on the system. If the output is long, we can use the less command to scroll through the output:
The output shown has columns such as Command, PID, User, FD, Type, Device, and so on, for a better understanding of the files. The FD column has information about the file’s description, such as the Current Working Directory (CWD), Root Directory (RTD), Program Text (TXT), and so on. If the FD column contains information like 0u, 1u, and so on, the number signifies the actual file descriptor and the alphabet signifies the different modes (read access, write access, and read/write access).
if you enconter the below error while running the lsof command:
ypu may install the below package as per your choice of distribution.
Distribution | Command |
---|---|
OS X | brew install lsof |
Debian | apt-get install lsof |
Ubuntu | apt-get install lsof |
Alpine | apk add lsof |
Arch Linux | pacman -S lsof |
Kali Linux | apt-get install lsof |
CentOS | yum install lsof |
Fedora | dnf install lsof |
Raspbian | apt-get install lsof |
Frequently used lsof options
- -c x: Only show files that are open by processes whose executable starts with the character(s) specified by x.
- -i x: Instead of showing open files, show sockets whose Internet address is x. If x is not specified, show all IP connections. This is functionally equivalent to netstat –anp.
- -u username: Show only the files that username has open.
- -P: Do not convert port numbers to port names (for example, show 25 instead of smtp).
lsof Command Examples
Simply running the lsof commands gives us a list of all the open files on the system. By using the -u option and specifying the username, we get a list of open files for a particular user. When we use the -i option and specify a port number, we get information about any process running on that port. When we use both the -i and -u options with a particular username, we get information about the files and commands being accessed by that user.
1. Find the processes that have a given file open:
2. Find the process that opened a local internet port:
3. Only output the process ID (PID):
4. List files opened by the given user:
5. List files opened by the given command or process:
# lsof -c process_or_command_name
6. List files opened by a specific process, given its PID:
7. List open files in a directory:
8. Find the process that is listening on a local IPv6 TCP port and don’t convert network or port numbers:
# lsof -i6TCP:port -sTCP:LISTEN -n -P
9. List processes that have your home directory opened:
10. List all open files in your home directory:
11. List the files opened by processes whose names begin with “i” and whose owner is “geek”:
12. List files using TCP port 80, repeating every two seconds until lsof is terminated:
Final Words
lsof lists open files. Since everything in Linux is a file, this tool can tell you a fantastic amount of information about your running system. It is primarily used to tell what processes have what files open, but it can also be used to view TCP and UDP connection information, among other things.
Some more articles you might also be interested in …
lsof command not found on CentOS 7 / RHEL 7 Server
I got the error ‘lsof command not found’ on my CentOS 7 server. lsof means ‘List Open Files’. Lsof is used to find all files that are open by a process that is running on the server.
root@centos7 [/]# lsof -i :443
-bash: /usr/sbin/lsof: No such file or directory
[root@centos7 ~]# lsof -p 443
-bash: lsof: command not found
Error shows that lsof command is not installed on your server. Do the below steps to install lsof command on your CentOS 7 / Redhat 7 Server.
1. Log into your linux server via SSH as root
2. Install lsof package on the server using yum
3. Enter ‘y’ if yum ask for confirmation while installing lsof
Resolving Dependencies
–> Running transaction check
—> Package lsof.x86_64 0:4.87-6.el7 will be installed
–> Finished Dependency Resolution
Total download size: 331 k
Installed size: 927 k
Is this ok [y/d/N]: y
Downloading packages:
lsof-4.87-6.el7.x86_64.rpm | 331 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lsof-4.87-6.el7.x86_64 1/1
Verifying : lsof-4.87-6.el7.x86_64 1/1
Installed:
lsof.x86_64 0:4.87-6.el7
Now lsof package is installed on your Linux Server.
lsof command not found – How to install lsof command
How to download and install lsof rpm on CentOS 7 / RHEL 7 server
1. Log into your CentOS 7 / RHEL 7 server as root user
2. Download lsof rpm from CentOS website using wget command
[root@redhat /]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/lsof-4.87-6.el7.x86_64.rpm
Saving to: ‘lsof-4.87-6.el7.x86_64.rpm’
2020-11-17 17:55:42 (1.37 MB/s) – ‘lsof-4.87-6.el7.x86_64.rpm’ saved [338864/338864]
3. Install the downloaded rpm file either using yum or rpm command
yum command will lsof package and the required dependencies
You must first install the dependencies, if you are installing lsof package using rpm command.
lsof command need the below dependencies :
The above dependencies will be installed by default on CentOS7 / RHEL7 server.
How to Install lsof Command on Ubuntu 21.04 Server
lsof means ‘List Open Files’. On Linux servers, Lsof command is used to find all files that are open by a process that is running on the server.
root@ubuntu-server [/]# lsof -i :8080
-bash: /usr/sbin/lsof: No such file or directory
[root@ubuntu-server ~]# lsof -p 3096
-bash: lsof: command not found
Error shows that lsof command is not installed on your Ubuntu server. Do the below steps to install lsof command on Ubuntu 21.04 Server.
1. Log into your Linux server via SSH as root or sudo user
2. Install lsof package on the server using apt
3. Enter ‘y’ if apt ask for confirmation while installing lsof.
root@ubuntu-21:/usr/local/src# apt install lsof
Reading package lists… Done
Building dependency tree
Reading state information… Done
Suggested packages:
perl
The following NEW packages will be installed:
lsof
0 upgraded, 1 newly installed, 0 to remove and 84 not upgraded.
Need to get 252 kB of archives.
After this operation, 466 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu hirsute/main amd64 lsof amd64 4.93.2+dfsg-1.1 [252 kB]
Fetched 252 kB in 3s (83.0 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package lsof.
(Reading database … 6125 files and directories currently installed.)
Preparing to unpack …/lsof_4.93.2+dfsg-1.1_amd64.deb …
Unpacking lsof (4.93.2+dfsg-1.1) …
Setting up lsof (4.93.2+dfsg-1.1) …
Now lsof package is installed on your Linux Server.
How to install Lsof command on Ubuntu 21.04 Server
Steps to download lsof.deb package on Ubuntu 21.04 Server
1. You must log into your Ubuntu server as root or sudo user
2. Download lsof deb file from ubuntu website using wget command
3. Install the package using apt or dpkg command
4. Run ‘lsof -v’ to verify the installation
apt command will install lsof package and the required dependencies
apt install ./lsof_4.93.2+dfsg-1.1_amd64.deb
lsof -v
lsof version information:
revision: 4.93.2
How to Install lsof Command on CentOS/RHEL 7 Servers
How to Install lsof Command on CentOS/RHEL 7 Servers
lsof means ‘List Open Files’. Lsof is used to find all files that are open by a process that is running on the server.
root@server [/]# lsof -i :80
-bash: /usr/sbin/lsof: No such file or directory
[root@server1 ~]# lsof -p 8916
-bash: lsof: command not found
Error shows that lsof command is not installed on your server. Do the below steps to install lsof command on your CentOS 7 / Redhat 7 Server.
1. Log into your linux server via SSH as root
2. Install lsof package on the server using yum
3. Enter ‘y’ if yum ask for confirmation while installing lsof.
root@server [/]# yum install lsof
Resolving Dependencies
–> Running transaction check
—> Package lsof.x86_64 0:4.87-4.el7 will be installed
–> Finished Dependency Resolution
Total download size: 331 k
Installed size: 927 k
Is this ok [y/d/N]: y
Downloading packages:
lsof-4.87-4.el7.x86_64.rpm | 331 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lsof-4.87-4.el7.x86_64 1/1
Verifying : lsof-4.87-4.el7.x86_64 1/1
Installed:
lsof.x86_64 0:4.87-4.el7
Now lsof package is installed on your Linux Server.
How to Install lsof command on CentOS RHEL 7 Linux Server
How to download and install lsof rpm on CentOS 7 / RHEL 7 server
1. You must log into your CentOS 7 / RHEL 7 server as root user
2. Download lsof rpm from CentOS website using wget command
3. You can install the downloaded rpm file using yum or rpm command
# yum install lsof-4.87-6.el7.x86_64.rpm
yum command will lsof package and the required dependencies
# rpm -ivh lsof-4.87-6.el7.x86_64.rpm
You must first install the dependencies, if you are installing using yum command.
lsof command need the below dependencies :
The above dependencies will be installed by default on CentOS7 / RHEL7 server.
Commands not found: netstat, nmap on CentOS 7
The package net-tools was deprecated in CentOS7 in favour of the iproute2 suite. You may either install it manually or check out this blogpost for replacement commands:
EDIT
Here is the URL to Red Hat’s Bugzilla for RHEL7 that covers the deprecation of netstat in more detail: https://bugzilla.redhat.com/show_bug.cgi?id=1119297
As stated before, net-tools are deprecated thus shouldn’t be used unless necessary. Behaviour in RHEL 7 is the same as in Fedora — net-tools is missing from minimal install, but is in @base (~= @standard in Fedora) which is installed in all non-minimal configurations.
There are also other tickets that deal with this such as IDs 682308 and 687920. Note that they are assigned to the Fedora project and are quite old.
I don’t think that lsof was deprecated. It’s just not installed by default. You can get it with yum install lsof .
It only took a few minutes for me to determine that netstat -plnt was replaced by ss -plnt using this link from @DanKeder’s answer above. I think we should first try to make the distribution’s recommended solutions work instead of reverting to deprecated approaches.
Whenever you cannot find executables on a Red Hat based distro and you know their names, you should do 1 of the following 2 things.
Use repoquery
You can search the YUM repositories that are available to your system by using the command repoquery . If it isn’t installed, then do a yum install yum-utils .
$ repoquery -qf */nmap nmap-2:6.40-4.el7.x86_64
From here you can see which packages have an executable with those names. Here’s all of them at once.
$ repoquery -qf */netstat */lsof */nmap net-tools-0:2.0-0.17.20131004git.el7.x86_64 ctdb-tests-0:2.5.1-2.el7.x86_64 lsof-0:4.87-4.el7.x86_64 nmap-2:6.40-4.el7.x86_64 ctdb-tests-0:2.5.1-2.el7.x86_64
Now just do a sudo yum install lsof or sudo yum install nmap to install these missing packages.
Use yum search
You can also do a similar search using yum search .
$ yum search netstat Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.advancedhosters.com * extras: mirror.cisp.com * updates: centos-mirror.jchost.net ================================================================== Matched: netstat ================================================================== dstat.noarch : Versatile resource statistics tool net-snmp.x86_64 : A collection of SNMP protocol tools and libraries net-tools.x86_64 : Basic networking tools
With this approach you’ll need to do a bit of digging to confirm that the resulting packages include the executable you’re looking for. I typically look in there lit of files for what I want, but for that you’ll have to use repoquery .
$ repoquery -ql net-tools.x86_64 | grep netstat /bin/netstat /usr/share/man/de/man8/netstat.8.gz /usr/share/man/fr/man8/netstat.8.gz /usr/share/man/man8/netstat.8.gz /usr/share/man/pt/man8/netstat.8.gz
So using my first approach saves you the extra steps.
netstat’s deprecation
Apparently in CentOS 7 netstat , which is part of the package net-tools has been officially deprecated, so you should be using ss (part of the package iproute2), going forward.