<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Stone &#187; OpenSSH</title>
	<atom:link href="http://phpstone.com/tag/openssh/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpstone.com</link>
	<description>——从石头向宝石进化</description>
	<lastBuildDate>Thu, 05 Aug 2010 14:04:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>在Windows上使用Putty通过OpenSSH安全地登陆FreeBSD</title>
		<link>http://phpstone.com/using-putty-on-windows-to-login-freebsd-securely-via-openssh/</link>
		<comments>http://phpstone.com/using-putty-on-windows-to-login-freebsd-securely-via-openssh/#comments</comments>
		<pubDate>Sat, 16 May 2009 17:05:11 +0000</pubDate>
		<dc:creator>frePerl</dc:creator>
				<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[OpenSSH]]></category>
		<category><![CDATA[putty]]></category>

		<guid isPermaLink="false">http://phpstone.com/?p=259</guid>
		<description><![CDATA[看过delphij的实现安全的三种途径这篇文章后决定将FreeBSD服务器的SSH登录改为使用密钥认证的方式；另外也是因为看到 /var/log/messages 里满是这样的记录：
May  1 13:00:12 stone sshd[12296]: error: PAM: authentication error for illegal user admin from 220.173.60.209
May  1 13:01:24 stone sshd[12301]: error: PAM: authentication error for illegal user admin from 204.8.216.165
May  1 13:01:57 stone sshd[12307]: error: PAM: authentication error for illegal user admin from 77.243.236.32
测试环境：FreeBSD 7.0-RELEASE Putty 0.60
OpenSSH 是一组用于安全地访问远程计算机的连接工具。它可以作为 rlogin、 rsh rcp [...]]]></description>
			<content:encoded><![CDATA[<p>看过delphij的<a href="http://blog.delphij.net/archives/2009/05/post-561.html">实现安全的三种途径</a>这篇文章后决定将FreeBSD服务器的SSH登录改为使用密钥认证的方式；另外也是因为看到 /var/log/messages 里满是这样的记录：</p>
<blockquote><p>May  1 13:00:12 stone sshd[12296]: error: PAM: authentication error for illegal user admin from 220.173.60.209<br />
May  1 13:01:24 stone sshd[12301]: error: PAM: authentication error for illegal user admin from 204.8.216.165<br />
May  1 13:01:57 stone sshd[12307]: error: PAM: authentication error for illegal user admin from 77.243.236.32</p></blockquote>
<p>测试环境：FreeBSD 7.0-RELEASE Putty 0.60<span id="more-259"></span></p>
<p>OpenSSH 是一组用于安全地访问远程计算机的连接工具。它可以作为 rlogin、 rsh rcp 以及 telnet 的直接替代品使用。更进一步， 其他任何 TCP/IP 连接都可以通过 SSH 安全地进行隧道/转发。 OpenSSH 对所有的传输进行加密， 从而有效地阻止了窃听、 连接劫持，以及其他网络级的攻击。</p>
<h3>1. 确保sshd正常启用</h3>
<p>查看 /etc/rc.conf  是否存在 sshd_enable=&#8221;YES&#8221; ；如果没有添加进去并人工方式启动#/etc/rc.d/sshd start ；然后查询 22端口是否启动：#sockstat -4l</p>
<blockquote><p>root     sshd       8085  4  tcp4   *:22                  *:*</p></blockquote>
<h3>2. 配置 OpenSSH</h3>
<p>针对 OpenSSH 服务程序和客户端的系统级配置文件在 /etc/ssh 目录中。ssh_config 用于配置客户端的设定， 而 sshd_config 则用于配置服务器端。</p>
<p>现在来说通过配置 OpenSSH 使服务器接受公钥（public-key）认证。sshd_config 这个文件指定的一些默认值都是生效的，虽然这些选项被注释掉了，如果你想改变默认值的话，那就取消注释更改值或者直接添加相同的选项和值覆盖默认值的作用。</p>
<p>#vi /etc/ssh/sshd_config</p>
<blockquote><p>#默认 SSH 端口为 22 ，如果需要可以更改<br />
Port 22</p>
<p>#仅接受版本2的密钥，默认值<br />
Protocol 2</p>
<p>#绝对不要允许 root 用户直接通过网络登录，默认值<br />
PermitRootLogin no<br />
StrictModes yes<br />
MaxAuthTries 6</p>
<p>#启用公钥认证<br />
RSAAuthentication no         #默认值为 yes<br />
PubkeyAuthentication yes   #默认值</p>
<p>#不使用基于主机的认证，加强 OpenSSH 的安全性<br />
RhostsRSAAuthentication no   #默认值<br />
HostbasedAuthentication no   #默认值<br />
IgnoreUserKnownHosts yes     #默认值为 no<br />
PermitEmptyPassword no       #默认值</p>
<p>#禁用 PAM 认证即口令形式认证 默认值为yes<br />
ChallengeResponseAuthentication no</p></blockquote>
<p>编辑完成后重启 sshd 进程：`#/etc/rc.d/sshd restart` ；Linux 系统上一般是 `/etc/init.d/ssh restart`</p>
<p><strong>注意：</strong>因为是通过 SSH 口令认证的方式连接到服务器来操作，操作ChallengeResponseAuthentication 这个选项改为 no 后不能退出登陆，继续下面操作，或者也可以在成功验证了密钥后再改这个选项。以免出错导致无法远程SSH登录服务器。</p>
<h3>3. 生成公钥（public key）和私钥（private key）</h3>
<p>用需要生成密钥的用户通过 SSH 密码认证的方式登录<br />
FreeBSD 使用 ssh-keygen 来生成 DSA 或 RSA 密钥对用于验证用户的身份：</p>
<blockquote><p>% ssh-keygen -t rsa<br />
Generating public/private dsa key pair.</p>
<p>#不输入即使用默认<br />
Enter file in which to save the key (/home/user/.ssh/id_rsa):<br />
Created directory &#8216;/home/user/.ssh&#8217;.</p>
<p>#密钥的密码，需要输入并记住<br />
Enter passphrase (empty for no passphrase):<br />
Enter same passphrase again:<br />
Your identification has been saved in /home/user/.ssh/id_rsa.<br />
Your public key has been saved in /home/user/.ssh/id_rsa.pub.<br />
The key fingerprint is:<br />
bb:48:db:f2:93:57:80:b6:aa:bc:f5:d5:ba:8f:79:17 user@host.example.com</p></blockquote>
<p>ssh-keygen 会生成一个包含公私钥对用于验证身份。 私钥将保存到 ~/.ssh/id_dsa 或 ~/.ssh/id_rsa， 而公钥则被存放到 ~/.ssh/id_dsa.pub 或 ~/.ssh/id_rsa.pub，文件名取决于您选择的 DSA 和 RSA 密钥类型。RSA 或者 DSA 公钥必须被存放到远程机器上的 ~/.ssh/authorized_keys 才能够使系统正确运转，因此 `#cp id_rsa.pub authorized_keys`<br />
将公钥复制一份为 authorized_keys 此文件既可。将私钥 id_rsa 复制到 Windows 客户端。</p>
<h3>4. 转换 OpenSSH 生成的私钥 id_rsa 到 putty 能使用的文件</h3>
<p>这里需要借助一个工具 puttygen.exe ，从 <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">Putty 的网站</a>上可以下载。</p>
<p>执行 puttygen.exe 后，点击 Load 按钮，打开从远程服务器下载的 id_rsa ，这时要求输入生成密钥时输入的密钥密码。</p>
<p><a href="http://phpstone.com/wp-content/uploads/2009/05/puttygen1.png"><img class="alignnone size-full wp-image-404" title="puttygen1" src="http://phpstone.com/wp-content/uploads/2009/05/puttygen1.png" alt="puttygen" width="492" height="475" /></a></p>
<p>验证成功后显示密钥文件，然后点击 Save private key，就可以将文件另存为 &#8220;id_rsa.ppk&#8221; 这样的 putty 能够使用的私钥文件了。</p>
<h3>5. 配置和使用 Putty</h3>
<p>打开 Putty 程序，点击边栏的 SSH ，在 “Preferred SSH protocol version”下选择“2 only”，表示只使用 SSH 版本2；</p>
<p>点击 SSH 下的 Auth，然后点击 Browse（浏览），打开刚才的转换好的“id_rsa.ppk”私钥；</p>
<p><a href="http://phpstone.com/wp-content/uploads/2009/05/putty1.png"><img class="alignnone size-full wp-image-405" title="putty1" src="http://phpstone.com/wp-content/uploads/2009/05/putty1.png" alt="putty_image" width="465" height="448" /></a></p>
<p>最后回到 Session ，在 Host Name 下填入服务器的IP地址或者域名，Saved Sessions 下填入这个会话的会话名称（任意你喜欢的命名），然后点Save保存就设置好了。</p>
<p>接着双击会话名称就打开了登陆界面，与采用口令认证的提示不同，然后输入生成密码时设置的密钥密码既可登陆：</p>
<blockquote><p>Login as: test<br />
Authenticating with public key &#8220;imported-openssh-key&#8221;<br />
Passphrase for key &#8220;imported-openssh-key&#8221;:<br />
Last login: Wed May 31 12:35:00 2006 from 192.168.1.10<br />
test@server:~$</p></blockquote>
<p>Tips：你应该定期地同时更改公钥和私钥。</p>
<p>Ps：如果本身是FreeBSD，Linux或者Mac OS客户端，使用密钥更加简单，只需要将id_rsa放入~/.ssh文件夹并且确保.ssh文件夹的权限为700，id_rsa的权限为600即可正常登陆。</p>
<p>参考资料：<br />
<a href="http://linux-sxs.org/networking/openssh.putty.html">Using Putty on Windows to login Linux securely via OpenSSH</a><br />
<a href="http://cnsnap.cn.freebsd.org/doc/zh_CN.GB2312/books/handbook/openssh.html">FreeBSD Handbook:OpenSSH</a></p>
]]></content:encoded>
			<wfw:commentRss>http://phpstone.com/using-putty-on-windows-to-login-freebsd-securely-via-openssh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
