Files
nexus/raw/Home Office/Ubuntu 24.04 enable SSH.md

111 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 启动 SSH 服务
source:
author: shenwei
published:
created:
description:
tags: [ssh, ubuntu]
---
#ubuntu #ssh
在 Ubuntu 24.04 中开启 SSH 服务非常简单,但这个版本引入了一个重要的变化:**默认使用 `ssh.socket` 激活机制**(即只有在连接请求进入时才启动 SSH 守护进程),这与旧版本略有不同。
以下是开启 SSH 的标准步骤:
### 1. 安装 OpenSSH Server
首先,你需要确保系统中安装了 SSH 服务端。打开终端并输入:
Bash
```
sudo apt update
sudo apt install openssh-server -y
```
---
### 2. 启动并设置开机自启
在 Ubuntu 24.04 中,你可以使用以下命令来确保服务处于活动状态并随系统启动:
Bash
```
# 启动 SSH 服务
sudo systemctl start ssh
# 设置开机自启
sudo systemctl enable ssh
```
> **注意:** 如果你发现 `systemctl status ssh` 显示服务未运行别担心。24.04 默认使用 **Socket 激活** 模式。你可以通过 `sudo systemctl status ssh.socket` 检查监听状态。
---
### 3. 配置防火墙 (UFW)
如果你的 Ubuntu 开启了防火墙,你需要手动允许 SSH 流量(默认端口 22
Bash
```
sudo ufw allow ssh
# 或者
sudo ufw allow 22/tcp
```
---
### 4. 验证 SSH 状态
你可以通过以下命令检查 SSH 是否已经准备就绪:
Bash
```
sudo systemctl status ssh
```
如果看到 `active (running)` 或者 `ListenStream=22` (针对 socket 模式),说明已经成功开启。
---
### 5. 如何从远程连接
在另一台电脑上,使用你的**用户名**和**局域网 IP** 进行连接:
Bash
```
ssh username@192.168.x.x
```
_(如果你不知道本机 IP请使用前面提到的 `hostname -I` 命令查看)_
---
### 进阶建议24.04 的特殊说明
如果你习惯了旧版本的管理方式,或者需要修改**自定义端口**(例如改为 2222在 24.04 中你可能需要注意:
- **修改端口:** 现在推荐通过 `sudo systemctl edit ssh.socket` 来修改监听端口,而不仅仅是修改 `/etc/ssh/sshd_config`
- **切换回传统模式:** 如果你希望 SSH 像以前一样一直作为后台进程运行,可以执行:
Bash
```
sudo systemctl disable --now ssh.socket
sudo systemctl enable --now ssh.service
```
你想了解如何为 SSH 配置密钥登录以提高安全性吗?
---
[How to Enable SSH on Ubuntu 24.04](https://www.youtube.com/watch?v=F1_fkOYgu8g) 这个视频详细演示了在 Ubuntu 系统中从安装 OpenSSH 到通过防火墙配置的完整操作流程,非常适合新安装系统的用户参考。