温馨提示×

ubuntu ssh怎么连接

小樊
38
2026-09-16 05:02:45
栏目: 智能运维

在 Ubuntu 上通过 SSH 连接远程服务器,一般分客户端连接服务端配置两部分。下面按常见使用场景说明:


一、作为客户端连接远程服务器(最常见)

1. 安装 SSH 客户端(一般已自带)

sudo apt update
sudo apt install openssh-client

2. 基本连接命令

ssh 用户名@服务器IP

示例:

ssh root@192.168.1.100

如果是非默认端口(如 2222):

ssh -p 2222 user@192.168.1.100

3. 第一次连接

会提示:

Are you sure you want to continue connecting (yes/no)?

输入 yes 回车,然后输入密码即可。


二、使用密钥登录(推荐)

1. 本地生成密钥

ssh-keygen

一路回车即可(默认生成在 ~/.ssh/id_rsa

2. 把公钥传到服务器

ssh-copy-id user@服务器IP

3. 再次登录

ssh user@服务器IP

无需输入密码。


三、Ubuntu 作为 SSH 服务端(让别人连你)

1. 安装 SSH 服务

sudo apt update
sudo apt install openssh-server

2. 启动并设置开机自启

sudo systemctl enable ssh
sudo systemctl start ssh

3. 查看状态

sudo systemctl status ssh

4. 查看本机 IP

ip a

四、常见问题

连接超时

  • 服务器是否开机
  • 防火墙是否放行 22 端口
sudo ufw allow 22

权限错误

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

修改 SSH 端口

编辑:

sudo nano /etc/ssh/sshd_config

修改:

Port 2222

重启:

sudo systemctl restart ssh

如果你是想:

  • Windows 连 Ubuntu
  • Ubuntu 连云服务器
  • 或用 VS Code / FinalShell / Xshell

可以告诉我具体场景,我给你更针对性的步骤。

0