怎么样从 MacOS 或 Linux 通过 SSH 远程 Linux

给 Linux 安装 OpenSSH

$ sudo apt-get install openssh-server

Mac 或 Linux SSH 去 Linux

打开 Mac 上的 Terminal. 然后输入:

$ ssh [要控制的用户名]@[它的IP地址]

确保操控和被操控的两台电脑连接上了同一个路由器. 然后在被操控电脑的

terminal 上输入这个获取 被操控 电脑的 ip 号码.

$ ifconfig

如果提示没有安装ficonfig输入下面指令就能安装.

$ sudo apt install net-tools

输入ifconfig, 接着找到以 inet 开头的字样, 就是在这个路由下的 ip 地址

inet addr:192.168.0.114

操控电脑的 terminal 中输入 ssh 开头的命令:

$ ssh morvan@192.168.0.114
morvan@192.168.0.114's password:

如果报错:

$ ssh-keygen -R 要 ssh 去的 ip 比如下面这样
$ ssh-keygen -R 192.168.0.114

省略密码直接登录

在 Mac 或者 Linux (控制电脑) 上生成一个public/private/ keypair(共锁和私锁),

然后将共锁(public key) 复制到要被远程的 Linux 上.

在 mac 的 Terminal 上输入指令ssh-keygen创建共锁和私锁, 用它默认的地方比较好. 回车确定.

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/MorvanZhou/.ssh/id_rsa):

确定后, 会确定是否想要一个保障密码,直接回车.

Enter passphrase (empty for no passphrase):

然后会要求再次确认, 回车

Enter same passphrase again:

最后, 会显示锁都已经生成好

Your identification has been saved in /Users/MorvanZhou/.ssh/id_rsa.
Your public key has been saved in /Users/MorvanZhou/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:yVr3PAPmxVO1lBd7KvqBsBCZSE8mdYce8mjBiUfRDVE MorvanZhou@Morvan
The key's randomart image is:
+---[RSA 2048]----+
|    o=*++*E    o+|
|   ..**++..   .o+|
|    ..=* .    .oo|
|      ooo. . . ..|
|     .. S + = .  |
|       + * B o   |
|      . . + *    |
|           . +   |
|            .    |
+----[SHA256]-----+

将这个生成好的 “公锁” 复制去被控制的 Linux:

$ ssh-copy-id [被控制的用户名]@[它的ip]

回车后会要求输入一次被控制端电脑的密码:

$ ssh-copy-id morvan@192.168.0.114

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/MorvanZhou/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
morvan@192.168.0.114's password:

密码正确后, 它将输出, 并告诉你怎么用 ssh 登录被控制端的电脑:

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh 'morvan@192.168.0.114'"
and check to make sure that only the key(s) you wanted were added.

Last updated