SSH, Secure Shell

  • 查看已有公钥
1
2
cd ~/.ssh # 进入目录
cat id_rsa.pub # 显示公钥
1
cat ~/.ssh/id_rsa.pub # 显示公钥
  • 生成新的密钥
1
2
3
4
5
6
7
8
9
10
11
12
13
ssh-keygen -t rsa -b 4096 -f ~/.ssh/newsshkey

# 参数说明:
# -t rsa 指定加密方式为RSA
# -b 4096 密钥长度(bit)
# -f 指定密钥文件名, 默认为 id_rsa

# 各个参数都可以不填, 使用默认参数
ssh-keygen

# 建议加上这些参数
ssh-keygen -t rsa -C xxx@xxx.com
# 这里的 xxx@xxx.com 只是用于生成的 sshkey 的名称, 并不要求具体命名为某个邮箱
  • 将公钥上传至服务器

SSH配置密钥登录以及简单的安全设置

1
2
3
ssh-copy-id -i ~/.ssh/newsshkey.pub -p port User@HostName
# -i ~/.ssh/newsshkey.pub 指定密钥文件公钥, 注意后缀为.pub, 这是公钥文件, 不要错传私钥!!!
# username@ipaddress -p 22, 指定用户名@主机, -p 指定端口号

这个命令会将公钥中的内容 copy 至服务器上 ~/.ssh/authorized_keys 文件中

  • ssh config
1
2
3
4
5
6
7
Host [name] # 配置别名
User <user> # 服务器端对应的用户名
HostName <ip> # 域名或者IP地址
Port <port> # 服务器端口号
PubkeyAcceptedKeyTypes +ssh-rsa
HostKeyAlgorithms +ssh-rsa
IdentityFile ~/.ssh/id_rsa # 私钥文件路径
  • 使用
1
ssh [name]

在登陆时可能会收到如下报错信息:

1
2
3
4
5
6
7
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '~/.ssh/id_rsa.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "~/.ssh/id_rsa.pub": bad permissions

这个错误信息表明你的 ~/.ssh/id_rsa.pub 文件的权限设置过为公开, 这是不安全的. SSH密钥应该被设置为仅当前用户可以读写, 而其他任何用户都不能访问. 执行以下命令来修改权限, 确保你的SSH公钥的权限是安全的:

1
chmod 600 ~/.ssh/id_rsa.pub

这个命令会将.ssh/id_rsa.pub文件的权限设置为只有 “所有者” 有读权限.

连接远端仓库时指定私钥路径

1
GIT_SSH_COMMAND="ssh -i /path/to/id_rsa" git clone git@github.com:xxx

Linux Command

Linux 命令大全

Check System Info

1
2
3
4
5
6
7
8
9
10
11
12
13
(base) zwx@7049GP-TRT:/$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.6 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.6 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Check Shell Implementation

1
2
(base) zwx@7049GP-TRT:/$ echo $SHELL
/bin/bash

SSH with VS Code

Visual Studio Code Remote - SSH | YouTube
Remote-SSH | VS Code Docs

如何传输文件

打包压缩一个文件夹

1
2
3
4
5
# server1
tar -czf package.tar.gz DIR_NAME
# -c 表示 creates, 创建一个新的 tar 包
# -z 表示 gzip compression, 创建一个 gz 的包
# -f 指定 filename

解压缩一个 tar.gz 包

1
2
3
# server2
tar -xzf package.tar.gz
# -x, extracts files and directories from an existing archive

使用 -C 指定目录

1
tar -xvpf package.tar.gz -C /path/to/destination/

使用多线程, 需要安装 pigz

1
tar --use-compress-program=pigz -xvpf package.tar.gz -C /path/to/destination/

使用 rsync 命令 remote synchronize 同步一个文件

1
2
3
4
# synchronize file from local to remote
rsync /path/to/package.tar.gz User@HostName:/path/to/destination/
# synchronize file from remote to local
rsync User@HostName:/path/to/package.tar.gz /path/to/destination/

The source and destination cannot both be remote.

注意远端和本地的区分

通过 -r 参数来同步一个文件夹

1
rsync -r /path/to/srcdir User2@HostName2:/path/to/destination/