Secure Shell (简体中文)

From ArchWiki
Revision as of 17:24, 7 March 2009 by Felonwan (Talk | contribs) (X11提升)

Jump to: navigation, search

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n links end

介绍

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 is typically used to log into a remote machine and execute commands, but it also supports tunneling, forwarding arbitrary TCP ports and X11 connections; it can transfer files using the associated SFTP or SCP protocols.

An SSH server, by default, listens on the standard TCP port 22. An ssh client program is typically used for establishing connections to an sshd daemon accepting remote connections. Both are commonly present on most modern operating systems, including Mac OS X, Linux, Solaris and OpenVMS. Proprietary, freeware and open source versions of various levels of complexity and completeness exist.

OpenSSH

OpenSSH (OpenBSD Secure Shell) is a set of computer programs providing encrypted communication sessions over a computer network using the ssh protocol. It was created as an open source alternative to the proprietary Secure Shell software suite offered by SSH Communications Security. OpenSSH is developed as part of the OpenBSD project, which is led by Theo de Raadt.

OpenSSH is occasionally confused with the similarly-named OpenSSL; however, the projects have different purposes and are developed by different teams, the similar name is drawn only from similar goals.

安装OpenSSH

# pacman -Sy openssh

设置SSH服务器

你要更改下面的配置文件:

$ su -c 'nano /etc/ssh/sshd_config'

你可能想要把默认的端口从22改成其它更高的端口(参考 security through obscurity)。

尽管ssh运行的端口可以被像nmap这样的端口扫描器侦测到,changing it will 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”。

允许其它主机进入

Template:Box Note

为了让其他人通过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通道

This is highly useful for laptop users connected to various unsafe wireless connections. The only thing you need is an SSH server running at a somewhat secure location, like your home or at work. It might be useful to use a dynamic DNS service like DynDNS so you don't have to remember your IP-address.

第一步:开始连接

You only have to execute this single command in your favorite terminal to start the connection:

$ ssh -ND 4711 user@host

where "user" is your username at the SSH server running at the "host". It will ask for your password, and then you're connected! The "N" flag disables the interactive prompt, and the "D" flag specifies the local port on wich to listen on (you can choose any port number if you want).

One way to make this easier is to put an alias line in your ~/.bashrc file as following:

alias sshtunnel="ssh -ND 4711 -v user@host"

It's nice to add the verbose "-v" flag, because then you can verify that it's actually connected from that output. Now you just have to execute the "sshtunnel" command :)

第二步:配置你的浏览器(或其它程序)

如果你不配置你的web浏览器以便使用新创建的socks通道的话,上面的一步完全没用!

  • 对于Firefox: Edit -> Preferences -> Advanced -> Network -> Connection -> Setting:
检查"Manual proxy configuration" radio button, 并且输入"localhost" 在"SOCKS host" 文本段, 然后在接下来的一个文本框中输入你的端口数(上面我们用的是4711)。
确定你选择使用SOCKS4。这个程序对不会对SOCKS5起作用.

享受你的安全通道吧!

X11 Forwarding

为了通过SSH运行图形程序你必须使用X11 Forwarding。一个选项就是需要设置服务器和客户端的配置文件。

在服务器上安装xorg-xauth:

# pacman -Sy xorg-xauth
  • 在服务器的“sshd_config”配置文件中,将“X11Forwarding”这个选项去掉注释。
  • 在服务器的“sshd_config”配置文件中,将“X11Forwarding”这个选项去年注释。

用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

链接和参考