ssh 设置chroot后无法进入shell 提示PTY allocation request failed on channel 0

2,102 views
Skip to first unread message

Qf Yang

unread,
Nov 7, 2011, 8:33:23 AM11/7/11
to sh...@googlegroups.com
vps上,给朋友开了ssh账号供ssh -D之用,他只需要ssh转发功能,别不都用不着;所以想限制不让他进入其它目录。是基于openVZ的vps,好像不能启用ACL;
于是考虑对这些账号启用chroot。主要做了以下设置:
1. 创建 /var/chroot目录,并创建以下子目录 bin  dev  etc  home  lib  usr ,将/bin、/lib 下的文件(不包括子目录)拷进去,另外导入/etc/passwd中相应用户记录到/var/chroot/etc/passwd
2. 文件 /etc/security/chroot.conf 添加
username  /var/chroot
3. /etc/pam.d/sshd 追加一行
session required pam_chroot.so

再通过ssh登录设置chroot的账号,输入密码后,只有一行提示
PTY allocation request failed on channel 0
不能进入shell

哪位大哥帮忙分析一下是什么问题,怎样解决,多谢!



Chaos Eternal

unread,
Nov 7, 2011, 8:42:42 AM11/7/11
to sh...@googlegroups.com
ssh -T -D xx

2011/11/7 Qf Yang <fen...@gmail.com>:

Qf Yang

unread,
Nov 7, 2011, 9:32:13 AM11/7/11
to sh...@googlegroups.com
ssh -T -D xx
是本地配置ssh转发端口时加上 -T 参数吗? 不过这个账号登录的话,还是能进入其它目录的。
就是想让这些账号不能查看其它目录文件的。

不知道是不是还有其它的办法,不允许用户访问指定目录。linux的rwxrwxrwx文件权限,个人感觉在精确设置目录权限时实在是有些力不从心,是burst.com的vps,基于openVZ,使用centos 6。
fstab如下,感觉是不是不能启用ACL?

# /etc/fstab
# Created by anaconda on Wed Jul 13 17:50:36 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
none                  /dev/pts                devpts  rw  0 0

Mo Mo

unread,
Nov 7, 2011, 11:43:45 AM11/7/11
to sh...@googlegroups.com

chsh为不可用的。

Qf Yang

unread,
Nov 7, 2011, 8:33:30 PM11/7/11
to sh...@googlegroups.com
我把/usr/bin/chsh, /etc/shells 复制到chroot目录,但还是不行,使用putty连接时提示“Server refused to allocate pty” (昨天是使用linux下ssh连接的)

Qf Yang

unread,
Nov 7, 2011, 8:56:03 PM11/7/11
to sh...@googlegroups.com
日志 /var/log/secure 里有如下的消息

Nov  7 20:52:56 v sshd[22229]: pam_unix(sshd:session): session opened for user proxy by (uid=0)
Nov  7 20:52:56 v sshd[22229]: pam_chroot(sshd:session): chroot(/var/chroot) succeeded
Nov  7 20:52:56 v sshd[22232]: pam_env(sshd:setcred): Unable to open config file: /etc/security/pam_env.conf: No such file or directory
Nov  7 20:52:57 v sshd[22229]: error: openpty: No such file or directory
Nov  7 20:52:57 v sshd[22232]: error: session_pty_req: session 0 alloc failed

陈若涵

unread,
Nov 7, 2011, 9:14:12 PM11/7/11
to sh...@googlegroups.com
# man ssh
-f      Requests ssh to go to background just before command execution.
-N      Do not execute a remote command.  This is useful for just forwarding ports (protocol version 2 only).
-n      Redirects stdin from /dev/null (actually, prevents reading from stdin).  This must be used when ssh is run in the background.
-T      Disable pseudo-tty allocation.
-q      Quiet mode.  Causes most warning and diagnostic messages to be suppressed.

使用这些参数就可以了,即使是nologin shell也能forward ports。。当然也能-D。。


-----------
悲观的思考,乐观的生活.我们既需要思考的深度,也需要生活的温度!
@ruohanc



2011/11/8 Qf Yang <fen...@gmail.com>

Qf Yang

