Secure Shell (简体中文)

From ArchWiki
Revision as of 14:39, 7 March 2009 by Felonwan (Talk | contribs) (连接到服务器)

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

Tips and Tricks

Encrypted Socks Tunnel

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 :)

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

The above step is completely useless if you don't configure your web browser (or other programs) to use this newly created socks tunnel.

  • For Firefox: Edit -> Preferences -> Advanced -> Network -> Connection -> Setting:
Check the "Manual proxy configuration" radio button, and enter "localhost" in the "SOCKS host" text field, and then enter your port number in the next text field (I used 4711 above).
Make sure you select SOCKS4 as the protocol to use. This procedure will not work for SOCKS5.

Enjoy your secure tunnel!

X11 Forwarding

To run graphical programs through a SSH connection you can enable X11 forwarding. An option needs to be set in the configuration files on the server and client.

Install xorg-xauth on the server:

# pacman -Sy xorg-xauth
  • Enable the X11Forwarding option in sshd_config on the server.
  • Enable the ForwardX11 option in ssh_config on the client.

用SSHFS挂载远程文件系统

Install sshfs

# pacman -Sy sshfs

Add the user that we want to give the permission to mount SSH folders to the fuse group

# gpasswd -a USER fuse

Load the fuse module (in /etc/rc.conf for example)

And then, after logging in, we can try to mount a remote folder using sshfs:

# mkdir ~/remote_folder
# sshfs USER@remote_server:/tmp ~/remote_folder

The command above will cause the folder /tmp on the remote server to be mounted as ~/remote_folder on the local machine. Copying any file to this folder will result in transparent copying over the network using SCP. Same concerns direct file editing, creating or removing.

When we’re done working with the remote filesystem, we can unmount the remote folder by issuing:

# fusermount -u ~/remote_folder

If we work on this folder on a daily basis, it is wise to add it to the /etc/fstab table. This way is can be automatically mounted upon system boot or mounted manually (if noauto option is chosen) without the need to specify the remote location each time. Here is a sample entry in the table:

sshfs#USER@remote_server:/tmp /full/path/to/directory fuse    defaults,auto    0 0

链接和参考