SSH 简体中文
From ArchWiki
i18n |
---|
Русский |
English |
简体中文 |
Contents |
介绍
Secure Shell或者SSH是一个允许两台电脑之间通过安全的连接进行数据交换的网络协议。加密技术保证了数据的保密性和完整性。如果有必要的话,SSH采用公匙加密技术来验证远程主机和允许远程主机验证用户?(SSH uses public-key cryptography to authenticate the remote computer and allow the remote computer to authenticate the user, if necessary.)
SSH
SSH通常用于远程访问和执行命令,但是它也支持tunneling,forwarding arbitrary TCP ports and X11 connections; 它还能够用SFTP或SCP协议来传递文件。
一个SSH服务器,默认地,在TCP端口22进行监听。一个SSH客户端程序通常被用来建立一个接受远程连接的sshd守护进程。这两者都以专利的,自由软件的以及开源版本的形式和不同的复杂性和完整性,被广泛地应用在现代的操作系统中,包括Mac OS X,Linux,Solaris和OpenVMS等。
OpenSSH
OpenSSH (OpenBSD Secure Shell) 是一套用ssh协议,通过电脑网络,提供加密通讯会话的电脑程序。它相对于SSH Communications Security公司拥有专利的Secure Shell软件套装,被创建为开源。OpenSSH是由Theo de Raadt领导的OpenBSD项目的一部分。
人们常把OpenSSH与相似的OpenSSL搞混,但是,这两个项目是由不同的团队开发出来的,有不同的用途,相似的名字只是由于相似的目标。
安装OpenSSH
# pacman -Sy openssh
设置SSH服务器
你要更改下面的配置文件:
$ su -c 'nano /etc/ssh/sshd_config'
你可能想要把默认的端口从22改成其它更高的端口(参考 security through obscurity)。
尽管ssh运行的端口可以被像nmap这样的端口扫描器侦测到,改变它可以减少由于自动验证的尝试造成登录次数?( reduce the number of log entries caused by automated authentication attempts.)
调整设置
配置文件是“/etc/ssh/ssh_config”,通常是像这样的:
# $OpenBSD: sshd_config,v 1.75 2007/03/19 01:01:29 djm Exp $ # This is the ssh client system-wide configuration file. See # ssh_config(5) for more information. This file provides defaults for # users, and the values can be changed in per-user configuration files # or on the command line. # Configuration data is parsed as follows: # 1. command line options # 2. user-specific file # 3. system-wide file # Any configuration value is only changed the first time it is set. # Thus, host-specific definitions should be at the beginning of the # configuration file, and defaults at the end. # Site-wide defaults for various options # Host * # ForwardAgent no # ForwardX11 no # RhostsRSAAuthentication no # RSAAuthentication yes # PasswordAuthentication yes # HostbasedAuthentication no # BatchMode no # CheckHostIP yes # AddressFamily any # ConnectTimeout 0 # StrictHostKeyChecking ask # IdentityFile ~/.ssh/identity # IdentityFile ~/.ssh/id_rsa # IdentityFile ~/.ssh/id_dsa # Port 22 # Protocol 2,1 # Cipher 3des # Ciphers aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc # EscapeChar ~
推荐改变“Protocol”行成这样:
Protocol 2
这表示只有协议2将被用到,因为协议1 被认为不太安全。
当然这里也有一个文件是关于SSH的守护进程的,它就是“etc/ssh/sshd_config”,一般是这样的:
# $OpenBSD: sshd_config,v 1.75 2007/03/19 01:01:29 djm Exp $ # This is the sshd server system-wide configuration file. See # sshd_config(5) for more information. # This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin # The strategy used for options in the default sshd_config shipped with # OpenSSH is to specify options with their default value where # possible, but leave them commented. Uncommented options change a # default value. #Port 22 #Protocol 2,1 ListenAddress 0.0.0.0 #ListenAddress :: # HostKey for protocol version 1 #HostKey /etc/ssh/ssh''host''key # HostKeys for protocol version 2 #HostKey /etc/ssh/ssh''host''rsa_key #HostKey /etc/ssh/ssh''host''dsa_key # Lifetime and size of ephemeral version 1 server key #KeyRegenerationInterval 1h #ServerKeyBits 768 # Logging #obsoletes ~QuietMode and ~FascistLogging #SyslogFacility AUTH #LogLevel INFO # Authentication: #LoginGraceTime 2m #PermitRootLogin yes #StrictModes yes #MaxAuthTries 6 #RSAAuthentication yes #PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys # For this to work you will also need host keys in /etc/ssh/ssh''known''hosts #RhostsRSAAuthentication no # similar for protocol version 2 #HostbasedAuthentication no # Change to yes if you don't trust ~/.ssh/known_hosts for # RhostsRSAAuthentication and HostbasedAuthentication #IgnoreUserKnownHosts no # Don't read the user's ~/.rhosts and ~/.shosts files #IgnoreRhosts yes # To disable tunneled clear text passwords, change to no here! #PasswordAuthentication yes #PermitEmptyPasswords no # Change to no to disable s/key passwords #ChallengeResponseAuthentication yes # Kerberos options #KerberosAuthentication no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #KerberosGetAFSToken no # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the ~ChallengeResponseAuthentication mechanism. # Depending on your PAM configuration, this may bypass the setting of # PasswordAuthentication, ~PermitEmptyPasswords, and # "PermitRootLogin without-password". If you just want the PAM account and # session checks to run without PAM authentication, then enable this but set # ChallengeResponseAuthentication=no #UsePAM no #AllowTcpForwarding yes #GatewayPorts no #X11Forwarding no #X11DisplayOffset 10 #X11UseLocalhost yes #PrintMotd yes #PrintLastLog yes #TCPKeepAlive yes #UseLogin no #UsePrivilegeSeparation yes #PermitUserEnvironment no #Compression yes #ClientAliveInterval 0 #ClientAliveCountMax 3 #UseDNS yes #PidFile /var/run/sshd.pid #MaxStartups 10 # no default banner path #Banner /some/path # override default of no subsystems Subsystem sftp /usr/lib/ssh/sftp-server
只允许某些用户的话,加入这一行:
AllowUsers user1 user2
也许你想改变一些行,使它们像下面这样:
Protocol 2 . . . LoginGraceTime 120 . . . PermitRootLogin no # (put yes here if you want root login)
为了给人一个好的欢迎信息,你也可以去掉BANNER选项的注释和编辑“/etc/issue”。
允许其它主机进入
为了让其他人通过ssh登录到你的机器上,你必须要设置“/etc/hosts.allow”这个文件,加入类似下面的文字:
# 让每个人都能连接到你 sshd: ALL # 或者限制只能特定的ip能够 sshd: 192.168.0.1 # 或者限制ip段 sshd: 10.0.0.0/255.255.255.0 # 或者限制为IP匹配 sshd: 192.168.1.
现在检查你的“/etc/hosts.deny”文件,内容类似下面一行:
ALL: ALL: DENY
你能SSH出去别人也应该能够SSH进来:)。
开始使用新的配置之前,要先重启守护进程:
$ su -c '/etc/rc.d/sshd restart'
管理SSHD守护进程
只要把sshd加到/etc/rc.conf中的"DAEMONS" section部分:
DAEMONS=(... ... sshd ... ...)
开始/重启/停止这个daemon:
# /etc/rc.d/sshd {start|stop|restart}
连接到服务器
运行下面的命令:
$ ssh -p port user@server-address
小细节和小戏法
加密Socks通道
这个对于连接到各种不安全的无线网络上的笔记本电脑用户来说是高度有用的!唯一所需要的就是一个一定程度上处于安全的地点的 SSH服务器,比如在家里或办公室。用动态的DNS服务DynDNS也可能是很有用的,这样你就不必记住你的IP了。
第一步:开始连接
你只要在你喜欢的终端中执行这一个命令就能开始你的连接:
$ ssh -ND 4711 user@host
这里的“user”是你在“host”这台SSH服务器上运行的用户名。它会让你输入密码,然后你就能连上了。“N”表示不采用交互提示,而“D”表示指定监听的本地端口(你可以使用任何你喜欢的数字)
一个办法可以让这个过程更简单,那就是在~/.bashrc中加入这样一行:
alias sshtunnel="ssh -ND 4711 -v user@host"
加入冗长的“-v”标志更好,因为这样你可以验证是真的是从那个端口连接的。现在你只要执行命令“sshtunnel”就可以。 :)
第二步:配置你的浏览器(或其它程序)
如果你不配置你的web浏览器以便使用新创建的socks通道的话,上面的一步完全没用!
- 对于Firefox: Edit -> Preferences -> Advanced -> Network -> Connection -> Setting:
- 检查"Manual proxy configuration" radio button?, 并且在"SOCKS host" 文本段输入"localhost" , 然后在接下来的一个文本框中输入你的端口数(上面我们用的是4711)。
- 确定你选择使用SOCKS4。这个程序对不会对SOCKS5起作用.
- 享受你的安全通道吧!
X11 Forwarding
为了通过SSH运行图形程序你必须使用X11 Forwarding。一个选项就是需要设置服务器和客户端的配置文件。
1.在服务器上安装xorg-xauth:
# pacman -Sy xorg-xauth
2.在服务器端修改 /etc/ssh/sshd_config 配置文件,将“X11Forwarding”这个选项去掉注释。
3.在客户端修改 /etc/ssh/ssh_config:
ForwardAgent yes ForwardX11 yes ForwardX11Trusted yes
4.登录
ssh -X user@hostname
用SSHFS挂载远程文件系统
安装sshfs
# pacman -Sy sshfs
将你想要允许挂载SSH文件夹的用户添加到fuse组里
# gpasswd -a USER fuse
加载fuse模块(比如说在/etc/rc.conf中)
And then然后,在登录后,你就可以试着挂用sshfs载远程文件夹了:
# mkdir ~/remote_folder # sshfs USER@remote_server:/tmp ~/remote_folder
上面的命令将把远程服务器上的/tmp文件夹挂载到本地的~/remote_folder目录下。复制任何文件到这个目录将使文件通过SCP通过网络传输。 Same concerns direct file editing, creating or removing.
当我们完成在远程文件夹下的工作,我们可以这样来卸载它:
# fusermount -u ~/remote_folder
如果我们需要经常在这个文件夹下,让它通过/etc/fstab挂载是一个明智的选择。这个办法可以让它在启动的时候挂载或者通过手动挂载(如果是noauto选项的话),而不需要每次都去挂载它。下面是一个简单的样本:
sshfs#USER@remote_server:/tmp /full/path/to/directory fuse defaults,auto 0 0