unread,
Nov 7, 2011, 9:49:07 PM11/7/11
to sh...@googlegroups.com
ssh的使用我知道的,但这个ssh账号是给朋友用的。大多数人的对代码、命令行  带着“天生的”恐惧心理。所以我推荐他使用tunnelier(windows下)连接ssh并建立本地转发端口。tunnelier本身带有terminal与sftp客户端功能,通过sftp客户端是可以很容易进入其它目录(我不想让他看到其它的文件,所以才想给他的账号使用chroot)
也就是说,我的实际目的是:限制用户,不让他看到某些文件,最好是让他只能看到指定的文件。
我目前所知的是:
  a. linux的rwxrwxrwx权限对于精确限制用户权限而言,实在不怎么方便。
  b. 因为是基于openVZ的vps,似乎不能启用ACL。(此路不通)
  c. selinux,vps上好像也不能启用。(也不通)
  d. chroot,限定在指定目录,应该是最好的方案。
所以我选择chroot的方案。
是否还有其它的方案,也可以实现这个目的?

实际上,按现在的情况,chroot的用户不能进入shell,通过tunnelier也不能打开shell, 也不能进入sftp;也就是已经实现了我的需求。
不过,ssh的chroot功能却没有配置好,不知道问题出现在哪里,所以在这里问问大家,也是探索学习了。

Chaos Eternal

unread,
Nov 7, 2011, 10:03:46 PM11/7/11
to sh...@googlegroups.com
哦,这个态度好。

那这样吧,麻烦你翻译一下 “Server refused to allocate pty” 是什么意思?


2011/11/8 Qf Yang <fen...@gmail.com>:

Qf Yang

unread,
Nov 7, 2011, 10:39:54 PM11/7/11
to sh...@googlegroups.com
Server refused to allocate pty
服务器拒绝分配伪终端,是不是也要在chroot/dev/ 下创建pty的设备文件,这我还真不会;刚才google一下,还没找到方法,可否提示一下?多谢!

Chaos Eternal

unread,
Nov 7, 2011, 10:44:23 PM11/7/11
to sh...@googlegroups.com
我给你的第一个回复邮件里面就写了 ssh -T

以下是ssh的manpage里面摘录的:

-T Disable pseudo-tty allocation.

你再翻译一下?


2011/11/8 Qf Yang <fen...@gmail.com>:

Qf Yang

unread,
Nov 7, 2011, 11:00:15 PM11/7/11
to sh...@googlegroups.com
ssh 加上 -T 参数可以正常连接到服务器上,看到的文件确实是chroot后的;那能不能让chroot用户不加 -T参数就能连接,是不是要在chroot/dev下创建设备文件呢?

Chaos Eternal

unread,
Nov 7, 2011, 11:06:33 PM11/7/11
to sh...@googlegroups.com
那你应该做的是在putty里面找到加上T的办法。

putty的菜单不长,很容易找到的。找到后作成配置导出给你的用户就可以了。

==
至于用户登陆后能执行命令,这个前面有另一个人给你解答了。


2011/11/8 Qf Yang <fen...@gmail.com>:

Qf Yang

unread,
Nov 7, 2011, 11:16:21 PM11/7/11
to sh...@googlegroups.com
我只在windows下使用putty;这个强制chroot的账号只给朋友用的,已经教会他使用tunnelier. 我自己用(不经过chroot)的账号作 -D转发。
朋友不需要登录到vps上执行命令,只不过我想探索一下怎么让chroot的账号正常使用登录使用基本命令。
多谢帮忙!

陈若涵

unread,
Nov 8, 2011, 11:01:28 AM11/8/11
to sh...@googlegroups.com
我并没有远程chroot的经验,不过根据gentoo安装过程的chroot设置来看,,你至少需要用mount rbind /dev /proc文件夹

Qf Yang

unread,
Nov 8, 2011, 8:20:01 PM11/8/11
to sh...@googlegroups.com
哈哈,好了!
先用root用户把 /proc /dev mount 到chroot下的相应目录上,然后(经过chroot的)账号登录就可以正常登录!
[root@v chroot]# mount -o bind /proc/ /var/chroot/proc/
[root@v chroot]# mount -o bind /dev/ /var/chroot/dev/

太感谢了!

Lu Yiguang

unread,
Nov 8, 2011, 1:11:50 PM11/8/11
to sh...@googlegroups.com
这么看着好累。。话说好像是需要绑定以下/dev和/proc,但是这地方我也是小白也不熟悉,不知道重新绑定之后会不会有什么预料不到的后果。。

2011/11/9 陈若涵 <crha...@gmail.com>

