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

相关推荐
zcg19422 天前
工作第一步建立连接——ssh
运维·服务器·ssh
_可乐无糖3 天前
用鼠标点击终端窗口的时候出现:0;61;50M0;61;50M0;62;50M0
linux·ssh
冰橙子id3 天前
linux-远程访问管理(sshd,scp,sftp)
linux·网络·ssh
电子小子洋酱5 天前
VScode SSH远程连接Ubuntu(通过SSH密钥对的方式)
vscode·物联网·ubuntu·ssh
TeleostNaCl5 天前
一次因 luci 无法登录而通过 SSH 升级 OpenWrt 固件的经验总结
运维·网络·经验分享·ssh·智能路由器
強云5 天前
使用 SSH 连接 GitHub
ssh·github
乌云暮年6 天前
Linux常用命令
linux·服务器·ssh·php
构建的乐趣6 天前
运行ssh -T git@github.com报错
git·ssh·github
言之。7 天前
借助ssh实现web服务的安全验证
运维·安全·ssh
muyun28008 天前
安全访问云端内部应用:用frp的stcp功能解决SSH转发的痛点
运维·安全·ssh·frp