首页 > 其他分享 >[Docker] chroot

[Docker] chroot

时间:2023-07-24 14:56:47浏览次数:25  
标签:chroot x86 lib 64 linux new Docker root

It's a Linux command that allows you to set the root directory of a new process. In our container use case, we just set the root directory to be where-ever the new container's new root directory should be. And now the new container group of processes can't see anything outside of it, eliminating our security problem because the new process has no visibility outside of its new root.

Let's try it. Start up a Ubuntu VM however you feel most comfortable

docker run -it --name docker-host --rm --privileged ubuntu:bionic

 

To see what version of Ubuntu you're using

cat /etc/issue

Ubuntu 18.04.6 LTS \n \l

 

Okay, so let's attempt to use chroot right now.

1. Make a new folder in your home: mkdir new-root

2. Insider that new folder run echo "my super secret thing" >> /new-root/secret.txt

3. Now try to run chroot /new-root bash and see the error it gives you

chroot: failed to run command 'bash': No such file or directory

 

You should see something about failing to run a shell or not being able to find bash. That's because bash is a program and your new root wouldn't have bash to run (because it can't reach outside of its new root.) So let's fix that! Run:

1. mkdir /new-root/bin

2. cp /bin/bash /bin/ls /bin/cat /new-root/bin/

3. chroot /new-root bash

chroot: failed to run command 'bash': No such file or directory

Still not working! The problem is that these commands rely on libraries to power them and we didn't bring those with us.

 

So let's do that too. Run ldd /bin/bash

root@d93ae2aee53d:/# ldd /bin/bash
	linux-vdso.so.1 (0x00007ffec7767000)
	libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f3e95023000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f3e94e1f000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3e94a2e000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f3e95567000)

These are the libraries we need for bash. Let's go ahead and copy those into our new environment.

1. mkdir /new-root/lib{,64}

2. Then we need to copy all those paths (ignore the lines that don't have paths) into our directory. Make sure you get the right files in the right directory. In my case above (yours likely will be different) it'd be two commands:

  • cp /lib/x86_64-linux-gnu/libtinfo.so.5 /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libc.so.6 /new-root/lib
  • cp /lib64/ld-linux-x86-64.so.2 /new-root/lib64

3. Do it again for ls. Run ldd /bin/ls

root@d93ae2aee53d:/# ldd /bin/ls
	linux-vdso.so.1 (0x00007ffd4939c000)
	libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f817b198000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f817ada7000)
	libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f817ab36000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f817a932000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f817b5e2000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f817a713000)

4. Follow the same process to copy the libraries for ls to new-root

cp /lib/x86_64-linux-gnu/libselinux.so.1 /lib/x86_64-linux-gnu/libpcre.so.3 /lib/x86_64-linux-gnu/libpthread.so.0 /new-root/lib

 

Now finally, run chroot /new-root bashand then ls

root@d93ae2aee53d:/# chroot /new-root bash
bash-4.4# ls
bin  lib  lib64  secret.txt
bash-4.4# cat secret.txt 
my super secret thing

You should successfully see everything in the directory. Now try pwd to see your working directory. You should see /. You can't get out of here! This, before being called containers, was called a jail for this reason. At any time, hit CTRL+D or run exit to get out of your chrooted environment. 

标签:chroot,x86,lib,64,linux,new,Docker,root
From: https://www.cnblogs.com/Answer1215/p/17577227.html

相关文章

  • 通过docker安装的jira提示We've detected a potential problem with JIRA's Dashboard
    正常通过docker安装jira后,访问是不会出问题的但是如果使用nginx代理后,就是在nginx里配置了proxy_passhttp://localhost:2800再访问后,就会报错We'vedetectedapotentialproblemwithJIRA'sDashboardconfigurationthatyouradministratorcancorrect.Clickhereto......
  • Spring Boot集成Docker部署教程
    前言Docker有时候确实易用,但SpringBoot官方好像并不推荐这种方式,按照个人理解应该是SpringBoot已经做了一个很优秀的集成,对于Docker这种容器化让开发人员不能专注于开发...附文为SpringBoot官方教程:https://spring.io/guides/topicals/spring-boot-docker/ 正文环境是J......
  • 2.dockerfile指令及数据卷
    dockerfile指令总结FROM:指定基础镜像LABEL:指定镜像元数据,即标签RUN:指定shell命令CMD:容器启动命令EXPORT:暴露端口ENV:设置环境变量ADD:复制和解包文件,增强版的copyCOPY:复制本地宿主机的文本到容器VLOUME:匿名卷WORKDIR:指定工作目录ARG:构建参数USER:指定当前用户ENTRYPOINT:......
  • 3.docker网络管理
    #MySQL容器默认使用了匿名数据卷[root@ubuntu1804~]#dockerrun-d--namemysql-p3306:3306-eMYSQL_ROOT_PASSWORD=123456mysql:5.7.30#备份数据库[root@ubuntu1804~]#dockerrun-it--rm--volumes-frommysql-v$(pwd):/backupcentostarxvf/backup/mysql.tar......
  • 4.镜像制作方式和dockerfile(Ubuntu、nginx)
    制作镜像方式dockercommit通过修改现有的容器,将之手动构建为镜像dockerbuild通过dockerfile文件,批量构建为镜像用镜像做成容器,在容器的基础上定制一个镜像手动制作镜像:commit基于busybox容器创建busybox:v1.0镜像[root@ubuntu2004~]#dockercommit-ali-m"initbusybo......
  • 1.Docker基础及自动安装
    docker的介绍和基础操作docker单机容器dockercompose单机多个容器k8s多机多个容器container即容器,集装箱docker就是搬运集装的码头工人,管理容器的管理工具Docker的组成:Docker主机(Host):一个物理机或虚拟机,用于运行Docker服务进程和容器,也称为宿主机,node节点Docker......
  • Creating network "docker_default" with the default driver ERROR: Failed to S
    创建网络"docker_default"withthedefaultdriverERROR:FailedtoS在使用Docker容器时,有时会遇到以下错误信息:Creatingnetwork"docker_default"withthedefaultdriverERROR:FailedtoS。这个错误通常表示Docker无法创建名为"docker_default"的网络。本文将解释此错......
  • linux 安装docker完整教程
    一、Docker及系统版本Docker从17.03版本之后分为CE(CommunityEdition:社区版)和EE(EnterpriseEdition:企业版)。相对于社区版本,企业版本强调安全性,但需付费使用。这里我们使用社区版本即可。Docker支持64位版本的CentOS7和CentOS8及更高版本,它要求Linux内核版本不低于3.10 ......
  • docker 国内镜像源
    Docker国内镜像源Docker是一种开源的容器化平台,允许开发者将应用程序及其依赖打包成一个独立的容器,方便部署和管理。在中国大陆,由于网络限制及外网访问速度较慢的问题,访问国外的Docker镜像源不太理想。为了解决这个问题,国内提供了一些镜像源,本文将介绍如何使用这些国内镜像源。......
  • docker 改中文
    Docker改中文简介Docker是一个开源的容器化平台,其提供了一种轻量级的虚拟化技术,可以将应用程序和其依赖的环境打包成一个独立的容器,并在不同的环境中进行部署和运行。它可以大大简化应用程序的部署和管理过程,提高开发和运维的效率。然而,Docker在默认情况下使用的是英文界面,对......