您的位置:首页 > 其它

Windows下通过cygwin架设sshd服务

2013-05-19 18:13 477 查看
Windows下通过cygwin架设sshd服务 转载:http://hi.baidu.com/qiraosky/blog/item/35a7f144d9f3488ab3b7dc2e.html
版权归 原创作者所有。
sshd服务是一种安全连接,它能让你访问服务器上的命令行界面。Windows本身没有提供该服务,所以我们可以通过 cygwin 来进行安装。安装 cygwin

安装
用管理员用户登录,启动 cygwin 命令行,执行以下命令。
$ ssh-host-config

Shall privilege separation be used? (yes/no) yes

Do you want to install sshd as service?
(Say "no" if it's already installed as service) (yes/no) yes

Which value should the environment variable CYGWIN have when
sshd starts? It's recommended to set at least "ntsec" to be
able to change user context without password.
Default is "ntsec". CYGWIN=binmode ntsec tty

The service has been installed under LocalSystem account.
To start the service, call `net start sshd' or `cygrunsrc -S ssdh'.

Host configuration finished. Have fun!配置 sshd
在 cygwin 的命令行中输入以下命令:
$ cd /etc
$ chmod 666 sshd_config
$ vi sshd_config
修改 sshd_config 的以下配置。
PermitRootLogin no # 禁止root登录
StrictModes yes # CYGWIN=ntsec时的安全配置
RhostsRSAAuthentication no # 禁止 rhosts 认证
IgnoreRhosts yes # 禁止 rhosts 认证
PasswordAuthentication no # 禁止密码认证
ChallengeResponseAuthentication no # 禁止密码认证
PermitEmptyPasswords no # 禁止空密码用户登录
最后将 sshd_config 的权限修改回 644。
$ chmod 644 sshd_config
启动 sshd 服务器。
$ cygrunsrv -S sshd生成公钥和密钥
由于我们上面的设置仅允许密钥方式认证,所以要为我们的用户生成一对公钥和密钥。
在 cygwin 的控制台中执行以下命令,生成 ssh1 的公钥和密钥。
$ ssh-keygen -t rsa1
Generating public/private rsa1 key pair.
Enter file in which to save the key (/home/charlee/.ssh/identity):
Enterpassphrase (empty for no passphrase): 输入密码
Enter same passphrase again: 再次输入密码
Your identification has been sabed in /home/charlee/.ssh/identity
Your public key has been saved in /home/charlee/.ssh/identity.pub
类似的方法,使用下面的命令生成 ssh2 的公钥和密钥。
$ ssh-keygen -t rsa
$ ssh-keygen -t dsa
将公钥导入到认证公钥中:
$ cd .ssh
$ cat identity.pub >> authorized_keys
$ cat id_rsa.pub >> authorized_keys
$ cat id_dsa.pub >> authorized_keys
因为我们在 /etc/sshd_config 的配置中使用了 StrictModes yes 的设置,所以要修改目录权限,命令如下。
$ chmod 755 /home/charlee
然后将密钥 identity、id_rsa、id_dsa 文件用某种方式复制到客户端。我使用的客户端是 Linux,因此只要将这三个文件复制到客户端的 $HOME/.ssh 目录下即可。
登录服务器。在客户端上输入以下命令,即可登录服务器。
$ ssh 192.168.0.2常见问题
2008-12-11更新
Q: cygrunsrv -S sshd不能启动,报告
cygrunsrv: Error starting a service: QueryServiceStatus: Win32 error 1062:
The service has not been started.
A: 很可能是/var/log的权限设置不正确。首先执行 mkpasswd 和 mkgroup 重新生成权限信息,再删除sshd服务,重新配置:
$ mkpasswd -l > /etc/passwd
$ mkgroup -l > /etc/group
$ cygrunsrv -R sshd
$ ssh-host-config -y
$ cygrunsrv -S sshd
Q: 用公钥登录时老是说Permission denied (publickey).,怎么办?
A: 可以在Windows的事件日志(我的电脑->右键->管理->事件查看器)中看到sshd产生的错误信息。常见的问题是 .ssh/authorized_keys权限设置不正确,该文件必须设置为 0644 才能正常登录

我遇到的问题
sshd服务无法开启?
解决,按照上面的提示
1)对/var/log目录 赋权
2)执行用户映射
3)删除sshd服务
4)配置sshd服务
5)开启sshd服务

1)对/var/log目录 赋权

