在Ubuntu环境中使用SSH(Secure Shell)可以帮助你远程连接到其他计算机或服务器。以下是在Ubuntu中使用SSH的基本步骤:
Ubuntu系统通常已经预装了SSH客户端。如果没有安装,可以使用以下命令进行安装:
sudo apt update
sudo apt install openssh-client
如果你是第一次使用SSH,可能需要启动SSH服务。可以使用以下命令启动SSH服务:
sudo systemctl start ssh
并设置SSH服务开机自启:
sudo systemctl enable ssh
使用以下命令连接到远程服务器:
ssh username@remote_host
其中:
username 是你在远程服务器上的用户名。remote_host 是远程服务器的IP地址或域名。例如,连接到IP地址为192.168.1.100的服务器,用户名为user:
ssh user@192.168.1.100
首次连接到新的远程服务器时,系统会提示你确认服务器的指纹。输入yes并按回车键继续:
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
连接成功后,系统会提示你输入密码。输入密码并按回车键。注意,输入密码时不会显示任何字符。
为了提高安全性,可以使用SSH密钥进行身份验证。以下是生成和配置SSH密钥的步骤:
在本地机器上生成SSH密钥对:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
按照提示操作,可以选择密钥保存位置和设置密码。
使用以下命令将公钥复制到远程服务器:
ssh-copy-id username@remote_host
输入远程服务器的密码,公钥将被添加到远程服务器的~/.ssh/authorized_keys文件中。
之后,你可以直接使用SSH密钥连接远程服务器,无需输入密码:
ssh username@remote_host
完成工作后,可以使用以下命令断开SSH连接:
exit
或者直接关闭终端窗口。
通过以上步骤,你可以在Ubuntu环境中使用SSH进行远程连接和管理。