SSH 的一般用途

Posted

您通常使用 SSH 来允许用户登录到一个远程主机并执行命令。然而,SSH 还支持隧道和 X11 连接。它甚至可以使用 SFTP 或 SCP 传输文件。SSH 适用于大部分常见平台内的多个应用程序,这些平台包括 Linux、UNIX、Windows 和 Apple® OS X,虽然有些应用程序可能需要仅在特定 SSH 客户端或服务器上提供或与之兼容的功能。 下面是一些常见的 SSH 语法例子: 远程主机 shell 访问(取代 telnet 和 rlogin 明文,不安全协议): ```bash Terminal

ssh fsmythe@example.com

[fsmythe@example.com] ~


在远程主机(代替 rsh)执行单一命令:
```bash Terminal
# ssh root@example.com reboot 
root@example.com's password: ******

通过 SCP 命令将文件从本地服务器复制到远程主机。 ```bash Terminal root@edb-01.example.com's password: ****** file1.txt 100% 0 0.0KB/s 00:00 file2.txt 100% 0 0.0KB/s 00:00


结合 SFTP,作为 FTP 文件传输的一个安全替代品:
```bash Terminal
sftp fsmythe@example.com 
Connecting to example.com...
fsmythe@example.com's password: *******
sftp>

结合 rsync 有效安全地备份、复制和镜像文件到一个本地或远程主机: ```bash Terminal

rsync -avul --rsh=ssh /opt/edbdata/ root@example.com:/root/backup/

root@example.com's password: ****** building file list ... done ./ file1.txt file2.txt file3.txt file4.txt dir1/file5.txt dir2/file6.txt

sent 982813 bytes received 2116 bytes 1374860.38 bytes/sec total size is 982138 speedup is 1.00


端口转发或端口隧道化(不要与 VPN 混淆):
```bash Terminal
ssh -L 8000:mailserver:110 example.com    fsmythe@example.com's password: ********

从一个远程主机转发 X 会话(可能通过多个中间主机): ```bash Terminal Edit /etc/ssh/sshd_config and change 2 keywords : AllowTcpForwarding yes X11Forwarding yes

service sshd restart

$ export DISPLAY $ ssh -X fsmythe@example.com


X11 转发配置与带 SSH X11 隧道的一个 X Windows 客户端的结合,支持实现通过 SSH 安全地在同一 Windows 主机上运行的一个 UNIX 或 Linux GUI 子系统,该 Windows 主机是到 Linux 或 UNIX 远程主机的 SSH 会话的来源:
```bash Terminal
ssh -ND 8000 fsmythe@example.com
Browser Settings, goto 'Manual Proxy Configuration' set "SOCKS Host" to example.com,
the 'Port to 8000' , Enable SOCKS v5, and lastly set 'No Proxy for' field
to 'localhost, 127.0.0.1'

使用 sshfs 将一个目录作为本地计算机上的文件系统安全地挂载到一个远程服务器: ```bash Terminal

yum install sshfs fuse-utils (Install sshfs and fuse-utils)

$sshfs example.com:/remote_dir /mnt/local_dir


通过一个或多个机制对服务器进行自动化的远程主机监控和管理:
```bash Terminal
(Report number of apache processes running on the remote server example.com):
$ ssh example.com ps -ef | grep httpd | wc -l
root@example.com's password: ***** 

转载http://www.ibm.com/developerworks/cn/aix/library/au-sshsecurity/


此文章 短链接: http://dlj.bz/oVGrcw