# AlmaLinux 9修改SSH端口

修改AlmaLinux 9的端口一共涉及3个方面：

1.修改/etc/ssh/sshd\_config（这个和之前的是一样的）

2.修改SELinux的配置

3.修改防火墙的设置

第一步：

<pre><code><strong>nano /etc/ssh/sshd_config
</strong></code></pre>

找到#Port 22，去掉前面的#并修改后面的数字为想设置的端口即可。

第二个是修改SELinux的配置，具体命令是：

```
semanage port -l | grep ssh
```

但是我打这个命令遇到了报错：

提示：-bash: semanage: command not found，意思是没有semanage命令，怎么解决呢？很简单：

下面两条命令，第一条查找semanage命令的包，第二条安装policycoreutils-python-utils

```
yum provides /usr/sbin/semanage
```

```
yum -y install policycoreutils-python-utils
```

然后再回到前面的：

```
semanage port -l | grep ssh
```

你会发现，此时就正常了，会反馈当前SELinux允许的SSH端口：

然后我们添加：

```
semanage port -a -t ssh_port_t -p tcp XXXX
```

XXXX为你需要添加的端口。

然后又用前面的命令查看，主要是确认是否添加成功了：

```
semanage port -l | grep ssh
```

不出意外应该是好了，那么SELinux的修改也就完成了，下面是防火墙。

首先添加端口到防火墙：

```
firewall-cmd --permanent --zone=public --add-port=XXXX/tcp
```

XXXX为你需要添加的端口。

此时如果遇到报错：FirewallD is not running，说明防火墙没运行，需要手动来：

```
systemctl start firewalld.service
```

然后重新输入上面的那个命令，应该就不会报错而输出success，添加完接下来重启防火墙：

```
firewall-cmd --reload
```

最后是确认添加是否成功：

```
firewall-cmd --zone=public --query-port=XXXX/tcp
```

添加成功会返回yes，否则返回no，不出意外到这里也没什么问题了。

至此，需要修改的三个部分应该是都改完了，最后重启一下SSH，然后开个新的窗口连接过去试一下吧。

重启SSH：

```
systemctl restart sshd.service
```

摘自：Jevin's Blog，[原文链接](https://www.ijevin.com/centos7-change-ssh-port/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.linh.eu.org/jian-zhan-ji-shu/almalinux-9-xiu-gai-ssh-duan-kou.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
