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


文章目录


前言

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


第一部分:SCP(Secure Copy Protocol)

功能

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

使用方法

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

bash 复制代码
scp /path/to/local/file username@remote_host:/path/to/remote/directory

/path/to/local/file:本地文件路径。

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

/path/to/remote/directory:远程主机上的目标目录。

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

bash 复制代码
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 选项递归复制目录:

bash 复制代码
scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory

4.指定端口

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

bash 复制代码
scp -P 2222 /path/to/local/file username@remote_host:/path/to/remote/directory

5.压缩传输

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

bash 复制代码
scp -C /path/to/local/file username@remote_host:/path/to/remote/directory

第二部分:SSH(Secure Shell)

功能

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

使用方法

1.远程登录

bash 复制代码
ssh username@remote_host

username:远程主机的用户名。

remote_host:远程主机的地址。

2.指定端口

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

bash 复制代码
ssh -p 2222 username@remote_host

3.执行远程命令

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

bash 复制代码
ssh username@remote_host "command"

例如:

bash 复制代码
ssh username@remote_host "ls -l /path/to/directory"

4.使用密钥认证

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

生成密钥对:

bash 复制代码
ssh-keygen -t rsa -b 4096

将公钥复制到远程主机:

bash 复制代码
ssh-copy-id username@remote_host

使用密钥登录:

bash 复制代码
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

然后可以通过别名连接:

bash 复制代码
ssh myserver

6.端口转发

本地端口转发:

bash 复制代码
ssh -L local_port:remote_host:remote_port username@remote_host

远程端口转发:

bash 复制代码
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密钥对
bash 复制代码
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

-t rsa:指定密钥类型为RSA

-b 4096:指定密钥长度为4096位

-C "your_email@example.com":添加注释,通常为邮箱

生成ED25519密钥对
bash 复制代码
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 文件中:

bash 复制代码
ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_host

手动复制公钥:

复制公钥内容:

bash 复制代码
cat ~/.ssh/id_rsa.pub

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

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

使用私钥进行SSH登录:

bash 复制代码
ssh -i ~/.ssh/id_rsa username@remote_host

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

bash 复制代码
ssh username@remote_host

7.SSH配置文件简化登录

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

Host myserver

HostName remote_host

User username

IdentityFile ~/.ssh/id_rsa

然后使用别名登录:

bash 复制代码
ssh myserver

8.使用密钥进行Git操作

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

9.使用SSH协议克隆仓库

bash 复制代码
git clone git@github.com:username/repository.git

10.密钥管理

备份密钥对

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

更改密钥密码

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

bash 复制代码
ssh-keygen -p -f ~/.ssh/id_rsa
删除或替换密钥对:

删除旧的密钥文件生成新的密钥对

更新远程主机和Git服务中的公钥

密钥、密钥对总结

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

总结

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

相关推荐
鸡鸭扣1 小时前
Docker:3、在VSCode上安装并运行python程序或JavaScript程序
运维·vscode·python·docker·容器·js
paterWang1 小时前
基于 Python 和 OpenCV 的酒店客房入侵检测系统设计与实现
开发语言·python·opencv
A ?Charis1 小时前
k8s-对接NFS存储
linux·服务器·kubernetes
东方佑1 小时前
使用Python和OpenCV实现图像像素压缩与解压
开发语言·python·opencv
神秘_博士2 小时前
自制AirTag,支持安卓/鸿蒙/PC/Home Assistant,无需拥有iPhone
arm开发·python·物联网·flutter·docker·gitee
Moutai码农3 小时前
机器学习-生命周期
人工智能·python·机器学习·数据挖掘
人工干智能4 小时前
科普:“Docker Desktop”和“Docker”以及“WSL”
运维·docker·容器
落笔画忧愁e4 小时前
FastGPT及大模型API(Docker)私有化部署指南
运维·docker·容器
小白教程4 小时前
python学习笔记,python处理 Excel、Word、PPT 以及邮件自动化办公
python·python学习·python安装
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化