返回顶部
s

ssh-essentialsSSH基础命令

Essential SSH commands for secure remote access, key management, tunneling, and file transfers.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
9,686
下载量
免费
免费
15
收藏
概述
安装方式
版本历史

ssh-essentials

SSH 基础

安全外壳协议(SSH)用于远程访问和安全文件传输。

基本连接

连接

bash

使用用户名连接

ssh user@hostname

连接到指定端口

ssh user@hostname -p 2222

使用详细输出连接

ssh -v user@hostname

使用指定密钥连接

ssh -i ~/.ssh/id_rsa user@hostname

连接并执行命令

ssh user@hostname ls -la ssh user@hostname uptime && df -h

交互式使用

bash

使用代理转发连接

ssh -A user@hostname

使用X11转发连接(图形界面应用)

ssh -X user@hostname ssh -Y user@hostname # 受信任的X11

转义序列(会话期间)

~. - 断开连接

~^Z - 挂起SSH

~# - 列出转发的连接

~? - 帮助

SSH密钥

生成密钥

bash

生成RSA密钥

ssh-keygen -t rsa -b 4096 -C your_email@example.com

生成ED25519密钥(推荐)

ssh-keygen -t ed25519 -C your_email@example.com

使用自定义文件名生成

ssh-keygen -t ed25519 -f ~/.ssh/id_myserver

无密码生成(自动化)

ssh-keygen -t ed25519 -N -f ~/.ssh/id_deploy

管理密钥

bash

将公钥复制到服务器

ssh-copy-id user@hostname

复制指定密钥

ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname

手动复制密钥

cat ~/.ssh/idrsa.pub | ssh user@hostname cat >> ~/.ssh/authorizedkeys

检查密钥指纹

ssh-keygen -lf ~/.ssh/id_rsa.pub

更改密钥密码

ssh-keygen -p -f ~/.ssh/id_rsa

SSH代理

bash

启动ssh-agent

eval $(ssh-agent)

添加密钥到代理

ssh-add ~/.ssh/id_rsa

列出代理中的密钥

ssh-add -l

从代理中移除密钥

ssh-add -d ~/.ssh/id_rsa

移除所有密钥

ssh-add -D

设置密钥有效期(秒)

ssh-add -t 3600 ~/.ssh/id_rsa

端口转发与隧道

本地端口转发

bash

将本地端口转发到远程

ssh -L 8080:localhost:80 user@hostname

通过以下方式访问:http://localhost:8080

转发到不同的远程主机

ssh -L 8080:database.example.com:5432 user@jumphost

通过跳板机访问数据库

多个转发

ssh -L 8080:localhost:80 -L 3306:localhost:3306 user@hostname

远程端口转发

bash

将远程端口转发到本地

ssh -R 8080:localhost:3000 user@hostname

远程服务器可以通过其8080端口访问localhost:3000

使服务可从远程访问

ssh -R 9000:localhost:9000 user@publicserver

动态端口转发(SOCKS代理)

bash

创建SOCKS代理

ssh -D 1080 user@hostname

与浏览器或应用一起使用

配置SOCKS5代理:localhost:1080

使用Firefox

firefox --profile $(mktemp -d) \ --preferences network.proxy.type=1;network.proxy.socks=localhost;network.proxy.socks_port=1080

后台隧道

bash

在后台运行

ssh -f -N -L 8080:localhost:80 user@hostname

-f: 后台运行

-N: 不执行命令

-L: 本地转发

保持连接

ssh -o ServerAliveInterval=60 -L 8080:localhost:80 user@hostname

配置

SSH配置文件(~/.ssh/config)

简单主机别名

Host myserver HostName 192.168.1.100 User admin Port 2222

带密钥和选项

Host production HostName prod.example.com User deploy IdentityFile ~/.ssh/id_prod ForwardAgent yes

跳板机(堡垒机)

Host internal HostName 10.0.0.5 User admin ProxyJump bastion

