<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Samekh</id>
	<title>Alpine Linux - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Samekh"/>
	<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/wiki/Special:Contributions/Samekh"/>
	<updated>2026-04-30T09:46:58Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.40.0</generator>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Alpine_Linux_in_a_chroot&amp;diff=20137</id>
		<title>Alpine Linux in a chroot</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Alpine_Linux_in_a_chroot&amp;diff=20137"/>
		<updated>2021-09-17T19:17:45Z</updated>

		<summary type="html">&lt;p&gt;Samekh: use &amp;#039;/bin/ash&amp;#039; instead of &amp;#039;ash&amp;#039; as chroot command - absolute path is required&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
Inside the chroot environment, you can build, debug, and run Alpine packages or develop things. It&#039;s the most known way to do so without replacing your system or using a Virtual Machine.&lt;br /&gt;
This document explains how to set up an [[Alpine_newbie#Developer|Alpine build environment]] in a chroot under a host Linux distro, that can also be used to install Alpine Linux from a non-Alpine Linux system or live environment.&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
&lt;br /&gt;
* Working Linux instalation where to perform all the process&lt;br /&gt;
* Linux kernel 2.6.22, with &amp;lt;code&amp;gt;curl&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;chroot&amp;lt;/code&amp;gt; binary installed&lt;br /&gt;
* target media with at least 100M, 900MB for more complete solution as minimum&lt;br /&gt;
* internet connection&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
The variables below: &lt;br /&gt;
&lt;br /&gt;
*&#039;&#039;&#039;${chroot_dir}&#039;&#039;&#039; = Should point to the new root directory&lt;br /&gt;
*&#039;&#039;&#039;${mirror}&#039;&#039;&#039; = Should be replaced with [http://nl.alpinelinux.org/alpine/MIRRORS.txt one of the available Alpine Linux mirrors].&lt;br /&gt;
*&#039;&#039;&#039;${arch}&#039;&#039;&#039; =  Should be the cpu architecture like x86 (i386) or amd64(x86_64)..&lt;br /&gt;
&lt;br /&gt;
== Set up APK ==&lt;br /&gt;
&lt;br /&gt;
Download the latest apk static package (replace &amp;lt;tt&amp;gt;${version}&amp;lt;/tt&amp;gt; with actual version):&lt;br /&gt;
&lt;br /&gt;
{{Cmd|curl -LO ${mirror}/latest-stable/main/${arch}/apk-tools-static-${version}.apk}}&lt;br /&gt;
&lt;br /&gt;
.apk packages are just gzipped tarballs, you can unpack them using:&lt;br /&gt;
{{Cmd|tar -xzf apk-tools-static-*.apk}}&lt;br /&gt;
&lt;br /&gt;
== Install the alpine base installation onto the chroot ==&lt;br /&gt;
&lt;br /&gt;
{{Cmd|./sbin/apk.static -X ${mirror}/latest-stable/main -U --allow-untrusted -p ${chroot_dir} --initdb add alpine-base}}&lt;br /&gt;
&lt;br /&gt;
== Seting up the chroot ==&lt;br /&gt;
&lt;br /&gt;
Before you change root to the new directory, you need to create the required devices&lt;br /&gt;
&lt;br /&gt;
===== Method 1.a Simple way: Using the host&#039;s /dev =====&lt;br /&gt;
&lt;br /&gt;
{{Cmd|mount -o bind /dev ${chroot_dir}/dev}}&lt;br /&gt;
&lt;br /&gt;
{{Note|Bind mounts can be made read-only which would limit the chroot from writing to the devices}}&lt;br /&gt;
&lt;br /&gt;
===== Method 1.b Manual way: Creating needed nodes =====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Manually creating devices will only provide the ones that have been created}}&lt;br /&gt;
&lt;br /&gt;
{{Cmd|mknod -m 666 ${chroot_dir}/dev/full c 1 7&lt;br /&gt;
mknod -m 666 ${chroot_dir}/dev/ptmx c 5 2&lt;br /&gt;
mknod -m 644 ${chroot_dir}/dev/random c 1 8&lt;br /&gt;
mknod -m 644 ${chroot_dir}/dev/urandom c 1 9&lt;br /&gt;
mknod -m 666 ${chroot_dir}/dev/zero c 1 5&lt;br /&gt;
mknod -m 666 ${chroot_dir}/dev/tty c 5 0}}&lt;br /&gt;
&lt;br /&gt;
If you need SCSI disc access you can create the device nodes like this:&lt;br /&gt;
&lt;br /&gt;
{{Note|Every device can have 15 sub-nodes, you should always increment by 16 for every new device}}&lt;br /&gt;
&lt;br /&gt;
{{Cmd|mknod -m 666 ${chroot_dir}/dev/sda b 8 0&lt;br /&gt;
mknod -m 666 ${chroot_dir}/dev/sda1 b 8 1&lt;br /&gt;
mknod -m 666 ${chroot_dir}/dev/sda2 b 8 2&lt;br /&gt;
mknod -m 666 ${chroot_dir}/dev/sda3 b 8 3&lt;br /&gt;
mknod -m 666 ${chroot_dir}/dev/sdb b 8 16&lt;br /&gt;
mknod -m 666 ${chroot_dir}/dev/sdb1 b 8 17}}&lt;br /&gt;
&lt;br /&gt;
==== Make the process fs and /sys fs available ====&lt;br /&gt;
&lt;br /&gt;
{{Cmd|mount -t proc none ${chroot_dir}/proc&lt;br /&gt;
mount -o bind /sys ${chroot_dir}/sys}}&lt;br /&gt;
&lt;br /&gt;
==== Set up name resolution ====&lt;br /&gt;
&lt;br /&gt;
A resolv.conf is needed for name resolution: &lt;br /&gt;
&lt;br /&gt;
You can either copy your host&#039;s resolv.conf:&lt;br /&gt;
{{Cmd|cp -L /etc/resolv.conf ${chroot_dir}/etc/}}&lt;br /&gt;
&lt;br /&gt;
or instead you can create a new one (this example uses OpenDNS):&lt;br /&gt;
{{Cmd|echo -e &#039;nameserver 8.8.8.8\nnameserver 2620:0:ccc::2&#039; &amp;gt; ${chroot_dir}/etc/resolv.conf}}&lt;br /&gt;
&lt;br /&gt;
==== Prepare the APK repositories ====&lt;br /&gt;
&lt;br /&gt;
Set up APK main repository (replace &amp;lt;tt&amp;gt;${branch}&amp;lt;/tt&amp;gt; with the latest stable branch name, e.g. v3.3):&lt;br /&gt;
&lt;br /&gt;
{{Cmd|mkdir -p ${chroot_dir}/etc/apk&lt;br /&gt;
echo &amp;quot;${mirror}/${branch}/main&amp;quot; &amp;gt; ${chroot_dir}/etc/apk/repositories}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|The chroot methods are commonly used to have Alpine installed in a existing system. Because of this, the steps for preparing a bootloader are not included.}}&lt;br /&gt;
&lt;br /&gt;
==== Entering your chroot ====&lt;br /&gt;
&lt;br /&gt;
You then can enter your chroot by running this command as the root user (UID 0).&lt;br /&gt;
&lt;br /&gt;
{{Cmd|chroot ${chroot_dir} /bin/ash -l}}&lt;br /&gt;
&lt;br /&gt;
==== Preparing init services ====&lt;br /&gt;
&lt;br /&gt;
If you plan to use your chroot with a init system or setup a new system on another device you should add these services:&lt;br /&gt;
&lt;br /&gt;
{{Cmd|rc-update add devfs sysinit&lt;br /&gt;
rc-update add dmesg sysinit&lt;br /&gt;
rc-update add mdev sysinit&lt;br /&gt;
&lt;br /&gt;
rc-update add hwclock boot&lt;br /&gt;
rc-update add modules boot&lt;br /&gt;
rc-update add sysctl boot&lt;br /&gt;
rc-update add hostname boot&lt;br /&gt;
rc-update add bootmisc boot&lt;br /&gt;
rc-update add syslog boot&lt;br /&gt;
&lt;br /&gt;
rc-update add mount-ro shutdown&lt;br /&gt;
rc-update add killprocs shutdown&lt;br /&gt;
rc-update add savecache shutdown}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- TODO: Create a page dedicated to installing GRUB and Syslinux and reference them here&lt;br /&gt;
==== Adding a bootloader ====&lt;br /&gt;
&lt;br /&gt;
[[GRUB]]&lt;br /&gt;
&lt;br /&gt;
{{Warning|Run this only if Alpine was installed to a dedicated partiton mounted at the &amp;lt;nowiki&amp;gt;${chroot_dir}&amp;lt;/nowiki&amp;gt; directory.}}&lt;br /&gt;
&lt;br /&gt;
{{Cmd|&amp;lt;nowiki&amp;gt;apk add syslinux&lt;br /&gt;
dd if=/usr/share/syslinux/mbr.bin of=/dev/sda&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Make sure that the device you give the &amp;lt;code&amp;gt;dd&amp;lt;/code&amp;gt; utility is the one you want to install the bootloader to. This process cannot be reverted!}}&lt;br /&gt;
 --&amp;gt;&lt;br /&gt;
= Troubleshooting =&lt;br /&gt;
&lt;br /&gt;
== Hardened kernels or alpine as chroot host ==&lt;br /&gt;
&lt;br /&gt;
If you are using Alpine as a native build system you will have to make sure that you can run &amp;lt;code&amp;gt;chmod&amp;lt;/code&amp;gt; from a chroot. Add the following to &amp;lt;code&amp;gt;/etc/sysctl.conf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;kernel.grsecurity.chroot_deny_chmod = 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then reload the sysctl configuration&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;sysctl -p&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== chroot: cannot run command &#039; ... Exec format error ==&lt;br /&gt;
&lt;br /&gt;
This usually indicates that you booted with one architecture (e.g. armf) and are trying to chroot into another (e.g. x86_64). The binaries must be built for the architecture that the host runs!&lt;br /&gt;
&lt;br /&gt;
Note that with &#039;&#039;&#039;one exception you can run 32 bit x86 chroot in x86_64, but not viceversa&#039;&#039;&#039;!&lt;br /&gt;
&lt;br /&gt;
== WARNING: Ignoring APKINDEX.xxxx.tar.gz ==&lt;br /&gt;
&lt;br /&gt;
Make sure &amp;lt;code&amp;gt;${chroot_dir}/etc/apk/repositories&amp;lt;/code&amp;gt; is valid and run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;apk update&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;br /&gt;
&lt;br /&gt;
* You can also use script [https://github.com/alpinelinux/alpine-chroot-install/ alpine-chroot-install]&lt;br /&gt;
* https://web.archive.org/web/20190808203313/https://isc.sans.edu/forums/diary/Forensic+use+of+mount+bind/22854/&lt;br /&gt;
* Alpine Linux in a chroot on Fedora : http://git.alpinelinux.org/cgit/user/fab/scripts/tree/alpine-chroot.sh script&lt;br /&gt;
* Alpine Linux aarch64 in a chroot on AWS Linux : https://gist.github.com/emolitor/0567e51c0ce04f4b025fc78d2cf0b4f1 script&lt;br /&gt;
&lt;br /&gt;
[[Category: Installation]]&lt;br /&gt;
[[category: System Administration]]&lt;/div&gt;</summary>
		<author><name>Samekh</name></author>
	</entry>
</feed>