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

相关推荐
Lois_Luo18 小时前
使用 DigitalPlat 免费搭配 Cloudflare Tunnel 实现飞牛系统、服务及 SSH 内网穿透教程
ssh·内网穿透
超龄超能程序猿1 天前
Bitvisse SSH Client 安装配置文档
运维·ssh·github
电脑能手2 天前
遇到该问题:kex_exchange_identification: read: Connection reset`的解决办法
linux·ubuntu·ssh
half-beast12 天前
ganymed-ssh2连接openssh 8.2
java·ssh·openssh·ganymed
qq_2148032912 天前
systemd[1]: Failed to start LSB: Bring up/down networking
ssh
家庭云计算专家13 天前
群晖如何开启及使用ssh:小白用户上手指南-家庭云计算专家
运维·云计算·ssh·onlyoffice
SH11HF13 天前
小菜狗的云计算之旅,shell脚本语言的基本内容和用法
运维·网络·云计算·ssh
KIKU啊13 天前
Docker+PyCharm+ssh部署深度学习docker环境
docker·pycharm·ssh
问~15 天前
gitea本地部署代码托管后仓库的新建与使用(配置好ssh密钥后仍然无法正常克隆仓库是什么原因)
运维·ssh·gitea
fangeqin15 天前
CentOS 8解决ssh连接github时sign_and_send_pubkey失败问题
centos·ssh·github