Qf Yang

unread,
Nov 8, 2011, 10:18:59 PM11/8/11
to sh...@googlegroups.com
我mount bind后,chroot用户可以正常的ssh登录vps,但是使用tunnelier 不能作ssh转发了,准备晚上回去再研究一下。tunnelier 消息如下
09:46:43.656 Authentication completed.
09:46:43.656 Initializing SOCKS / HTTP CONNECT proxy on 127.0.0.1:7070 succeeded.
09:47:05.125 Closing SOCKS5 connection from 127.0.0.1:4082, sent: 0, received: 0.
09:47:05.125 SOCKS5 connection from 127.0.0.1:4082 failed: Client connection closed before completion of protocol.
09:47:05.531 Closing SOCKS5 connection from 127.0.0.1:4098, sent: 0, received: 0.
09:47:05.546 SOCKS5 connection from 127.0.0.1:4098 failed: Client connection closed before completion of protocol.
09:47:06.093 Closing SOCKS5 connection from 127.0.0.1:4112, sent: 0, received: 0.
09:47:06.093 Closing SOCKS5 connection from 127.0.0.1:4142, sent: 0, received: 0.
09:47:06.140 Closing SOCKS5 connection from 127.0.0.1:4159, sent: 0, received: 0.
09:47:06.203 Closing SOCKS5 connection from 127.0.0.1:4172, sent: 0, received: 0.
09:47:06.218 SOCKS5 connection from 127.0.0.1:4112 failed: Client connection closed before completion of protocol.
09:47:06.218 SOCKS5 connection from 127.0.0.1:4142 failed: Client connection closed before completion of protocol.
09:47:06.218 SOCKS5 connection from 127.0.0.1:4159 failed: Client connection closed before completion of protocol.
09:47:06.218 SOCKS5 connection from 127.0.0.1:4172 failed: Client connection closed before completion of protocol.
09:47:06.640 Closing SOCKS5 connection from 127.0.0.1:4204, sent: 0, received: 0.
09:47:06.640 SOCKS5 connection from 127.0.0.1:4204 failed: Client connection closed before completion of protocol.
09:47:07.218 Closing SOCKS5 connection from 127.0.0.1:4214, sent: 0, received: 0.
09:47:07.234 SOCKS5 connection from 127.0.0.1:4214 failed: Client connection closed before completion of protocol.

Úcarenya

unread,
Nov 9, 2011, 1:15:43 PM11/9/11
to sh...@googlegroups.com
On 02:11 Wed 09 Nov , Lu Yiguang wrote:
> 这么看着好累。。话说好像是需要绑定以下/dev和/proc,但是这地方我也是小白也不熟悉
> ,不知道重新绑定之后会不会有什么预料不到的后果。。
>
> 2011/11/9 陈若涵 <crha...@gmail.com>
>
> 我并没有远程chroot的经验,不过根据gentoo安装过程的chroot设置来看,,你至少
> 需要用mount rbind /dev /proc文件夹
>
> 参考: http://www.gentoo.org/doc/en/handbook/handbook-amd64.xml?full=1#
> book_part1_chap6
>
> -----------
> 悲观的思考,乐观的生活.我们既需要思考的深度,也需要生活的温度!
> @ruohanc
>
> 2011/11/8 Qf Yang <fen...@gmail.com>
>
> 我只在windows下使用putty;这个强制chroot的账号只给朋友用的,已经教会他
> 使用tunnelier. 我自己用(不经过chroot)的账号作 -D转发。
> 朋友不需要登录到vps上执行命令,只不过我想探索一下怎么让chroot的账号正常
> 使用登录使用基本命令。
> 多谢帮忙!
>
> 在 2011年11月8日下午12:06,Chaos Eternal <chaose...@shlug.org>写道:
>
> 那你应该做的是在putty里面找到加上T的办法。
>
> putty的菜单不长,很容易找到的。找到后作成配置导出给你的用户就可以了
> 。
>
> ==
> 至于用户登陆后能执行命令,这个前面有另一个人给你解答了。
>
> 2011/11/8 Qf Yang <fen...@gmail.com>:
> > ssh 加上 -T 参数可以正常连接到服务器上,看到的文件确实是chroot后
> 的;那能不能让chroot用户不加
> > -T参数就能连接,是不是要在chroot/dev下创建设备文件呢?
> >
> > 在 2011年11月8日上午11:44,Chaos Eternal <chaose...@shlug.org>