Administrator@8a0dbeec9a074e1 /var
$ chown Administrator:Administrators /log

Administrator@8a0dbeec9a074e1 /var/log

$ chown -R Administrator:Administrators *
Administrator@8a0dbeec9a074e1 /var/log
$ ls -rtl

2)执行用户映射
Administrator@8a0dbeec9a074e1 ~
$ mkpasswd -l > /etc/passwd

Administrator@8a0dbeec9a074e1 ~
$ mkgroup -l > /etc/group

3)删除sshd服务
Administrator@8a0dbeec9a074e1 ~
$ cygrunsrv -R sshd

4)配置sshd服务

Administrator@8a0dbeec9a074e1 ~
$ ssh-host-config -y
*** Query: Overwrite existing /etc/ssh_config file? (yes/no) yes
*** Info: Creating default /etc/ssh_config file
*** Query: Overwrite existing /etc/sshd_config file? (yes/no) yes
*** Info: Creating default /etc/sshd_config file
*** Info: Privilege separation is set to yes by default since OpenSSH 3.3.
*** Info: However, this requires a non-privileged account called 'sshd'.
*** Info: For more info on privilege separation read /usr/share/doc/openssh/READ
ME.privsep.
*** Query: Should privilege separation be used? (yes/no) yes
*** Info: Updating /etc/sshd_config file
*** Query: Overwrite existing /etc/inetd.d/sshd-inetd file? (yes/no) yes
*** Info: Creating default /etc/inetd.d/sshd-inetd file
*** Info: Updated /etc/inetd.d/sshd-inetd

*** Warning: The following functions require administrator privileges!

*** Query: Do you want to install sshd as a service?
*** Query: (Say "no" if it is already installed as a service) (yes/no) yes
*** Query: Enter the value of CYGWIN for the daemon: []

*** Info: The sshd service has been installed under the LocalSystem
*** Info: account (also known as SYSTEM). To start the service now, call
*** Info: `net start sshd' or `cygrunsrv -S sshd'. Otherwise, it
*** Info: will start automatically after the next reboot.

*** Info: Host configuration finished. Have fun!

5)开启sshd服务
Administrator@8a0dbeec9a074e1 ~
$ cygrunsrv -S sshd

Administrator@8a0dbeec9a074e1 ~
$ cygrunsrv -S sshd

Administrator@8a0dbeec9a074e1 ~
$ ps -ef|grep sshd
SYSTEM 1316 6200 ? 19:42:30 /usr/sbin/sshd

Administrator@8a0dbeec9a074e1 ~
$ ssh root@172.16.4.132
The authenticity of host '172.16.4.132 (172.16.4.132)' can't be established.
RSA key fingerprint is b0:f3:36:d2:46:45:2f:04:7f:50:0b:e4:81:f2:5d:bf.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.4.132' (RSA) to the list of known hosts.
root@172.16.4.132's password:
____________________, ______________________________________
.QQQQQQQQQQQQQQQQQQQQQQQQL_ | |
.gQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ__ | |
gQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ== | _.---.) |
QQQQQQQQQQQQQQQQQQQQQQQQQQQF= | (^--^)_.-" `; |
QQQQQQQQQ================! | ) ee ( | |
QQQQQQQQ | (_.__._) / |
QQQQQQQQ | `--', ,' |
QQQQQQQQ ~"jjj__, | jgs )_|--')_| |
QQQQQQQQ "jjjjjjjjjj___ | ""' ""' |
QQQQQQQQ ~jjjjjjjjjjjjjjjjj__ | |
QQQQQQQQ _jjjjjjjjjjjjjj/~~~~ | The Hippo says: Welcome to |
QQQQQQQQ .{jjjjjjj/~~~~~ | _ |
QQQQQQQQ .{/~~~~` | ____ _ _ ____ _ _ _ (_) ____ |
QQQQQQQQ | / ___)| | | | / _ || | | || || _ \ |
QQQQQQQQ |( (___ | |_| |( (_| || | | || || | | ||
QQQQQQQQQL_______________, | \____) \__ | \___ | \___/ |_||_| |_||
QQQQQQQQQQQQQQQQQQQQQQQQQQQL___ | (___/ (____| |
4QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ___ | |
(=QQQQQQQQQQQQQQQQQQQQQQQQQQQQQ==== | -.-. -.-- --. .-- .. -. |
(QQQQQQQQQQQQQQQQQQQQQQQQF= |______________________________________|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Windows 服务 cygwin