LinuxchrootSSHlogin
Linux_chroot_SSHlogin
[toc]
# chroot
github:如何让特定用户通过SSH登陆后自动chroot (opens new window)
linux:测试 sftp 的 chroot 监狱 (opens new window)
archlinux:Chroot (简体中文)
# steps
属于该组的用户通过ssh登陆系统时将被自动chroot。
- 创建chroot用户组
groupadd chrootjail or addgroup chrootjail
添加用户到chrootjail组中 sudo adduser tester chrootjail
配置sshd 在sshd的配置文件
/etc/ssh/sshd_config
中添加:- 用户组group
Match group chrootjail
ChrootDirectory /var/chroot/
1
2
2
2.用户user
Match User tecmint
ChrootDirectory /var/chroot
1
2
2
- 重启sshd服务
service ssh restart or systemd restart systemd-networkd
- 配置sftp
在
/etc/ssh/sshd_config
中添加:
ForceCommand internal-sftp
重启服务
#systemctl restart sshd
使用ssh连接会报错
- note:
当用户登陆到chroot环境中,其home目录不存在时,它的当前工作目录为“/”.
创建 chroot 监狱通用配置目录 /home/test/etc 并复制已更新的账号文件(/etc/passwd 和 /etc/group)到这个目录中.
每次向系统添加更多 SSH 用户时,都需要将更新的帐户文件复制到 /var/chroot/etc 目录中.
- 附:shell脚本来完成chroot基本功能的的环境准备
#!/bin/bash
# This script can be used to create simple chroot environment
# Written by LinuxCareer.com <http://linuxcareer.com/>
# (c) 2013 LinuxCareer under GNU GPL v3.0+
#!/bin/bash
CHROOT='/home/jail'
mkdir $CHROOT
for i in $( ldd $* | grep -v dynamic | cut -d " " -f 3 | sed 's/://' | sort | uniq )
do
cp --parents $i $CHROOT
done
# ARCH amd64
if [ -f /lib64/ld-linux-x86-64.so.2 ]; then
cp --parents /lib64/ld-linux-x86-64.so.2 /$CHROOT
fi
# ARCH i386
if [ -f /lib/ld-linux.so.2 ]; then
cp --parents /lib/ld-linux.so.2 /$CHROOT
fi
echo "Chroot jail is ready. To access it execute: chroot $CHROOT"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
上次更新: 2023/10/10, 14:48:21