1
1
# 🚀 用于 GitHub Actions 的 SSH
2
2
3
- [ English] ( ./README.md ) | [ 繁體中文] ( ./README.zh-tw.md )
3
+ [ English] ( ./README.md ) | [ 繁體中文] ( ./README.zh-tw.md ) | 简体中文
4
4
5
- [ GitHub Action] ( https://github.com/features/actions ) 用于执行远程 SSH 命令 。
5
+ 一个用于执行远程 SSH 命令的 [ GitHub Action] ( https://github.com/features/actions ) 。
6
6
7
7
![ ssh workflow] ( ./images/ssh-workflow.png )
8
8
12
12
13
13
## 输入变量
14
14
15
- 更详细的信息,请参考 [ action.yml] ( ./action.yml ) 。
15
+ 有关更详细的信息,请参阅 [ action.yml] ( ./action.yml ) 。
16
16
17
17
| 输入参数 | 描述 | 默认值 |
18
18
| ------------------------- | ----------------------------------------------------- | ------ |
22
22
| username | SSH 用户名 | |
23
23
| password | SSH 密码 | |
24
24
| protocol | SSH 协议版本(tcp, tcp4, tcp6) | tcp |
25
- | sync | 如果有多个主机,启用同步执行 | false |
25
+ | sync | 如果指定了多个主机,则启用同步执行 | false |
26
26
| use_insecure_cipher | 使用不安全的密码算法 | false |
27
- | cipher | 允许的密码算法。如果未指定,则使用适当的算法 | |
27
+ | cipher | 允许的密码算法。如果未指定,则使用适当的默认值 | |
28
28
| timeout | SSH 连接到主机的超时时间 | 30s |
29
29
| command_timeout | SSH 命令的超时时间 | 10m |
30
30
| key | SSH 私钥的内容,例如 ~ /.ssh/id_rsa 的原始内容 | |
54
54
55
55
## 使用方法
56
56
57
- 执行远程 SSH 命令
57
+ 执行远程 SSH 命令。
58
58
59
59
``` yaml
60
60
name : remote ssh command
@@ -68,71 +68,88 @@ jobs:
68
68
69
69
with :
70
70
host : ${{ secrets.HOST }}
71
- username : ${{ secrets.USERNAME }}
71
+ username : linuxserver.io
72
72
password : ${{ secrets.PASSWORD }}
73
73
port : ${{ secrets.PORT }}
74
74
script : whoami
75
75
` ` `
76
76
77
- 画面输出
77
+ 输出:
78
78
79
79
` ` ` sh
80
80
======CMD======
81
81
whoami
82
82
======END======
83
- out : ***
83
+ linuxserver.io
84
84
===============================================
85
85
✅ Successfully executed commands to all hosts.
86
86
===============================================
87
87
```
88
88
89
89
### 设置 SSH 密钥
90
90
91
- 请在创建 SSH 密钥并使用 SSH 密钥时遵循以下步骤。最佳做法是在本地机器上创建 SSH 密钥而不是远程机器上。请使用 Github Secrets 中指定的用户名登录。生成 RSA 密钥:
91
+ 请按照以下步骤创建和使用 SSH 密钥。
92
+ 最佳做法是在本地机器上创建 SSH 密钥,而不是在远程机器上。
93
+ 使用 GitHub Secrets 中指定的用户名登录并生成 RSA 密钥对:
92
94
93
95
### 生成 RSA 密钥
94
96
95
97
``` bash
96
- ssh-keygen -t rsa -b 4096 -C
” [email protected] “
98
+ ssh-keygen -t rsa -b 4096 -C
" [email protected] "
97
99
```
98
100
99
101
### 生成 ed25519 密钥
100
102
101
103
``` bash
102
- ssh-keygen -t ed25519 -a 200 -C
” [email protected] “
104
+ ssh-keygen -t ed25519 -a 200 -C
" [email protected] "
103
105
```
104
106
105
107
将新生成的密钥添加到已授权的密钥中。详细了解已授权的密钥请点[ 此处] ( https://www.ssh.com/ssh/authorized_keys/ ) 。
106
108
107
109
### 将 RSA 密钥添加到已授权密钥中
108
110
109
111
``` bash
110
- cat .ssh/id_rsa.pub | ssh b@B ’ cat >> .ssh/authorized_keys‘
112
+ cat .ssh/id_rsa.pub | ssh b@B ' cat >> .ssh/authorized_keys'
111
113
```
112
114
113
115
### 将 ed25519 密钥添加到已授权密钥中
114
116
115
117
``` bash
116
- cat .ssh/id_ed25519.pub | ssh b@B ’ cat >> .ssh/authorized_keys‘
118
+ cat .ssh/id_ed25519.pub | ssh b@B ' cat >> .ssh/authorized_keys'
117
119
```
118
120
119
- 复制私钥内容,然后将其粘贴到 Github Secrets 中。
121
+ 复制私钥内容,然后将其粘贴到 GitHub Secrets 中。
120
122
121
- ### 复制 rsa 私钥内容
123
+ ### 复制 RSA 私钥内容
124
+
125
+ 在复制私钥之前,按照以下步骤安装 ` clip ` 命令:
122
126
123
127
``` bash
124
- clip < ~ /.ssh/id_rsa
128
+ # Ubuntu
129
+ sudo apt-get install xclip
130
+ ```
131
+
132
+ 复制私钥:
133
+
134
+ ``` bash
135
+ # macOS
136
+ pbcopy < ~ /.ssh/id_rsa
137
+ # Ubuntu
138
+ xclip < ~ /.ssh/id_rsa
125
139
```
126
140
127
141
### 复制 ed25519 私钥内容
128
142
129
143
``` bash
130
- clip < ~ /.ssh/id_ed25519
144
+ # macOS
145
+ pbcopy < ~ /.ssh/id_ed25519
146
+ # Ubuntu
147
+ xclip < ~ /.ssh/id_ed25519
131
148
```
132
149
133
150
有关无需密码登录 SSH 的详细信息,请[ 见该网站] ( http://www.linuxproblem.org/art_9.html ) 。
134
151
135
- ** 来自读者的注意事项 ** : 根据您的 SSH 版本,您可能还需要进行以下更改:
152
+ ** 注意 ** :根据您的 SSH 版本,您可能还需要进行以下更改:
136
153
137
154
- 将公钥放在 ` .ssh/authorized_keys2 ` 中
138
155
- 将 ` .ssh ` 的权限更改为 700
@@ -146,19 +163,19 @@ clip < ~/.ssh/id_ed25519
146
163
ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey]
147
164
```
148
165
149
- 请确保您所选择的密钥算法得到支持。在 Ubuntu 20.04 或更高版本上,您必须明确允许使用 SSH-RSA 算法。请在 OpenSSH 守护进程文件中添加以下行(它可以是 ` /etc/ssh/sshd_config ` 或 ` /etc/ssh/sshd_config.d/ ` 中的一个附加文件):
166
+ 请确保您所选择的密钥算法得到支持。在 Ubuntu 20.04 或更高版本上,您必须明确允许使用 ssh-rsa 算法。请在 OpenSSH 守护进程文件中添加以下行(它可以是 ` /etc/ssh/sshd_config ` 或 ` /etc/ssh/sshd_config.d/ ` 中的一个附加文件):
150
167
151
168
``` bash
152
169
CASignatureAlgorithms +ssh-rsa
153
170
```
154
171
155
- 或者,` Ed25519 ` 密钥在 OpenSSH 中默认被接受。如果需要,您可以使用它来替代 RSA。
172
+ 或者,` ed25519 ` 密钥在 OpenSSH 中默认被接受。如果需要,您可以使用它来替代 RSA:
156
173
157
174
``` bash
158
- ssh-keygen -t ed25519 -a 200 -C
” [email protected] “
175
+ ssh-keygen -t ed25519 -a 200 -C
" [email protected] "
159
176
```
160
177
161
- ### Example
178
+ ### 示例
162
179
163
180
#### 使用密码执行远程 SSH 命令
164
181
203
220
204
221

205
222
223
+ #### 从文件执行命令
224
+
225
+ ` ` ` yaml
226
+ - name : file commands
227
+
228
+ with :
229
+ host : ${{ secrets.HOST }}
230
+ username : ${{ secrets.USERNAME }}
231
+ key : ${{ secrets.KEY }}
232
+ port : ${{ secrets.PORT }}
233
+ script_path : scripts/script.sh
234
+ ` ` `
235
+
206
236
#### 多台主机
207
237
208
238
` ` ` diff
209
239
- name : multiple host
210
240
211
241
with :
212
- - host : ” foo.com“
213
- + host : ” foo.com,bar.com“
242
+ - host : " foo.com"
243
+ + host : " foo.com,bar.com"
214
244
username : ${{ secrets.USERNAME }}
215
245
key : ${{ secrets.KEY }}
216
246
port : ${{ secrets.PORT }}
219
249
ls -al
220
250
` ` `
221
251
222
- #### Commands from a file
223
-
224
- ` ` ` yaml
225
- - name : file commands
226
-
227
- with :
228
- host : ${{ secrets.HOST }}
229
- username : ${{ secrets.USERNAME }}
230
- key : ${{ secrets.KEY }}
231
- port : ${{ secrets.PORT }}
232
- script_path : scripts/script.sh
233
- ` ` `
252
+ 默认的 ` port` 值是 `22`。
234
253
235
254
# ### 多个不同端口的主机
236
255
237
256
` ` ` diff
238
257
- name: multiple host
239
258
240
259
with:
241
- - host : ” foo.com“
242
- + host : ” foo.com:1234,bar.com:5678“
260
+ - host: " foo.com"
261
+ + host: " foo.com:1234,bar.com:5678"
243
262
username: ${{ secrets.USERNAME }}
244
263
key: ${{ secrets.KEY }}
245
264
script: |
253
272
- name: multiple host
254
273
255
274
with:
256
- host : ” foo.com,bar.com“
275
+ host: " foo.com,bar.com"
257
276
+ sync: true
258
277
username: ${{ secrets.USERNAME }}
259
278
key: ${{ secrets.KEY }}
263
282
ls -al
264
283
` ` `
265
284
266
- #### 将环境变量传递到 Shell 脚本
285
+ # ### 将环境变量传递到 shell 脚本
267
286
268
287
` ` ` diff
269
288
- name: pass environment
270
289
271
290
+ env:
272
- + FOO : ” BAR“
273
- + BAR : ” FOO“
291
+ + FOO: " BAR"
292
+ + BAR: " FOO"
274
293
+ SHA: ${{ github.sha }}
275
294
with:
276
295
host: ${{ secrets.HOST }}
279
298
port: ${{ secrets.PORT }}
280
299
+ envs: FOO,BAR,SHA
281
300
script: |
282
- echo ” I am $FOO“
283
- echo ” I am $BAR“
284
- echo ” sha: $SHA“
301
+ echo " I am $FOO"
302
+ echo " I am $BAR"
303
+ echo " sha: $SHA"
285
304
` ` `
286
305
287
306
_在 `env` 对象中,您需要将每个环境变量作为字符串传递,传递 `Integer` 数据类型或任何其他类型可能会产生意外结果。_
288
307
289
308
# ### 如何使用 `ProxyCommand` 连接远程服务器?
290
309
291
310
` ` ` bash
292
- +——— + +-——— + +———— +
293
- | Laptop | <— > | Jumphost | <— > | FooServer |
294
- +——— + +-——— + +———— +
311
+ +-------- + +---------- + +----------- +
312
+ | Laptop | <-- > | Jumphost | <-- > | FooServer |
313
+ +-------- + +---------- + +----------- +
295
314
` ` `
296
315
297
316
在您的 `~/.ssh/config` 文件中,您会看到以下内容。
@@ -329,9 +348,9 @@ Host FooServer
329
348
ls -al
330
349
` ` `
331
350
332
- # ### 如何保护私钥?
351
+ # ### 保护私钥
333
352
334
- 密码短语通常用于加密私钥。这使得攻击者无法单独使用密钥文件 。文件泄露可能来自备份或停用的硬件,黑客通常可以从受攻击系统中泄露文件。因此,保护私钥非常重要 。
353
+ 密码短语通常用于加密私钥。这使得密钥文件本身对攻击者无用 。文件泄露可能来自备份或停用的硬件,黑客通常可以从受攻击系统中泄露文件。
335
354
336
355
` ` ` diff
337
356
- name: ssh key passphrase
@@ -351,10 +370,10 @@ Host FooServer
351
370
352
371
设置 SSH 主机指纹验证可以帮助防止中间人攻击。在设置之前,运行以下命令以获取 SSH 主机指纹。请记得将 `ed25519` 替换为您适当的密钥类型(`rsa`、 `dsa`等),而 `example.com` 则替换为您的主机。
353
372
354
- 现代 OpenSSH 版本中,需要提取的**默认密钥**类型是 `rsa`(从版本 5.1 开始)、`ecdsa`(从版本 6.0 开始)和 `ed25519`(从版本 6.7 开始)。
373
+ 在现代 OpenSSH 版本中,默认提取的密钥类型是 `rsa`(从版本 5.1 开始)、`ecdsa`(从版本 6.0 开始)和 `ed25519`(从版本 6.7 开始)。
355
374
356
375
` ` ` sh
357
- ssh example.com ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub | cut -d ’ ‘ -f2
376
+ ssh example.com ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub | cut -d ' ' -f2
358
377
` ` `
359
378
360
379
现在您可以调整您的配置:
@@ -379,4 +398,4 @@ ssh example.com ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub | cut -d ’
379
398
380
399
# # 授权方式
381
400
382
- 本项目中的脚本和文档采用 [MIT](LICENSE) 许可证 发布。
401
+ 本项目中的脚本和文档采用 [MIT 许可证 ](LICENSE) 发布。
0 commit comments