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

## 给 Linux 安装 OpenSSH

```python
$ sudo apt-get install openssh-server
```

## Mac 或 Linux SSH 去 Linux

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

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

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

&#x20;**terminal 上输入这个获取 被操控 电脑的 ip 号码**.

```python
$ ifconfig
```

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

```python
$ sudo apt install net-tools
```

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

```
inet addr:192.168.0.114
```

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

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

如果报错:

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

## 省略密码直接登录

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

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

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

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

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

```python
Enter passphrase (empty for no passphrase):
```

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

```python
Enter same passphrase again:
```

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

```python
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:

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

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

```python
$ 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 登录被控制端的电脑:

```python
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.
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/linux/yuan-cheng-cao-kong/zen-yao-yang-cong-macoshuo-linux-tong-guo-ssh-yuan-cheng-linux.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
