通过 SSH 访问 VirtualBox Guest

首先在VBox Guest中安装SSH相关软件包:

[kodango@devops ~]$ sudo pacman -S openssh

启动SSH服务,并设置为开机自动启动:

[kodango@devops ~]$ sudo systemctrl start sshd.service
[kodango@devops ~]$ sudo systemctrl enable sshd.service

设置端口转发:
ArchLinux Port Forward

通过SSH连接到指定端口:

Host 'localhost' resolved to 127.0.0.1.
Connecting to 127.0.0.1:3022...
Connection established.
Escape character is '^@]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Oct 21 19:57:26 2012
[kodango@devops ~]$ ls
share

virtualboxservice可以在Windows下以服务的形式启动VBox Guest。

通过公钥的方式 SSH 到服务器

在此之前,将自己服务器的公钥拷贝上远程服务器上,加添到~/.ssh/authorized_keys文件中。可以用ssh-keygen -t rsa 或者 ssh-keygen -t dsa命令生成公钥和私钥。这一点不难,最关键的是要留意远程服务器上的文件和目录的权限问题。

Make sure the permissions on the ~/.ssh directory and its contents are proper. When I first set up my ssh key auth, I didn't have the ~/.ssh folder properly set up, and it yelled at me.

  1. Your home directory ~ and your ~/.ssh directory on the remote machine must be writable only by you: rwx------ and rwxr-xr-x are fine, but rwxrwx--- is no good, even if you are the only user in your group (if you prefer numeric modes: 700 or 755, not 775).
  2. Your private key file (on the local machine) must be readable and writable only by you: rw-------, i.e. 600.
  3. Your ~/.ssh/authorized_keys file (on the remote machine) must be readable (at least 400), but you'll need it to be also writable (600) if you will add any more keys to it.
  4. Also, if SELinux is set to enforcing, you may need to run restorecon -R -v ~/.ssh (see e.g. Ubuntu bug 965663 and Debian bug #658675; this is patched in CentOS 6).

参考:http://unix.stackexchange.com/questions/36540/why-am-i-still-getting-a-password-prompt-with-ssh-with-public-key-authentication

在 Linux 下管理 SSH 连接

工作过程中,往往需要连接到不同的服务器上,有些服务器因为处于特定的集群中,可以方便的通过跳板机跳转过去。而有一些开发机、测试机等零散的服务器就需要凭记忆来记住IP地址。肯定,这种方式非常麻烦,有些客户端可以用来管理 SSH 连接 ,例如Win下的xshell和putty等。

配置 SSH 连接

这里提供一种不需要安装客户端的简单方法,只利用SSH的配置文件(~/.ssh/config)来记录和管理多个 SSH 连接 。关于~/.ssh/config的配置很简单,随便找个手册了解一下就行。 例如,下面定义了连接到 test 这台机器所需的配置:

# Test host
Host test
    HostName 10.1.1.1
    User admin

一旦在配置文件中写好之后,可以简单地通过"ssh test"来连接到相应的服务器,非常简单。

参考Mac OS X 平台有哪些好用的 SSH 客户端? - 知乎,在上面的基础上再添加以下配置:

Host *
    ServerAliveInterval 60
    ControlMaster auto
    ControlPath ~/.ssh/%h-%p-%r
    ControlPersist yes

下面介绍两种方法来利用配置文件管理好 SSH 连接。

继续阅读