什么是SSH
SSH(全名为Secure Shell Protocol)是一种加密网络通信协议,它使两台计算机能够在不安全的网络上安全地通信,SSH主要用于远程登录应用程序和命令行可执行程序(比如终端应用程序)。对于希望使用SSH连接到服务器或其他计算机的用户,客户端和远程主机都需要安装SSH并启用SSH服务。在下面的教程中,你将学习如何在Ubuntu 22.04 LTS Jammy Jellyfish Desktop or Server上安装和启用SSH,并使用命令行终端(iTerm2)连接到远程PC。
更新Ubuntu
在安装完新系统后,先执行更新操作确保在当前Ubuntu系统上安装和启用SSH不会发生冲突,使用sudo来执行更新操作
sudo apt update && sudo apt upgrade -y
安装SSH(OpenSSH)
最新的Ubuntu 22.04 LTS系统默认没有安装和启用SSH服务,因此首先在终端中运行以下命令,执行安装操作
sudo apt install openssh-server -y
安装完后,使用systemctl启动SSH服务
sudo systemctl enable --now ssh
然后检查SSH服务运行状态,确保服务正常运行并且没有错误输出(显示active running表示服务正常)
sudo systemctl status ssh
Last login: Mon May 9 14:10:36 2022 from 10.211.55.2
hellboycc@hellboycc-Parallels-ARM-Virtual-Machine:~$ sudo systemctl status ssh
[sudo] password for hellboycc:
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-05-11 20:56:47 CST; 1min 27s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 705 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 726 (sshd)
Tasks: 1 (limit: 2238)
Memory: 6.8M
CPU: 94ms
CGroup: /system.slice/ssh.service
└─726 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
5月 11 20:56:47 hellboycc-Parallels-ARM-Virtual-Machine systemd[1]: Starting OpenBSD Secure Shell server...
5月 11 20:56:47 hellboycc-Parallels-ARM-Virtual-Machine sshd[726]: Server listening on 0.0.0.0 port 22.
5月 11 20:56:47 hellboycc-Parallels-ARM-Virtual-Machine sshd[726]: Server listening on :: port 22.
5月 11 20:56:47 hellboycc-Parallels-ARM-Virtual-Machine systemd[1]: Started OpenBSD Secure Shell server.
5月 11 20:57:52 hellboycc-Parallels-ARM-Virtual-Machine sshd[2039]: Accepted password for hellboycc from 10.211.55.2 port 60804 ssh2
5月 11 20:57:52 hellboycc-Parallels-ARM-Virtual-Machine sshd[2039]: pam_unix(sshd:session): session opened for user hellboycc(uid=1000) by (uid=0)
另一个方便检查连接状态的命令如下
sudo ss -lt
hellboycc@hellboycc-Parallels-ARM-Virtual-Machine:~$ sudo ss -lt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 127.0.0.53%lo:domain 0.0.0.0:*
LISTEN 0 128 0.0.0.0:ssh 0.0.0.0:*
LISTEN 0 128 127.0.0.1:ipp 0.0.0.0:*
LISTEN 0 128 [::]:ssh [::]:*
LISTEN 0 128 [::1]:ipp [::]:*
检查防火墙状态
在进行远程连接前,检查系统防火墙的状态,当前我安装的Ubuntu 22.04 LTS Jammy Jellyfish Desktop防火墙处于未激活状态
hellboycc@hellboycc-Parallels-ARM-Virtual-Machine:~$ sudo ufw status
Status: inactive
因此,可以直接使用终端工具进行远程连接,首先需要有内部/外部IP地址或主机名,最好是所连接帐户的用户名,然后使用ssh命令进行连接
ssh username@ip-address/hostname
我使用的是iTerm2终端工具,整个连接过程如下
(base) Hellboycc :: ~ » ssh hellboycc@10.211.55.3
The authenticity of host '10.211.55.3 (10.211.55.3)' can't be established.
ED25519 key fingerprint is SHA256:N3YRUn8FZ+F6nnwnbDe8nTKHV9nulmiF1o+fRMFBS10.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes^[[C
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '10.211.55.3' (ED25519) to the list of known hosts.
hellboycc@10.211.55.3's password:
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-27-generic aarch64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
0 updates can be applied immediately.
Last login: Wed May 11 20:57:53 2022 from 10.211.55.2
hellboycc@hellboycc-Parallels-ARM-Virtual-Machine:~$
这里有一点需要注意,我们可以看到最后一次登录是什么时候,如果看到出现很多次失败登录失败尝试,这意味着可能有暴力攻击正在发生,这时候应该查看防火墙规则或者其他安全方面的设置
关闭SSH (OpenSSH)服务
对于不需要运行SSH的主机,出于安全考虑,应该禁用该服务,许多恶意程序会扫描互联网以寻找SSH服务,如果要禁用SSH,使用如下命令
sudo systemctl disable ssh --now
当需要再次开启SSH服务,使用如下命令
sudo systemctl enable ssh --now
如果想彻底删除SSH服务,使用如下命令
sudo apt autoremove openssh-server -y
简单总结
通过本安装教程的学习,你应该掌握了如何在Ubuntu 22.04 LTS上安装和启用SSH(OpenSSH)服务,总的来说,与另外一个不安全的其他类似服务(比如Telnet)相比,SSH是首选服务,不过建议在不安全的网络环境中使用这些连接服务时,都首先激活并检查防火墙。