> 写道:
> >>
> >> 我给你的第一个回复邮件里面就写了 ssh -T
> >>
> >> 以下是ssh的manpage里面摘录的:
> >>
> >> -T Disable pseudo-tty allocation.
> >>
> >> 你再翻译一下?
> >>
> >> 2011/11/8 Qf Yang <fen...@gmail.com>:
> >> > Server refused to allocate pty
> >> > 服务器拒绝分配伪终端,是不是也要在chroot/dev/
> >> > 下创建pty的设备文件,这我还真不会;刚才google一下,还没找到方
> 法,可否提示一下?多谢!
> >> >
> >> > 在 2011年11月8日上午11:03,Chaos Eternal <
> >> >> > 在 2011年11月8日上午10:14,陈若涵 <crha...@gmail.com>写道
> >> >> >>> 在 2011年11月8日上午9:33,Qf Yang <fen...@gmail.com>写道

> :
> >> >> >>>>
> >> >> >>>> 我把/usr/bin/chsh, /etc/shells 复制到chroot目录,但还是
> 不行,使用putty连接时提示“Server
> >> >> >>>> refused to allocate pty” (昨天是使用linux下ssh连接的)
> >> >> >>>>
> >> >> >>>> 在 2011年11月8日上午12:43,Mo Mo <mobei...@gmail.com>

> 写道:
> >> >> >>>>>
> >> >> >>>>> chsh为不可用的。
> >> >> >>>>>
> >> >> >>>>> On Nov 7, 2011 9:33 PM, "Qf Yang" <fen...@gmail.com>
> wrote:
> >> >> >>>>>>
> >> >> >>>>>> vps上,给朋友开了ssh账号供ssh
> >> >> >>>>>> -D之用,他只需要ssh转发功能,别不都用不着;所以想限制
> 不让他进入其它目录。是基于openVZ的vps,好像不能启用ACL;
> >> >> >>>>>> 于是考虑对这些账号启用chroot。主要做了以下设置:
> >> >> >>>>>> 1. 创建 /var/chroot目录,并创建以下子目录 bin dev
> etc home lib usr
> >> >> >>>>>> ,将/bin、/lib
> >> >> >>>>>> 下的文件(不包括子目录)拷进去,另外导入/etc/passwd中
> 相应用户记录到/var/chroot/etc/passwd
> >> >> >>>>>> 2. 文件 /etc/security/chroot.conf 添加
> >> >> >>>>>> username /var/chroot
> >> >> >>>>>> 3. /etc/pam.d/sshd 追加一行
> >> >> >>>>>> session required pam_chroot.so
> >> >> >>>>>>
> >> >> >>>>>> 再通过ssh登录设置chroot的账号,输入密码后,只有一行提
> 示
> >> >> >>>>>> PTY allocation request failed on channel 0
> >> >> >>>>>> 不能进入shell
> >> >> >>>>>>
> >> >> >>>>>> 哪位大哥帮忙分析一下是什么问题,怎样解决,多谢!
> >> >> >>>>>>
> >> >> >>>>
> >> >> >>>
> >> >> >>
> >> >> >
> >> >
> >
>
ssh 不知道会有什么不同,一般Linux chroot需要的就这两:

mount -t proc none /MOUNTED/TARGET/proc
mount -o bind /dev /MOUNTED/TARGET/dev

然后chroot

然后看你的系统,怎样把那些环境导入. Gentoo的话:

env-update && source /etc/profile


--
- Úcarenya - infernoxu at gmail dot com
- http://ucarenya.com/

Qf Yang

unread,
Nov 9, 2011, 12:39:47 AM11/9/11
to sh...@googlegroups.com
 mount -t proc none /MOUNTED/TARGET/proc
 mount -o bind /dev /MOUNTED/TARGET/dev
感觉这样应该更好吧,/path/to/chroot/proc与真实的/proc 隔离,经测试可以正常进入shell
chroot后,要能够使用基本的命令,所以才把/bin, /lib,里的文件拷过去,/bin目录mount bind过去应该也可以,没有测试

Qf Yang

unread,
Nov 9, 2011, 12:44:01 AM11/9/11
to sh...@googlegroups.com
而且被chroot的账号,也可以正常使用tunnelier正常转发了
Reply all
Reply to author
Forward
0 new messages