Host bastion
HostName bastion.example.com
User admin

通配符配置

Host *.example.com User admin ForwardAgent yes

保持连接存活

Host * ServerAliveInterval 60 ServerAliveCountMax 3

使用配置

bash

使用别名连接

ssh myserver

自动通过堡垒机跳转

ssh internal

覆盖配置选项

ssh -o StrictHostKeyChecking=no myserver

文件传输

SCP(安全复制)

bash

复制文件到远程

scp file.txt user@hostname:/path/to/destination/

从远程复制文件

scp user@hostname:/path/to/file.txt ./local/

递归复制目录

scp -r /local/dir user@hostname:/remote/dir/

使用指定端口复制

scp -P 2222 file.txt user@hostname:/path/

使用压缩复制

scp -C large-file.zip user@hostname:/path/

保留属性(时间戳、权限)

scp -p file.txt user@hostname:/path/

SFTP(安全FTP)

bash

连接到SFTP服务器

sftp user@hostname

常用SFTP命令:

pwd - 远程工作目录

lpwd - 本地工作目录

ls - 列出远程文件

lls - 列出本地文件

cd - 更改远程目录

lcd - 更改本地目录

get file - 下载文件

put file - 上传文件

mget *.txt - 下载多个文件

mput *.jpg - 上传多个文件

mkdir dir - 创建远程目录

rmdir dir - 删除远程目录

rm file - 删除远程文件

exit/bye - 退出

批处理模式

sftp -b commands.txt user@hostname

通过SSH使用Rsync

bash

同步目录

rsync -avz /local/dir/ user@hostname:/remote/dir/

显示进度同步

rsync -avz --progress /local/dir/ user@hostname:/remote/dir/

同步并删除(镜像)

rsync -avz --delete /local/dir/ user@hostname:/remote/dir/

排除模式

rsync -avz --exclude *.log --exclude node_modules/ \ /local/dir/ user@hostname:/remote/dir/

自定义SSH端口

rsync -avz -e ssh -p 2222 /local/dir/ user@hostname:/remote/dir/

试运行

rsync -avz --dry-run /local/dir/ user@hostname:/remote/dir/

安全最佳实践

加固SSH

bash

禁用密码认证(编辑/etc/ssh/sshd_config)

PasswordAuthentication no PubkeyAuthentication yes

禁用root登录

PermitRootLogin no

更改默认端口

Port 2222

仅使用协议2

Protocol 2

限制用户

AllowUsers user1 user2

重启SSH服务

sudo systemctl restart sshd

连接安全

bash

检查主机密钥

ssh-keygen -F hostname

移除旧主机密钥

ssh-keygen -R hostname

严格主机密钥检查

ssh -o StrictHostKeyChecking=yes user@hostname

使用特定加密算法

ssh -c aes256-ctr user@hostname

故障排除

调试

bash

详细输出

ssh -v user@hostname ssh -vv user@hostname # 更详细 ssh -vvv user@hostname # 最详细

测试连接

ssh -T user@hostname

检查权限

ls -la ~/.ssh/

应为:~/.ssh

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 ssh-essentials-1776378799 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 ssh-essentials-1776378799 技能

通过命令行安装

skillhub install ssh-essentials-1776378799

下载

⬇ 下载 ssh-essentials v1.0.0(免费)

文件大小: 3.62 KB | 发布时间: 2026-4-17 14:41

v1.0.0 最新 2026-4-17 14:41
Initial release of SSH Essentials – a comprehensive reference for secure remote access, key management, tunneling, and file transfers using SSH.

- Covers basic SSH commands for connecting and running remote commands
- Includes SSH key generation, management, and agent usage
- Provides port forwarding, tunneling, and SOCKS proxy examples
- Features SSH config file usage, aliases, and jump hosts
- Details secure file transfers via SCP, SFTP, and Rsync over SSH
- Adds security best practices, troubleshooting, and advanced SSH techniques
- Offers concise command examples and practical tips throughout

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部