Featured image of post Ubuntu配置SSH服务

Ubuntu配置SSH服务

Ubuntu 配置SSH服务

1 、安装SSH服务器

更新软件索引+安装SSH服务器

1
sudo apt update && sudo apt install openssh-server

2 、检查SSH服务的状态

1
sudo systemctl status ssh

如果服务正在运行,你将看到“active (running)”的状态信息。

3 、查看SSH配置文件

1
cat /etc/ssh/sshd_config

4 、修改配置文件+保持连接+重启ssh服务,使配置生效+查看IP地址

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sudo sed -i.bak \
  -e '/Port 22/ a Port 22' \
  -e '/PermitRootLogin/ a PermitRootLogin yes' \
  -e '/GSSAPIAuthentication/ a GSSAPIAuthentication no' \
  -e '/UseDNS/ a UseDNS no' \
  -e '/ClientAliveInterval/ a ClientAliveInterval 10' \
  -e '/ClientAliveCountMax/ a ClientAliveCountMax 999' \
  /etc/ssh/sshd_config

systemctl restart ssh && hostname -I

解释如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
-i.bak备份为sshd_config.bak
#Port 22
#PermitRootLogin prohibit-password
#        GSSAPIAuthentication no
#UseDNS no
SSH保持连接
# ClientAliveInterval 0
# ClientAliveCountMax 3
修改为:
Port 22
PermitRootLogin yes
GSSAPIAuthentication no
UseDNS no
SSH保持连接
ClientAliveInterval 10
ClientAliveCountMax 999

5 、SSH服务开启、设置、检查

1
sudo systemctl start ssh  # 启动 SSH 服务
1
sudo systemctl enable ssh  # 设置 SSH 服务在开机时自动启动
1
sudo systemctl status ssh  # 检查 SSH 服务状态

没有报错说明SSH服务开启成功,按Ctrl+Z退出

6 、检查防火墙设置

使用以下命令允许22号端口通过Ubuntu防火墙:

1
sudo ufw allow ssh

7 、查看IP地址

1
hostname -I
本博客已稳定运行 小时 分钟
共发表 112 篇文章 · 总计 109.83 k 字
本站总访问量