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

相关推荐
埃博拉酱5 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
zhangfeng11336 天前
趋动云 如何ssh登录 服务区 项目server
运维·人工智能·ssh
txzz88886 天前
CentOS-Stream-10 Secure Shell服务器
linux·centos·ssh·secure shell·ssh服务器
文静小土豆7 天前
CentOS 7 OpenSSH 10.2p1 升级全攻略(含离线安装与回退方案)
linux·运维·centos·ssh
最最菜的菜鸟7 天前
本地环境通过 SSH 协议访问 GitHub 上的仓库时,SSH 公钥认证失败,配置 Git SSH 密钥
git·ssh·github
ol木子李lo9 天前
Linux 命令备忘录
linux·运维·服务器·windows·编辑器·ssh·bash
m0_738120729 天前
渗透测试——Momentum靶机渗透提取详细教程(XSS漏洞解密Cookie,SS获取信息,Redis服务利用)
前端·redis·安全·web安全·ssh·php·xss
小哈里11 天前
【工具】Linux远程开发核心工具,Git命令缩写与SSH常用命令
linux·git·ssh·工具·远程开发
推理幻觉11 天前
ssh远程连接错误
运维·ssh
能源革命11 天前
Ubuntu24.04修改ssh默认端口
linux·服务器·ssh