2021-02-19 18:26:00 +08:00
|
|
|
---
|
|
|
|
|
title: Lsof
|
|
|
|
|
date: 2021-02-05 16:12:47
|
|
|
|
|
background: bg-blue-400
|
|
|
|
|
tags:
|
2024-05-22 03:37:57 -07:00
|
|
|
- port
|
|
|
|
|
- processes
|
|
|
|
|
- utility
|
2021-02-19 18:26:00 +08:00
|
|
|
categories:
|
2024-05-22 03:37:57 -07:00
|
|
|
- Linux Command
|
2021-02-19 18:26:00 +08:00
|
|
|
intro: |
|
2024-05-22 03:37:57 -07:00
|
|
|
This quick reference cheat sheet provides various for using lsof command.
|
2023-03-06 16:19:19 +08:00
|
|
|
plugins:
|
2024-05-22 03:37:57 -07:00
|
|
|
- copyCode
|
2021-02-19 18:26:00 +08:00
|
|
|
---
|
|
|
|
|
|
2024-05-22 03:37:57 -07:00
|
|
|
## Getting Started
|
2021-02-19 18:26:00 +08:00
|
|
|
|
|
|
|
|
### Introduction
|
2024-05-22 03:37:57 -07:00
|
|
|
|
2021-02-19 18:26:00 +08:00
|
|
|
**lsof** meaning `L`i`S`t `O`pen `F`iles is used to find out which files are open by which process
|
|
|
|
|
|
|
|
|
|
```shell script
|
|
|
|
|
$ lsof
|
|
|
|
|
$ sudo lsof -u root
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Port-specific
|
|
|
|
|
|
|
|
|
|
```shell script
|
|
|
|
|
$ lsof -i :8080
|
|
|
|
|
$ lsof -i :80 -i :22
|
|
|
|
|
$ lsof -i TCP:22
|
|
|
|
|
$ lsof -i TCP:1-1024
|
|
|
|
|
$ lsof -i UDP
|
|
|
|
|
$ lsof -i @192.168.1.5
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Process-specific
|
2024-05-22 03:37:57 -07:00
|
|
|
|
2021-02-19 18:26:00 +08:00
|
|
|
```shell script
|
|
|
|
|
$ lsof -c mysql
|
|
|
|
|
$ lsof -c java
|
|
|
|
|
$ lsof -c ssh
|
|
|
|
|
$ lsof -c nginx
|
|
|
|
|
$ lsof -c ssh -c httpd
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### User-specific
|
|
|
|
|
|
|
|
|
|
```shell script
|
|
|
|
|
$ lsof -u www-data
|
|
|
|
|
$ lsof -u www-data -u ubuntu
|
|
|
|
|
$ lsof -i -u ^root # Except certain user
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Network-specific
|
2024-05-22 03:37:57 -07:00
|
|
|
|
2021-02-19 18:26:00 +08:00
|
|
|
```shell script
|
|
|
|
|
$ lsof -i 4 # IPv4 only
|
|
|
|
|
$ lsof -i 6 # IPv6 only
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### PID-specific
|
2024-05-22 03:37:57 -07:00
|
|
|
|
2021-02-19 18:26:00 +08:00
|
|
|
```shell script
|
|
|
|
|
$ lsof -p 1753
|
|
|
|
|
$ lsof -p ^3 # Except certain pids
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Filename-specific
|
2024-05-22 03:37:57 -07:00
|
|
|
|
2021-02-19 18:26:00 +08:00
|
|
|
```shell script
|
|
|
|
|
$ lsof /var/log/messages
|
|
|
|
|
$ lsof /etc/passwd
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Directory-specific
|
2024-05-22 03:37:57 -07:00
|
|
|
|
2021-02-19 18:26:00 +08:00
|
|
|
```shell script
|
|
|
|
|
$ lsof +D /var/log # Within a directory
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Kill
|
2024-05-22 03:37:57 -07:00
|
|
|
|
2021-02-19 18:26:00 +08:00
|
|
|
```shell script
|
|
|
|
|
$ kill -9 `lsof -t -u apache`
|
|
|
|
|
$ kill -9 $(lsof -t -i :8080)
|
|
|
|
|
```
|