chroot命令的主要作用是提供一个隔离的环境,使得在该环境下运行的进程无法访问到主系统的文件和目录。这样可以增加系统的安全性,防止进程对系统进行非法操作或者破坏。
一、基础环境
0 1 2 3 4 5 |
$ uname -a Linux hk 5.14.0-514.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Sep 30 14:56:52 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux $ uname -r 5.14.0-514.el9.x86_64 $ uname -m x86_64 |
二、创建账号
0 1 |
$ useradd developer $ echo '123456' | passwd --stdin developer |
三、配置SSHD
0 1 2 3 |
$ echo 'Match User developer' >> /etc/ssh/sshd_config $ echo 'ChrootDirectory /data/chroot' >> /etc/ssh/sshd_config $ sed -i 's/^Subsystem.*$/Subsystem sftp internal-sftp/' /etc/ssh/sshd_config $ systemctl restart sshd |
四、权限配置
VIP内容
VIP登录后查看
Login
五、bug fix
0 1 |
$ \cp /etc/bashrc /data/chroot/etc/ $ chmod 1777 /data/chroot/tmp |
六、其他说明
SourceByrd's Weblog-https://note.t4x.org/environment/linux-env-chroot/SourceByrd's Weblog-https://note.t4x.org/environment/linux-env-chroot/① 查找命令在系统中的实际位置,并拷贝到bin或者sbin目录下【指的是copy到chroot的bin或者sbin目录】
② 用ldd命令查找出命令运行时所依赖的共享包
③ 把这些共享包拷贝到lib64下
4️⃣ 下面的示例参考:
SourceByrd's Weblog-https://note.t4x.org/environment/linux-env-chroot/
01234567891011121314151617181920212223242526272829303132333435363738394041 cd /data/chroot/bin\cp /usr/bin/ls .chroot=/data/chrootldd /usr/bin/lsfor i in `ldd /usr/bin/ls | awk '{print $3}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donefor i in `ldd /usr/bin/ls | awk '{print $1}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;done\cp /usr/sbin/ping .\cp /usr/sbin/arp /data/chroot/bin/\cp /usr/sbin/ip /data/chroot/bin/\cp /usr/bin/netstat /data/chroot/bin/\cp /usr/sbin/route /data/chroot/bin/\cp /usr/sbin/ifconfig /data/chroot/bin/\cp /usr/bin/echo /data/chroot/bin/ldd /usr/sbin/pingfor i in `ldd /usr/sbin/ping | awk '{print $3}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donefor i in `ldd /usr/sbin/ping | awk '{print $1}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donechroot=/data/chrootldd /usr/sbin/arpfor i in `ldd /usr/sbin/arp | awk '{print $3}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donefor i in `ldd /usr/sbin/arp | awk '{print $1}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donechroot=/data/chrootldd /usr/bin/netstatfor i in `ldd /usr/bin/netstat | awk '{print $3}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donefor i in `ldd /usr/bin/netstat | awk '{print $1}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donechroot=/data/chrootldd /usr/sbin/ipfor i in `ldd /usr/sbin/ip | awk '{print $3}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donefor i in `ldd /usr/sbin/ip | awk '{print $1}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donechroot=/data/chrootldd /usr/sbin/routefor i in `ldd /usr/sbin/route | awk '{print $3}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donefor i in `ldd /usr/sbin/route | awk '{print $1}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donechroot=/data/chrootldd /usr/sbin/ifconfigfor i in `ldd /usr/sbin/ifconfig | awk '{print $3}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donefor i in `ldd /usr/sbin/ifconfig | awk '{print $1}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donechroot=/data/chroot\cp /usr/bin/curl /data/chroot/binldd /usr/bin/curlfor i in `ldd /usr/bin/curl | awk '{print $3}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;donefor i in `ldd /usr/bin/curl | awk '{print $1}'| grep '^/lib'`;do \cp -v $i $chroot/lib64/;done\cp -ap /etc/pki/ /data/chroot/etc/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!