ssh鎖定(chroot)普通帳號的活動目錄vim
一、建立一個普通帳號:bash
[root@localhost ~]# useradd user1 [root@localhost ~]# passwd user1 Changing password for user user1. New password: BAD PASSWORD: it is too simplistic/systematic BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully #必定要給帳號設置密碼
二、建立活動目錄:session
[root@localhost ~]# mkdir /vm/user1 -p
三、進入目錄,建立須要的目錄:ssh
[root@localhost ~]# cd /vm/user1/ [root@localhost user1]# mkdir bin dev etc lib64 proc dev/pts -p [root@localhost user1]# ll total 20 drwxr-xr-x 2 root root 4096 Nov 23 01:33 bin drwxr-xr-x 3 root root 4096 Nov 23 01:33 dev drwxr-xr-x 2 root root 4096 Nov 23 01:33 etc drwxr-xr-x 2 root root 4096 Nov 23 01:33 lib64 drwxr-xr-x 2 root root 4096 Nov 23 01:33 proc
四、拷貝須要的文件和庫文件ide
#拷貝lib庫文件: [root@localhost user1]# pwd /vm/user1 [root@localhost user1]# cp -a /lib64/* lib64/ #注:如果32位,則拷貝/lib/*
#拷貝密碼文件: [root@localhost user1]# pwd /vm/user1 [root@localhost user1]# grep "user1:" /etc/passwd > etc/passwd [root@localhost user1]# cat etc/passwd user1:x:504:504::/home/user1:/bin/bash #只須要此用戶這一行便可
#拷貝dev和proc: [root@localhost user1]# CHROOT_DIR=/vm/user1/ [root@localhost user1]# mount proc $CHROOT_DIR/proc -t proc [root@localhost user1]# mount devpts $CHROOT_DIR/dev/pts -t devpts [root@localhost user1]# mknod $CHROOT_DIR/dev/ptmx c 5 2 [root@localhost user1]# chmod 666 /dev/ptmx #注意: mount proc $CHROOT_DIR/proc -t proc #必須建立這個proc文件系統,用戶登陸後能夠看到這個目錄中有不少文件/子目錄 mount devpts $CHROOT_DIR/dev/pts -t devpts #devpts文件系統,提供訪問終端的接口 mknod $CHROOT_DIR/dev/ptmx c 5 2 #必須有,用戶登陸後要使用它,做爲做爲僞終端的master設備
#拷貝腳本文件: [root@localhost user1]# cp -a /bin/bash bin/ #必需要拷的 [root@localhost user1]# cp -a /bin/ls bin/ [root@localhost user1]# cp -a /bin/cat bin/ #根據本身的需求拷貝相應的腳本文件
#修改權限: [root@localhost user1]# chown -R user1. /vm/user1/*
五、修改/etc/pam.d/sshd測試
[root@localhost user1]# vim /etc/pam.d/sshd 添加: session required pam_chroot.so
六、修改/etc/security/chroot.confui
[root@localhost user1]# vim /etc/security/chroot.conf 添加: user1 /vm/user1
七、重啓sshdspa
[root@localhost user1]# /etc/init.d/sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ]
八、測試:rest
[root@localhost ~]# ssh user1@localhost user1@localhost's password: Could not chdir to home directory /home/user1: No such file or directory -bash-4.1$ ls bin dev etc lib64 proc -bash-4.1$ ps -bash: ps: command not found -bash-4.1$ pwd / -bash-4.1$ cd / -bash-4.1$ ls bin dev etc lib64 proc -bash-4.1$
九、注意:token
以上方法只能使用原系統中/bin/目錄下的命令
對於/usr/bin/目錄下的命令目前還有問題,沒法使用
如:ssh命令,ssh命令須要用到/usr/lib64/目錄下的相應的庫文件
#使用如下腳本能夠實現將命令依賴的/usr/lib64/下的庫文件拷貝到活動目錄的lib64/目錄下 [root@localhost lib64]# vim abc.sh #!/bin/bash cmd="ssh" cd /vm/user1/lib64 for i in $cmd do ldd `which $i` |grep "/usr/lib64/"|sed 's/(.*//g'|awk '{print $NF}'|while read j do \cp -a $j ./ if [ `file $j | grep -c symbolic` -eq 1 ];then sourceFile=`ls -l $j |awk '{print $NF}'` \cp -a `dirname $j`/$sourceFile ./ fi done done
#建議使用此方法:將/usr/lib64/所有拷貝過去 [root@localhost user1]# pwd /vm/user1 [root@localhost user1]# mkdir usr [root@localhost user1]# cp -a /usr/lib64/ usr/
即使將ssh依賴的/usr/lib64/目錄下的庫文件所有拷貝過去,ssh命令的使用仍是有不少問題,尚待研究