最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

bash - chroot cannot recognize the file's existance - Stack Overflow

programmeradmin0浏览0评论

Trying to do LFS, but now stalled, because chroot(1) doesn't work properly.

#!/bin/bash


_place="$(pwd)/jail"

# Clear before do something
for _x in dev/shm run sys proc dev/pts dev; do
    sudo umount ${_place}/${_x} >& /dev/null
done
for _x in dev/pts dev run sys proc; do
    sudo rmdir ${_place}/${_x} >& /dev/null
done
sudo rm -rf ${_place}/bin >& /dev/null
sudo rmdir ${_place} >& /dev/null

# Run with "go" to mount actually
if (test "${1}" = "go"); then
    sudo mkdir -p ${_place}/{dev/pts,proc,sys,run} || exit 1
    sudo mount --bind /dev ${_place}/dev || exit 1
    sudo mount -t devpts devpts -o gid=5,mode=0620 ${_place}/dev/pts || exit 1
    sudo mount -t proc proc ${_place}/proc || exit 1
    sudo mount -t sysfs sysfs ${_place}/sys || exit 1
    sudo mount -t tmpfs tmpfs ${_place}/run || exit 1
    if (test -h ${_place}/dev/shm); then
        sudo install -v -d -m 1777 ${_place}$(realpath /dev/shm) || exit 1
    else
        sudo mount -t tmpfs -o nosuid,nodev tmpfs ${_place}/dev/shm || exit 1
    fi

    sudo mkdir -p ${_place}/bin || exit 1
    sudo cp -f /bin/bash ${_place}/bin/ || exit 1

    sudo chroot ${_place}
fi

So I wrote this small script to test if chroot(1) works properly. The goal is to run /bin/bash in a chrooted environment. But it fails with:

chroot: failed to run command ‘/bin/bash’: No such file or directory

May I ask you to answer me what should I do in this script to make this script work properly?

Trying to do LFS, but now stalled, because chroot(1) doesn't work properly.

#!/bin/bash


_place="$(pwd)/jail"

# Clear before do something
for _x in dev/shm run sys proc dev/pts dev; do
    sudo umount ${_place}/${_x} >& /dev/null
done
for _x in dev/pts dev run sys proc; do
    sudo rmdir ${_place}/${_x} >& /dev/null
done
sudo rm -rf ${_place}/bin >& /dev/null
sudo rmdir ${_place} >& /dev/null

# Run with "go" to mount actually
if (test "${1}" = "go"); then
    sudo mkdir -p ${_place}/{dev/pts,proc,sys,run} || exit 1
    sudo mount --bind /dev ${_place}/dev || exit 1
    sudo mount -t devpts devpts -o gid=5,mode=0620 ${_place}/dev/pts || exit 1
    sudo mount -t proc proc ${_place}/proc || exit 1
    sudo mount -t sysfs sysfs ${_place}/sys || exit 1
    sudo mount -t tmpfs tmpfs ${_place}/run || exit 1
    if (test -h ${_place}/dev/shm); then
        sudo install -v -d -m 1777 ${_place}$(realpath /dev/shm) || exit 1
    else
        sudo mount -t tmpfs -o nosuid,nodev tmpfs ${_place}/dev/shm || exit 1
    fi

    sudo mkdir -p ${_place}/bin || exit 1
    sudo cp -f /bin/bash ${_place}/bin/ || exit 1

    sudo chroot ${_place}
fi

So I wrote this small script to test if chroot(1) works properly. The goal is to run /bin/bash in a chrooted environment. But it fails with:

chroot: failed to run command ‘/bin/bash’: No such file or directory

May I ask you to answer me what should I do in this script to make this script work properly?

Share Improve this question edited yesterday user11140649 asked yesterday user11140649user11140649 134 bronze badges 1
  • Does it fail when sudo chroot ${_place} is run? – Arkadiusz Drabczyk Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 1

Looking at your script; when you chroot you need to copy not just the binary itself but also all its dependencies.

The error message chroot: failed to run command '/bin/bash': No such file or directory happens when the dynamic linker can not resolve the dependencies for bash. The binary exists but it can not be executed because the required shared libraries are not available in the chroot environment.

Try this fixed version:

#!/bin/bash

_place="$(pwd)/jail"

# Clear before do something
for _x in dev/shm run sys proc dev/pts dev; do
    sudo umount ${_place}/${_x} >& /dev/null
done
for _x in dev/pts dev run sys proc; do
    sudo rmdir ${_place}/${_x} >& /dev/null
done
sudo rm -rf ${_place}/bin ${_place}/lib ${_place}/lib64 >& /dev/null
sudo rmdir ${_place} >& /dev/null

# Run with "go" to mount actually
if (test "${1}" = "go"); then
    # Create directory structure
    sudo mkdir -p ${_place}/{bin,lib,lib64,dev/pts,proc,sys,run} || exit 1
    
    # Copy bash and its dependencies
    sudo cp -f /bin/bash ${_place}/bin/ || exit 1
    
    # Get bash dependencies from ldd and copy them
    for lib in $(ldd /bin/bash | grep -v linux-vdso | awk '{print $3}' | grep ^/); do
        sudo mkdir -p ${_place}/$(dirname $lib) || exit 1
        sudo cp -f $lib ${_place}/$lib || exit 1
    done
    
    # Handle dynamically loaded libs, especially libdl (needed by bash)
    sudo cp -f /lib64/ld-linux-* ${_place}/lib64/ 2>/dev/null || sudo cp -f /lib/ld-linux-* ${_place}/lib/ 2>/dev/null
    
    # Do all the mounts from your original script
    sudo mount --bind /dev ${_place}/dev || exit 1
    sudo mount -t devpts devpts -o gid=5,mode=0620 ${_place}/dev/pts || exit 1
    sudo mount -t proc proc ${_place}/proc || exit 1
    sudo mount -t sysfs sysfs ${_place}/sys || exit 1
    sudo mount -t tmpfs tmpfs ${_place}/run || exit 1
    if (test -h ${_place}/dev/shm); then
        sudo install -v -d -m 1777 ${_place}$(realpath /dev/shm) || exit 1
    else
        sudo mount -t tmpfs -o nosuid,nodev tmpfs ${_place}/dev/shm || exit 1
    fi

    # Chroot and specify the command explicitly
    sudo chroot ${_place} /bin/bash
fi

hope this will work for you.

发布评论

评论列表(0)

  1. 暂无评论