上位机知识篇---SSH&SCP&密钥与密钥对

发布于:2025-02-13 ⋅ 阅读:(84) ⋅ 点赞:(0)


前言

以上就是今天要讲的内容,本文仅仅简单介绍了SCP、SSH、密钥、密钥对。


第一部分:SCP(Secure Copy Protocol)

功能

SCP(Secure Copy Protocol)是一种基于SSH协议的文件传输工具,用于在本地和远程主机之间安全地复制文件。它利用SSH的加密机制,确保数据传输的安全性。

使用方法

1.从本地复制到远程主机

scp /path/to/local/file username@remote_host:/path/to/remote/directory

/path/to/local/file:本地文件路径。
username@remote_host:远程主机的用户名和地址。
/path/to/remote/directory:远程主机上的目标目录。

2.从远程主机复制到本地

scp username@remote_host:/path/to/remote/file /path/to/local/directory

username@remote_host:/path/to/remote/file:远程主机上的文件路径。
/path/to/local/directory:本地目标目录。

3.复制整个目录

使用 -r 选项递归复制目录:

scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory

4.指定端口

如果SSH服务使用非默认端口(默认22),使用 -P 选项指定端口:

scp -P 2222 /path/to/local/file username@remote_host:/path/to/remote/directory

5.压缩传输

使用 -C 选项启用压缩,加快传输速度:

scp -C /path/to/local/file username@remote_host:/path/to/remote/directory

第二部分:SSH(Secure Shell)

功能

SSH(Secure Shell)是一种加密网络协议,用于安全地访问和管理远程主机。它提供加密的通信通道,支持远程登录、命令执行和文件传输。

使用方法

1.远程登录

ssh username@remote_host

username:远程主机的用户名。
remote_host:远程主机的地址。

2.指定端口

如果SSH服务使用非默认端口,使用 -p 选项指定端口

ssh -p 2222 username@remote_host

3.执行远程命令

可以在登录时直接执行命令:

ssh username@remote_host "command"

例如:

ssh username@remote_host "ls -l /path/to/directory"

4.使用密钥认证

使用SSH密钥对进行认证,避免每次输入密码

生成密钥对:

ssh-keygen -t rsa -b 4096

将公钥复制到远程主机:

ssh-copy-id username@remote_host

使用密钥登录:

ssh -i /path/to/private/key username@remote_host

5.SSH配置文件

使用 ~/.ssh/config 文件简化连接:
Host myserver
HostName remote_host
User username
Port 2222
IdentityFile /path/to/private/key
然后可以通过别名连接:

ssh myserver

6.端口转发

本地端口转发:

ssh -L local_port:remote_host:remote_port username@remote_host

远程端口转发:

ssh -R remote_port:local_host:local_port username@remote_host

scp\ssh总结

SCP:用于安全地复制文件,支持目录和压缩传输
SSH:用于安全地远程登录和执行命令,支持端口转发和密钥认证
两者都基于SSH协议,确保数据传输的安全性。

第三部分:密钥与密钥对

基本概念

1.密钥(Key)

密钥是用于加密和解密数据的字符串,分为公钥和私钥

公钥(Public Key):可以公开,用于加密数据或验证签名
私钥(Private Key):必须保密,用于解密数据或生成签名

2.密钥对(Key Pair)

密钥对由公钥和私钥组成,通常用于非对称加密
公钥和私钥在数学上相关,但无法从公钥推导出私钥。

3.生成密钥对

使用 ssh-keygen 生成密钥对:
ssh-keygen 是生成SSH密钥对的常用工具。

生成RSA密钥对
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

-t rsa:指定密钥类型为RSA
-b 4096:指定密钥长度为4096位
-C “your_email@example.com”:添加注释,通常为邮箱

生成ED25519密钥对
ssh-keygen -t ed25519 -C "your_email@example.com"

-t ed25519:指定密钥类型为ED25519。

4.密钥对存储

生成过程中会提示输入存储路径和文件名,默认路径为 **~/.ssh/id_rsa(RSA)**或 ~/.ssh/id_ed25519(ED25519)

生成的文件

id_rsa 或 id_ed25519:私钥文件
id_rsa.pub 或 id_ed25519.pub:公钥文件

设置密钥密码

生成过程中会提示设置密钥密码(passphrase),增加安全性。
每次使用密钥时需输入密码。

5.使用密钥对

将公钥添加到远程主机:

使用 ssh-copy-id 将公钥复制到远程主机~/.ssh/authorized_keys 文件中:

ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_host

手动复制公钥:
复制公钥内容:

cat ~/.ssh/id_rsa.pub

将内容粘贴到远程主机的 ~/.ssh/authorized_keys 文件中。

6.使用密钥登录远程主机

使用私钥进行SSH登录:

ssh -i ~/.ssh/id_rsa username@remote_host

如果私钥文件为默认路径(~/.ssh/id_rsa 或 ~/.ssh/id_ed25519),可省略 -i 选项:

ssh username@remote_host

7.SSH配置文件简化登录

编辑 ~/.ssh/config 文件,添加以下内容:

Host myserver
HostName remote_host
User username
IdentityFile ~/.ssh/id_rsa

然后使用别名登录:

ssh myserver

8.使用密钥进行Git操作

将**公钥添加到Git服务(如GitHub、GitLab)**的SSH密钥设置中。

9.使用SSH协议克隆仓库

git clone git@github.com:username/repository.git

10.密钥管理

备份密钥对

~/.ssh 目录下的私钥和公钥文件备份到安全位置。

更改密钥密码

使用 ssh-keygen 更改密钥密码:

ssh-keygen -p -f ~/.ssh/id_rsa
删除或替换密钥对:

删除旧的密钥文件生成新的密钥对
更新远程主机和Git服务中的公钥

密钥、密钥对总结

  1. 密钥对:由公钥和私钥组成,用于非对称加密
  2. 生成密钥对:使用 ssh-keygen 生成RSA或ED25519密钥对
  3. 使用密钥对:将公钥添加到远程主机,使用私钥进行SSH登录或Git操作。
  4. 密钥管理:备份、更改密码、删除或替换密钥对。
  5. 通过密钥对认证,可以提高安全性并简化远程登录和文件传输操作

总结

以上就是今天要讲的内容,本文仅仅简单介绍了SCP、SSH、密钥、密钥对。


网站公告

今日签到

点亮在社区的每一天
去签到