操作系统:
[root@station28 ~]# cat /etc/issue.net
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Kernel \r on an \m
[root@station28 ~]# uname -a
Linux station28.example.com 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
在配置sftp时,需要chroot特定的目录,则openssh的版本需不低于5.1,我这下载的是openssh 6.1版本的源码包,需要进行编译,
则需要安装gcc,同时需要安装openssl-devel-0.9.8e-12.el5
openssh-6.1p1下载地址:或者进入官网下载
ftp://ftp.openbsd.org.ar/pub/OpenBSD/OpenSSH/portable/openssh-6.1p1.tar.gz
若不安装openssl-devel,则报如下错误:
[root@station28 openssh-6.1p1]#./configure --prefix=/usr/local/ssh --sysconfdir=/etc/ssh --with-zlib --with-ssl-dir=/usr/local/ssl--with-md5-passwords --mandir=/usr/share/man
checking for openssl/opensslv.h... no
configure: error: *** OpenSSL headers missing - please install first or check config.log ***
1、配置yum,安装gcc,openssl-devel-0.9.8e-12.el5,和telnet(当ssh不能用的时候,用于连接服务器) ####此步骤不需要
[root@station28 ~]# cat /etc/yum.repos.d/base.repo ####此步骤不需要
[base]
baseurl=file:///mnt/Server
gpgcheck=0
[root@station28 ~]# yum -y install "gcc*" ####此步骤不需要
[root@station28 ~]# yum -y install "openssl-devel-0.9.8e-12.el5" ####此步骤不需要
[root@station28 openssh-6.1p1]# yum -y install "telnet-server*" ####此步骤不需要
卸载openssh,观察openssh的各个包包含的内容: ####此步骤不需要
[root@station28 ~]# cd /etc/ssh ####此步骤不需要
[root@station28 ssh]# mkdir /root/1114_ssh_bak ####此步骤不需要
[root@station28 ssh]# cp * /root/1114_ssh_bak/ ####此步骤不需要
[root@station28 ssh]# rpm -qa | grep openssh //目前系统安装的是4.3版本的
openssh-server-4.3p2-36.el5
openssh-askpass-4.3p2-36.el5
openssh-4.3p2-36.el5
openssh-clients-4.3p2-36.el5
[root@station28 ~]# ssh -V
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
[root@station28 ssh]# rpm -ql openssh-server-4.3p2-36.el5 //查看安装包涉及的内容
/etc/pam.d/sshd
/etc/rc.d/init.d/sshd
/etc/ssh
/etc/ssh/sshd_config
/usr/libexec/openssh/sftp-server
/usr/sbin/.sshd.hmac
/usr/sbin/sshd
/usr/share/man/man5/sshd_config.5.gz
/usr/share/man/man8/sftp-server.8.gz
/usr/share/man/man8/sshd.8.gz
/var/empty/sshd
/var/empty/sshd/etc
/var/empty/sshd/etc/localtime
[root@station28 ssh]# rpm -ql openssh-askpass-4.3p2-36.el5
/etc/profile.d/gnome-ssh-askpass.csh
/etc/profile.d/gnome-ssh-askpass.sh
/usr/libexec/openssh/gnome-ssh-askpass
/usr/libexec/openssh/ssh-askpass
[root@station28 ssh]# rpm -ql openssh-4.3p2-36.el5
/etc/ssh
/etc/ssh/moduli
/usr/bin/ssh-keygen
/usr/libexec/openssh
/usr/libexec/openssh/ssh-keysign
.....
....
[root@station28 ssh]# rpm -ql openssh-clients-4.3p2-36.el5
/etc/ssh/ssh_config
/usr/bin/.ssh.hmac
/usr/bin/scp
/usr/bin/sftp
/usr/bin/slogin
/usr/bin/ssh
/usr/bin/ssh-add
/usr/bin/ssh-agent
/usr/bin/ssh-copy-id
/usr/bin/ssh-keyscan
......
.....
[root@station28 ssh]# rpm -qa | grep openssh //全部卸载
openssh-server-4.3p2-36.el5
openssh-askpass-4.3p2-36.el5
openssh-4.3p2-36.el5
openssh-clients-4.3p2-36.el5
[root@station28 ssh]# rpm -e openssh-server-4.3p2-36.el5
[root@station28 ssh]# rpm -e openssh-askpass-4.3p2-36.el5
[root@station28 ssh]# rpm -e openssh-4.3p2-36.el5 //存在依赖关系,需要先卸载openssh-clients-4.3p2-36.el5.x86_64
error: Failed dependencies:
openssh = 4.3p2-36.el5 is needed by (installed) openssh-clients-4.3p2-36.el5.x86_64
[root@station28 ssh]# rpm -e openssh-clients-4.3p2-36.el5
[root@station28 ssh]# rpm -e openssh-4.3p2-36.el5
[root@station28 ssh]# rpm -qa | grep openssh //全部卸载干净
[root@station28 ~]# pwd
/root
[root@station28 ~]# ls openssh-6.1p1.tar.gz
openssh-6.1p1.tar.gz
[root@station28 ~]# tar -zxpf openssh-6.1p1.tar.gz //解压openssh 6.1
[root@station28 ~]# cd openssh-6.1p1 //执行以下命令进行编译
[root@station28 openssh-6.1p1]# ./configure --prefix=/usr/local/ssh --sysconfdir=/etc/ssh --with-zlib --with-ssl-dir=/usr/local/ssl --with-md5-passwords --mandir=/usr/share/man
[root@station28 openssh-6.1p1]# make
[root@station28 openssh-6.1p1]# make install //warn的报错忽略
[root@station28 openssh-6.1p1]# cd /usr/local/ssh/bin //刚用--frefix参数指定的路径/usr/local/ssh,将其bin下的拷贝到/usr/bin下
[root@station28 bin]# ls
scp sftp slogin ssh ssh-add ssh-agent ssh-keygen ssh-keyscan
[root@station28 bin]# cp * /usr/bin/
[root@station28 bin]# cd /usr/local/ssh/sbin
[root@station28 sbin]# ls
sshd
[root@station28 sbin]# cp sshd /usr/sbin/sshd //同理
[root@station28 sbin]# cd /root/openssh-6.1p1/contrib/redhat/ //将sshd.init拷贝到/etc/init.d/下,名字为sshd(ssh的daemon名称)
[root@station28 redhat]# ls
gnome-ssh-askpass.csh gnome-ssh-askpass.sh openssh.spec sshd.init sshd.init.old sshd.pam sshd.pam.old
[root@station28 redhat]# cp sshd.init /etc/init.d/sshd
[root@station28 redhat]# ls -l /etc/init.d/sshd
-rwxr-xr-x 1 root root 1768 Nov 14 23:21 /etc/init.d/sshd
[root@station28 redhat]# chkconfig --add sshd
[root@station28 redhat]# service sshd restart
[root@station28 ~]# service sshd restart //提示报错,只要touch该文件即可规避
Stopping sshd: [ OK ]
lstat(/etc/ssh/ssh_host_ecdsa_key.pub) failed: No such file or directory
Starting sshd: [ OK ]
[root@station28 ~]# touch /etc/ssh/ssh_host_ecdsa_key.pub
[root@station28 ~]# service sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
[root@station28 ~]# ssh -V //安装成功
OpenSSH_6.1p1, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
一、限制 sftp 用户登陆后,只能在家目录下活动,不能到其他或上级目录,可以按照下面方法进行操作:
一、升级ssh软件,版本必须在 4.8 以上;
[root@station28 ~]# ssh -V //安装成功
OpenSSH_6.1p1, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
二、备份配置文件:(以下所有命令,都需要使用root用户来执行)
cd /etc/ssh/
mv sshd_config sshd_config.bak
mv sshd_config.rpmnew sshd_config
三、修改 /etc/ssh/sshd_config
#Subsystem sftp /usr/libexec/openssh/sftp-server (这行必须注释掉)
Subsystem sftp internal-sftp
Match Group chroot(此group可更改)
ChrootDirectory /chroot/%u(此目录可更改,不过需要存在)
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
说明:本配置中,ChrootDirectory 是 /chroot/%u,匹配这个规则的组名为 chroot,所以以后添加需要限制活动目录的用户时,必须属于chroot这个组;
四、建立相关目录,添加用户和组,这里以work用户为例:
cd /
mkdir chroot
groupadd chroot
useradd -d /chroot/work(创建work用户目录) -g chroot (添加属组) work
passwd work (设置密码)
chown root /chroot/work
chmod 750 /chroot/work
说明:在/chroot目录下,用户家目录 work 的属主必须是root,而不能是用户本身,属组的话,必须是chroot,权限 750 (需要注意的是,新建用户的家目录,权限是700,
但这样sftp登陆的话,是看不到目录下的列表的,所以,必须是750或755的权限) 例如:
1 drwxr-x--- 7 root chroot 4096 Aug 5 13:23 work
五、如果需要上传文件,则按照下面执行:
1 cd /chroot/work
2 mkdir in
3 chmod –R 777 in
此时,in目录为可读写权限,可以用来上传
六、测试
1 service sshd restart
2 sftp work@ip
阅读(3747) | 评论(0) | 转发(0) |