309 lines
10 KiB
HTML
309 lines
10 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>chroot 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">chroot 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>chroot changes the apparent root directory for the current running process and its children. Used for system maintenance, creating isolated environments, and recovery operations.</p>
|
|
<hr>
|
|
<h2>Basic Usage</h2>
|
|
<ul>
|
|
<li>chroot <newroot> <command> - Change root and run command</li>
|
|
</ul>
|
|
<ul>
|
|
<li>chroot <newroot> /bin/sh - Change root and start shell</li>
|
|
</ul>
|
|
<ul>
|
|
<li>chroot <newroot> /bin/bash - Change root and start bash</li>
|
|
</ul>
|
|
<ul>
|
|
<li>chroot <newroot> /usr/bin/env sh - Change root with env path</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Common Use Cases</h2>
|
|
<h3>System Recovery/Maintenance</h3>
|
|
<pre><code># Boot from live CD/USB, mount root filesystem
|
|
mount /dev/sda2 /mnt
|
|
|
|
# Mount necessary filesystems
|
|
mount --bind /dev /mnt/dev
|
|
mount --bind /proc /mnt/proc
|
|
mount --bind /sys /mnt/sys
|
|
|
|
# Chroot into the system
|
|
chroot /mnt /bin/bash</code></pre>
|
|
<h3>Testing/Debugging</h3>
|
|
<pre><code># Chroot into a directory with minimal system
|
|
chroot /path/to/root /bin/sh
|
|
|
|
# Run specific command in chroot
|
|
chroot /path/to/root /usr/bin/ls -la /</code></pre>
|
|
<hr>
|
|
<h2>Required Setup Before chroot</h2>
|
|
<p>Before using chroot, you typically need to:</p>
|
|
<ol>
|
|
<li>Mount the target filesystem</li>
|
|
</ol>
|
|
<ul>
|
|
<li>* `mount /dev/sdaX /mnt` - Mount root partition</li>
|
|
</ul>
|
|
<ol>
|
|
<li>Mount essential virtual filesystems</li>
|
|
</ol>
|
|
<ul>
|
|
<li>* `mount --bind /dev /mnt/dev` - Bind device directory</li>
|
|
</ul>
|
|
<ul>
|
|
<li>* `mount --bind /proc /mnt/proc` - Bind proc filesystem</li>
|
|
</ul>
|
|
<ul>
|
|
<li>* `mount --bind /sys /mnt/sys` - Bind sys filesystem</li>
|
|
</ul>
|
|
<ul>
|
|
<li>* `mount --bind /dev/pts /mnt/dev/pts` - Bind pseudo-terminals (optional)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>* `mount --tmpfs /mnt/run` - Mount tmpfs for /run (if needed)</li>
|
|
</ul>
|
|
<ol>
|
|
<li>Copy network configuration (if needed)</li>
|
|
</ol>
|
|
<ul>
|
|
<li>* `cp /etc/resolv.conf /mnt/etc/resolv.conf` - DNS resolution</li>
|
|
</ul>
|
|
<ol>
|
|
<li>Ensure shell and binaries exist in new root</li>
|
|
</ol>
|
|
<ul>
|
|
<li>* Check `/mnt/bin/sh`, `/mnt/bin/bash`, etc. exist</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Mounting Essential Filesystems</h2>
|
|
<ul>
|
|
<li>mount --bind /dev <newroot>/dev - Bind device directory</li>
|
|
</ul>
|
|
<ul>
|
|
<li>mount --bind /proc <newroot>/proc - Bind proc filesystem</li>
|
|
</ul>
|
|
<ul>
|
|
<li>mount --bind /sys <newroot>/sys - Bind sys filesystem</li>
|
|
</ul>
|
|
<ul>
|
|
<li>mount --bind /dev/pts <newroot>/dev/pts - Bind pseudo-terminals</li>
|
|
</ul>
|
|
<ul>
|
|
<li>mount --tmpfs /run <newroot>/run - Mount tmpfs for /run</li>
|
|
</ul>
|
|
<ul>
|
|
<li>mount -t tmpfs none <newroot>/tmp - Mount tmpfs for /tmp (optional)</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Practical Examples</h2>
|
|
<h3>System Recovery Session</h3>
|
|
<pre><code># 1. Mount root filesystem
|
|
mount /dev/sda2 /mnt
|
|
|
|
# 2. Mount virtual filesystems
|
|
mount --bind /dev /mnt/dev
|
|
mount --bind /proc /mnt/proc
|
|
mount --bind /sys /mnt/sys
|
|
mount --bind /dev/pts /mnt/dev/pts
|
|
|
|
# 3. Copy network config (if needed)
|
|
cp /etc/resolv.conf /mnt/etc/resolv.conf
|
|
|
|
# 4. Chroot and work
|
|
chroot /mnt /bin/bash
|
|
|
|
# Inside chroot:
|
|
# - Fix boot issues
|
|
# - Update packages
|
|
# - Edit configuration files
|
|
# - Reinstall bootloader
|
|
|
|
# 5. Exit chroot
|
|
exit
|
|
|
|
# 6. Unmount everything
|
|
umount /mnt/dev/pts
|
|
umount /mnt/sys
|
|
umount /mnt/proc
|
|
umount /mnt/dev
|
|
umount /mnt</code></pre>
|
|
<h3>Package Management in Chroot</h3>
|
|
<pre><code># Arch Linux
|
|
chroot /mnt pacman -Syu
|
|
|
|
# Debian/Ubuntu
|
|
chroot /mnt apt update
|
|
chroot /mnt apt upgrade
|
|
|
|
# Red Hat/CentOS/Fedora
|
|
chroot /mnt dnf update</code></pre>
|
|
<hr>
|
|
<h2>Exiting Chroot</h2>
|
|
<ul>
|
|
<li>exit - Exit shell (returns to original root)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Ctrl+D - Exit shell (same as exit)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Unmount all bind mounts before exiting</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Unmounting After Chroot</h2>
|
|
<ul>
|
|
<li>umount <newroot>/dev/pts - Unmount pseudo-terminals</li>
|
|
</ul>
|
|
<ul>
|
|
<li>umount <newroot>/sys - Unmount sys</li>
|
|
</ul>
|
|
<ul>
|
|
<li>umount <newroot>/proc - Unmount proc</li>
|
|
</ul>
|
|
<ul>
|
|
<li>umount <newroot>/dev - Unmount dev</li>
|
|
</ul>
|
|
<ul>
|
|
<li>umount <newroot>/run - Unmount run (if mounted)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>umount <newroot> - Unmount root filesystem</li>
|
|
</ul>
|
|
<p>If unmounting fails:</p>
|
|
<ul>
|
|
<li>fuser -m <mountpoint> - Find processes using mount</li>
|
|
</ul>
|
|
<ul>
|
|
<li>fuser -km <mountpoint> - Kill processes using mount</li>
|
|
</ul>
|
|
<ul>
|
|
<li>lsof <mountpoint> - List open files on mount</li>
|
|
</ul>
|
|
<ul>
|
|
<li>umount -l <mountpoint> - Lazy unmount (unmount when not busy)</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Troubleshooting</h2>
|
|
<h3>"chroot: failed to run command '/bin/bash': No such file or directory"</h3>
|
|
<ul>
|
|
<li>Check that `/bin/bash` exists in the new root</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Verify architecture matches (32-bit vs 64-bit)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Check library dependencies with `ldd /mnt/bin/bash`</li>
|
|
</ul>
|
|
<h3>"chroot: cannot change root directory to '/mnt': Operation not permitted"</h3>
|
|
<ul>
|
|
<li>chroot requires root privileges - use `sudo` or `su`</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Verify the directory is actually mounted</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Check filesystem permissions</li>
|
|
</ul>
|
|
<h3>Network Not Working in Chroot</h3>
|
|
<ul>
|
|
<li>Copy `/etc/resolv.conf` from host: `cp /etc/resolv.conf /mnt/etc/resolv.conf`</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Bind mount `/etc/resolv.conf`: `mount --bind /etc/resolv.conf /mnt/etc/resolv.conf`</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Ensure network is up in chroot if using network commands</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Tips</h2>
|
|
<ul>
|
|
<li>Always mount /dev, /proc, and /sys before chrooting for full functionality</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use `mount --bind` instead of copying files when possible (reflects host changes)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Copy or bind mount `/etc/resolv.conf` if you need DNS in chroot</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use `exit` to leave chroot, don't just close terminal</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Unmount bind mounts in reverse order of mounting</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Check library dependencies if binaries don't run: `ldd <binary>`</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use `arch-chroot` on Arch Linux for automatic setup of bind mounts</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Create chroot environment with `debootstrap` (Debian/Ubuntu) or `pacstrap` (Arch)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Test chroot setup with simple command first: `chroot /mnt /bin/echo "Hello"`</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Be careful - filesystem operations in chroot affect the actual system</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use chroot for system maintenance, recovery, building packages, and testing</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Some distributions provide helper scripts: `arch-chroot` (Arch), `systemd-nspawn` (systemd)</li>
|
|
</ul>
|
|
<hr>
|
|
<p><a href="index.html">← Back to cheatsheets</a></p>
|
|
<p><a href="../index.html">← Home</a></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
|
<script src="../theme.js"></script>
|
|
</body>
|
|
</html> |