Ubuntun安装SSH服务

今天用虚拟机安装了Ubuntun操作系统,安装完成后,想用主机远程到操作系统居然连接不上,查看了服务居然没有,那就动手搞一下,将SSH安装上;
本文中直接使用的是 root 用户进行操作,非 root 用户有些操作需要使用 sudo 命令进行增强;
或者也可以直接使用 sudo -i 切换到 root 用户进行操作;

1.查看操作系统版本:

cat /etc/os-release

在这里插入图片描述

2.更新一下仓库

root@tony-pc:~# sudo apt update
命中:3 http://security.ubuntu.com/ubuntu jammy-security InRelease
命中:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
命中:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates InRelease
命中:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports InRelease
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成
有 224 个软件包可以升级。请执行 ‘apt list --upgradable’ 来查看它们。
root@tony-pc:~#

3.安装SSH

root@tony-pc:~# apt install openssh-server -y
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成
将会同时安装下列软件:
  ncurses-term openssh-client openssh-sftp-server ssh-import-id
建议安装:
  keychain libpam-ssh monkeysphere ssh-askpass molly-guard
下列【新】软件包将被安装:
  ncurses-term openssh-server openssh-sftp-server ssh-import-id
下列软件包将被升级:
  openssh-client
升级了 1 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 223 个软件包未被升级。
需要下载 752 kB/1,658 kB 的归档。
解压缩后会消耗 6,050 kB 的额外空间。
获取:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/main amd64 openssh-sftp-server amd64 1:8.9p1-3ubuntu0.6 [38.7 kB]
获取:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/main amd64 openssh-server amd64 1:8.9p1-3ubuntu0.6 [435 kB]
获取:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/main amd64 ncurses-term all 6.3-2ubuntu0.1 [267 kB]
获取:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/main amd64 ssh-import-id all 5.11-0ubuntu1 [10.1 kB]
已下载 752 kB,耗时 1秒 (656 kB/s)
正在预设定软件包 ...
(正在读取数据库 ... 系统当前共安装有 203959 个文件和目录。)
准备解压 .../openssh-client_1%3a8.9p1-3ubuntu0.6_amd64.deb  ...
正在解压 openssh-client (1:8.9p1-3ubuntu0.6) 并覆盖 (1:8.9p1-3ubuntu0.3) ...
正在选中未选择的软件包 openssh-sftp-server。
准备解压 .../openssh-sftp-server_1%3a8.9p1-3ubuntu0.6_amd64.deb  ...
正在解压 openssh-sftp-server (1:8.9p1-3ubuntu0.6) ...
正在选中未选择的软件包 openssh-server。
准备解压 .../openssh-server_1%3a8.9p1-3ubuntu0.6_amd64.deb  ...
正在解压 openssh-server (1:8.9p1-3ubuntu0.6) ...
正在选中未选择的软件包 ncurses-term。
准备解压 .../ncurses-term_6.3-2ubuntu0.1_all.deb  ...
正在解压 ncurses-term (6.3-2ubuntu0.1) ...
正在选中未选择的软件包 ssh-import-id。
准备解压 .../ssh-import-id_5.11-0ubuntu1_all.deb  ...
正在解压 ssh-import-id (5.11-0ubuntu1) ...
正在设置 openssh-client (1:8.9p1-3ubuntu0.6) ...
正在设置 ssh-import-id (5.11-0ubuntu1) ...
正在设置 ncurses-term (6.3-2ubuntu0.1) ...
正在设置 openssh-sftp-server (1:8.9p1-3ubuntu0.6) ...
正在设置 openssh-server (1:8.9p1-3ubuntu0.6) ...

Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:bndQgZqdIx46wW2aSnjQ1Vsa37+rUa4f+P/XMTJZVGk root@tony-pc (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:/1xS3ffeSCz3T6T+bxTHyQJVZaCKLMgVNsMzb966440 root@tony-pc (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:eWRIp634lPn8+Cf2HUL9eCFDq2p9BTiGvCE39C/5s+A root@tony-pc (ED25519)
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.servi
ce.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/s
ystemd/system/ssh.service.
rescue-ssh.target is a disabled or a static unit, not starting it.
ssh.socket is a disabled or a static unit, not starting it.
正在处理用于 man-db (2.10.2-1) 的触发器 ...
正在处理用于 ufw (0.36.1-4ubuntu0.1) 的触发器 ...

安装完成后,SSH服务通常会自动启动。可以使用以下命令检查SSH服务的状态;

4.查看服务状态

root@tony-pc:~# systemctl status ssh
	● ssh.service - OpenBSD Secure Shell server
	     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: e>
	     Active: active (running) since Wed 2024-03-13 14:04:22 CST; 33s ago
	       Docs: man:sshd(8)
	             man:sshd_config(5)
	   Main PID: 5634 (sshd)
	      Tasks: 1 (limit: 4555)
	     Memory: 1.7M
	        CPU: 86ms
	     CGroup: /system.slice/ssh.service
	             └─5634 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
	
	3月 13 14:04:21 tony-pc systemd[1]: Starting OpenBSD Secure Shell server...
	3月 13 14:04:22 tony-pc sshd[5634]: Server listening on 0.0.0.0 port 22.
	3月 13 14:04:22 tony-pc sshd[5634]: Server listening on :: port 22.
	3月 13 14:04:22 tony-pc systemd[1]: Started OpenBSD Secure Shell server.
	root@tony-pc:~#

可以看到加精部分已经显示的是 running 状态,表示服务正在运行;
在这里插入图片描述

5.将SSH服务加入系统启动服务中

确保SSH服务随着系统启动而运行,这里将SSH服务加入到系统服务中;

root@tony-pc:~# systemctl enable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
root@tony-pc:~#

6.使用SSH远程主机

C:\Users\Tony>ssh tony@192.168.19.130
The authenticity of host '192.168.19.130 (192.168.19.130)' can't be established.
ED25519 key fingerprint is SHA256:eWRIp634lPn8+Cf2HUL9eCFDq2p9BTiGvCE39C/5s+A.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.19.130' (ED25519) to the list of known hosts.
tony@192.168.19.130's password:
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 6.5.0-25-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

扩展安全维护(ESM)Applications 未启用。

222 更新可以立即应用。
这些更新中有 151 个是标准安全更新。
要查看这些附加更新,请运行:apt list --upgradable

启用 ESM Apps 来获取未来的额外安全更新
请参见 https://ubuntu.com/esm 或者运行: sudo pro status

Last login: Wed Mar 13 14:26:30 2024 from 192.168.19.1
tony@tony-pc:~$
tony@tony-pc:~$

在这里插入图片描述

注意:首次登录时需要输入 yes ,然后回车

  • 16
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值