220 lines
6.6 KiB
HTML
220 lines
6.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
|
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
|
<link rel="stylesheet" href="../assets/css/style.css">
|
|
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
|
<script>
|
|
// Apply theme immediately to prevent flash
|
|
(function() {
|
|
const theme = localStorage.getItem('theme') ||
|
|
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
})();
|
|
</script>
|
|
<title>lsof Cheatsheet - Cheatsheets - Launch Pad</title>
|
|
</head>
|
|
<body>
|
|
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
|
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
|
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
|
</button>
|
|
<br/><br/>
|
|
<div class="name">
|
|
__ _______________________ _________._________________________
|
|
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
|
|
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
|
|
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
|
|
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
|
|
\/ \/ \/ \/ \/ \/ \/
|
|
</div>
|
|
<div class="blog-page-header">
|
|
<div class="blog-header-content">
|
|
<a href="/cheatsheets" class="back-link" title="Back to Cheatsheets">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
|
</a>
|
|
<h1 class="blog-page-title">lsof Cheatsheet</h1>
|
|
</div>
|
|
</div>
|
|
<div class="blog-post-container">
|
|
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
|
<div class="blog-post">
|
|
<div class="blog-post-content">
|
|
<p><a href="index.html">← Back to cheatsheets</a></p>
|
|
<p><a href="../index.html">← Home</a></p>
|
|
<hr>
|
|
<p>lsof (list open files) shows information about files opened by processes. Lists open files, network connections, and file descriptors. Essential for debugging and system administration.</p>
|
|
<hr>
|
|
<h2>Basic Usage</h2>
|
|
<ul>
|
|
<li>lsof - List all open files</li>
|
|
</ul>
|
|
<ul>
|
|
<li>lsof -p PID - Files by process</li>
|
|
</ul>
|
|
<ul>
|
|
<li>lsof -u user - Files by user</li>
|
|
</ul>
|
|
<ul>
|
|
<li>lsof file - Processes using file</li>
|
|
</ul>
|
|
<ul>
|
|
<li>lsof -i - Network connections</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Process Selection</h2>
|
|
<ul>
|
|
<li>-p PID - Process ID</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-p PID1,PID2 - Multiple PIDs</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-p ^PID - Exclude PID</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-c command - Command name</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-u user - Username</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-u ^user - Exclude user</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>File Selection</h2>
|
|
<ul>
|
|
<li>lsof file - Specific file</li>
|
|
</ul>
|
|
<ul>
|
|
<li>lsof +D directory - Directory</li>
|
|
</ul>
|
|
<ul>
|
|
<li>lsof +d directory - Directory (non-recursive)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-a - AND conditions</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-t - Show only PIDs</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Network Options</h2>
|
|
<ul>
|
|
<li>-i - All network connections</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-i TCP - TCP connections</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-i UDP - UDP connections</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-i :port - Specific port</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-i @host - Specific host</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-i @host:port - Host and port</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-i TCP:port - TCP on port</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-i UDP:port - UDP on port</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Output Options</h2>
|
|
<ul>
|
|
<li>-F - Field output</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-l - Show login names</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-n - No hostname resolution</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-P - No port name resolution</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-t - PID only</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-w - Suppress warnings</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-V - Version</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Common Examples</h2>
|
|
<h3>Process Files</h3>
|
|
<pre><code>lsof -p 1234</code></pre>
|
|
<p>Files opened by process.</p>
|
|
<h3>User Files</h3>
|
|
<pre><code>lsof -u username</code></pre>
|
|
<p>Files opened by user.</p>
|
|
<h3>File Usage</h3>
|
|
<pre><code>lsof /path/to/file</code></pre>
|
|
<p>Processes using file.</p>
|
|
<h3>Network Connections</h3>
|
|
<pre><code>lsof -i</code></pre>
|
|
<p>All network connections.</p>
|
|
<h3>Port Usage</h3>
|
|
<pre><code>lsof -i :80</code></pre>
|
|
<p>Process using port 80.</p>
|
|
<h3>TCP Connections</h3>
|
|
<pre><code>lsof -i TCP</code></pre>
|
|
<p>All TCP connections.</p>
|
|
<h3>Specific Host</h3>
|
|
<pre><code>lsof -i @192.168.1.1</code></pre>
|
|
<p>Connections to/from host.</p>
|
|
<h3>Command Files</h3>
|
|
<pre><code>lsof -c firefox</code></pre>
|
|
<p>Files opened by command.</p>
|
|
<h3>Directory Files</h3>
|
|
<pre><code>lsof +D /var/log</code></pre>
|
|
<p>Files in directory.</p>
|
|
<h3>PID Only</h3>
|
|
<pre><code>lsof -t -i :80</code></pre>
|
|
<p>Get PID using port.</p>
|
|
<hr>
|
|
<h2>Tips</h2>
|
|
<ul>
|
|
<li>Use -i for network troubleshooting</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use -t to get PIDs for scripts</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use -n and -P for faster output</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Combine with kill to free resources</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use -a to combine conditions</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Essential for debugging file locks</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Great for finding port conflicts</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Useful for security auditing</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
|
<script src="../theme.js"></script>
|
|
</body>
|
|
</html>
|
|
|