探索SSH:常见功能与使用技巧

文章目录

SSH(Secure Shell)是一种安全网络协议,用于通过加密的方式在网络上安全地进行远程登录和执行命令。它是管理远程服务器和网络设备的重要工具之一。在本文中,我们将探讨SSH的常见功能以及一些使用技巧。

远程登录

使用SSH可以远程登录到另一台计算机或服务器。以下是登录到远程服务器的基本命令:

bash 复制代码
ssh username@hostname

其中,username 是远程服务器的用户名,hostname 是远程服务器的主机名或IP地址。

密钥认证

SSH还支持使用密钥对进行身份验证,比密码更安全。生成SSH密钥对的命令如下:

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

然后将公钥(~/.ssh/id_rsa.pub)添加到远程服务器的authorized_keys文件中,以实现密钥认证。

文件传输

通过SCP(Secure Copy Protocol)或SFTP(SSH File Transfer Protocol)可以在本地计算机和远程服务器之间安全地传输文件。

使用SCP复制本地文件到远程服务器:

bash 复制代码
scp /path/to/local/file username@hostname:/path/to/remote/location

使用SCP从远程服务器复制文件到本地:

bash 复制代码
scp username@hostname:/path/to/remote/file /path/to/local/location

端口转发

SSH还可以用于端口转发,将本地端口与远程服务器上的端口关联起来,用于安全访问内部网络服务。

本地端口转发:

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

远程端口转发:

bash 复制代码
ssh -R remote_port:local_host:local_port username@hostname

执行远程命令

可以使用SSH执行远程服务器上的命令,而无需登录到远程服务器。

bash 复制代码
ssh username@hostname 'command'

例如,查看远程服务器上的系统信息:

bash 复制代码
ssh username@hostname 'uname -a'

会话保持

通过在SSH配置中设置ServerAliveIntervalServerAliveCountMax参数,可以保持SSH会话持续连接而不被断开。

编辑SSH配置文件:

bash 复制代码
nano ~/.ssh/config

添加以下行:

复制代码
Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

总结

SSH是一种强大而灵活的工具,用于远程管理服务器和网络设备。通过远程登录、密钥认证、文件传输、端口转发、执行远程命令和会话保持等功能,SSH为系统管理员和开发人员提供了便捷且安全的远程访问解决方案。熟练掌握SSH的使用技巧,将有助于提高工作效率和网络安全性。

相关推荐
开发者联盟league1 天前
安装pnpm
ssh
2601_961875241 天前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant
小小小花儿2 天前
SSH密钥配置(免密连接远程服务器)
服务器·ssh
m0_526119402 天前
ssh key生成,gitee配置ssh
运维·gitee·ssh
qq_338432372 天前
VSCode Remote-SSH 远程 Windows Server 卡死的排查与解决
windows·vscode·ssh
着迷不白2 天前
十、网络客户端工具curl, wget, ssh, scp, sftp, rsync
运维·网络·ssh
江畔柳前堤3 天前
github实战指南01-账号配置与 SSH 密钥
运维·人工智能·深度学习·ssh·github·pyqt·信号处理
相醉为友3 天前
Trae IDE WSL2/SSH 环境网络故障排查笔记
ide·笔记·ssh
c238564 天前
git常见错误和ssh验证推送
运维·git·ssh
zh路西法4 天前
【tmux入门】终端分屏、SSH远程守护与一键启动脚本
linux·运维·ssh·bash