探索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的使用技巧,将有助于提高工作效率和网络安全性。

相关推荐
雾沉川2 天前
Netcatty 开源 SSH 客户端完整技术使用教程
运维·开源·ssh
网络小白不怕黑2 天前
13.SSH服务相关配置
服务器·网络·ssh
柠石榴3 天前
远程连接校内服务器 ssh
运维·服务器·ssh
daad7773 天前
记录ssh远程查看对端的摄像头显示
运维·ssh
流浪0014 天前
Git篇(三):吃透远程协作:SSH 密钥、分支、标签全流程实战指南
运维·git·ssh
handsome09165 天前
苹果电脑可以使用yunedit-ssh代替winscp
ssh·管理linux文件
行止AC7 天前
vscode-ssh免密链接
ssh·免密连接·vscode免密ssh连接
Asuicao8 天前
centos7.9版本升级ssh OpenSSH 9.8p1 源码 OpenSSL 1.1.1w 源码包以及安装升级回滚文档
运维·ssh
AlfredZhao8 天前
Win7老系统登录报错怎么解?PuTTY这次真能救急
ssh·putty·win7
单位圆的日常10 天前
FRP反向隧道实现异地SSH家用Linux
linux·运维·ssh·frp