19 Linux之Python定制篇-apt软件管理和远程登录
文章目录
- 19 Linux之Python定制篇-apt软件管理和远程登录
- 19.1 apt软件管理
- 19.1.1 apt介绍
- 19.1.2 更新软件下载地址-阿里源
- 19.1.3 使用apt完成安装和卸载vim
- 19.2 远程登录Ubuntu
- 学习视频来自于B站【小白入门 通俗易懂】2021韩顺平 一周学会Linux。
- 可能会用到的资料有如下所示,下载链接见文末:
- 《鸟哥的Linux私房菜 基础学习篇 第四版》1
- 《鸟哥的Linux私房菜 服务器架设篇 第三版》2
- 《韩顺平_2021图解Linux全面升级》3
19.1 apt软件管理
19.1.1 apt介绍
apt是Advanced Packaging Tool的简称,是一款应用在Ubuntu、Debian和相关Linux发行版上安装包管理工具。在Ubuntu下,我们可以使用apt命令很方便的进行软件包的安装、删除、清理等,类似于Windows中的软件管理工具(如“360软件管家”)。比如下面给出了Ubuntu下 apt软件操作相关指令 (带*号的都是重点):
# 带*号的是重点
sudo apt-get update #*更新源
sudo apt-get install package #*安装包
sudo apt-get remove package #*删除包
sudo apt-cache search package # 搜索软件包
sudo apt-cache show package #*获取包的相关信息,如说明、大小、版本等
sudo apt-get install package --reinstall # 重新安装包
sudo apt-get -f install # 修复安装
sudo apt-get remove package --purge # 删除包,包括配置文件等
sudo apt-get build-dep package # 安装相关的编译环境
sudo apt-get upgrade # 更新已安装的包
sudo apt-get dist-upgrade # 升级系统
sudo apt-cache depends package # 了解使用该包依赖那些包
sudo apt-cache rdepends package # 查看该包被哪些包依赖
sudo apt-get source package #*下载该包的源代码
比如现在我们要安装Vim编辑器,只需输入下面的一行代码就可以一键完成了:
sudo apt-get install vim
但是不出意外的话,在没有更新软件下载地址的情况下,大部分人此时肯定会下载失败或者下载巨慢,下一节就来解释为什么。
19.1.2 更新软件下载地址-阿里源
上一小节提到,在没有更新软件下载地址的情况下,大部分人使用apt下载软件是常常会下载失败或者下载巨慢,这是因为Ubuntu预装的软件下载地址(/etc/apt/sources.list
)存储的都是美国的APT服务器地址。这个“APT服务器地址”可以理解成下载软件的地方,比如Android手机上可以下载软件的“应用商店”、ios手机上可以下载软件的“AppStore”。那这个“应用商店”在美国,在国内使用Ubuntu访问肯定会极其不稳定啦。
为了解决这个问题、方便广大Linux开发者,国内很多大公司&高校,就将美国APT服务器的东西全部搬到自己的网页上,做成镜像网站,也就相当于国内的“应用商店”。此时只需要将Ubuntu默认存储的软件下载地址切换成国内的镜像源网址,就可以很方便的实现软件的安装了,如下图所示。
下面列出一些常见的镜像源地址:
- 阿里云:https://developer.aliyun.com/mirror
- 清华大学:https://mirrors.tuna.tsinghua.edu.cn/
注:更多国内的镜像源可以参考CSDN博文“国内镜像网站列表”。
吐槽:我刚安装好Ubuntu的时候,没更新源,倒霉催的啥也装不上(比如Vim)。结果按照老韩说的一步步来更新源吧,最后到复制地址到文件的时候,居!然!不!能!复!制!查了一下大概是虚拟机和Windows主机不共享剪贴板,解决了一圈没解决。我想这直接虚拟机上网复制不就好了,结果不知道为啥虚拟机又不能上网了😭。TND,我只好删除再重新安装Ubuntu虚拟机,好在可以虚拟机可以复制粘贴主机上的内容了。这一把我啥也不敢耽误,直接去更新老韩说的“清华大学镜像源”,TND的更新源的时候总是失败😭😭。最后没办法我换了阿里源,才终于更新好了😢。所以下面我演示的是更新阿里源。
打开镜像源网站后,找到Ubuntu对应的连接(网页搜索ctrl+f更快搜索),就可以找到对应的镜像地址了,如下图所示。注意一定要复制对应版本的镜像源,比如Ubuntu20.04.3就复制阿里源20.04(focal)镜像源。
现在就演示如何将/etc/apt/sources.list
文件中的美国服务器地址全改成阿里云的镜像源地址:
# 指令速览
# 1. 备份原来的镜像源地址文件
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
# 2. 清空当前的镜像源文件
echo '' > /etc/apt/sources.list
# 3. 将阿里源的内容复制到镜像源文件中
vi sources.list
################################################################
# 打开后进入vi编辑器,不熟悉的同学可以百度“vi编辑器”自行了解一下。
按i键 进入编辑模式
鼠标复制网站上的镜像源内容,然后用鼠标右键粘贴到文件中。
按下esc键
输入:wq 意为保存并退出
################################################################
# 4. 告诉系统更新源
sudo apt-get update
下面是实际的终端演示:
# 1. 为了方便,切换成root用户,并切换到/etc/apt目录
lyl@ubuntu:~/Desktop$ su root
Password:
root@ubuntu:/home/lyl/Desktop# cd /etc/apt
# 2. 备份原来的镜像源地址文件
root@ubuntu:/etc/apt# ls
apt.conf.d preferences.d sources.list trusted.gpg.d
auth.conf.d sources.bak sources.list.d
root@ubuntu:/etc/apt# cp sources.list sources.list.backup
root@ubuntu:/etc/apt# ls
apt.conf.d preferences.d sources.list sources.list.d
auth.conf.d sources.bak sources.list.backup trusted.gpg.d
# 3. 清空当前的镜像源文件
root@ubuntu:/etc/apt# echo '' > sources.list
# 4. 将阿里源的内容复制到镜像源文件中
root@ubuntu:/etc/apt# vi sources.list
################################################################
# 打开后进入vi编辑器,不熟悉的同学可以百度“vi编辑器”自行了解一下。
# 使用vi是假设之前网不好,没装上vim
按i键 进入编辑模式
鼠标复制网站上的镜像源内容,然后用鼠标右键粘贴到文件中。
按下esc键
输入:wq 意为保存并退出
################################################################
# 5. 告诉系统更新源
root@ubuntu:/etc/apt# sudo apt-get update
Get:1 https://mirrors.aliyun.com/ubuntu focal InRelease [265 kB]
Get:2 https://mirrors.aliyun.com/ubuntu focal-security InRelease [114 kB]
Get:3 https://mirrors.aliyun.com/ubuntu focal-updates InRelease [114 kB]
Get:4 https://mirrors.aliyun.com/ubuntu focal-backports InRelease [108 kB]
Get:5 https://mirrors.aliyun.com/ubuntu focal/universe Sources [9,707 kB]
Get:6 https://mirrors.aliyun.com/ubuntu focal/restricted Sources [6,472 B]
Get:7 https://mirrors.aliyun.com/ubuntu focal/main Sources [847 kB]
Get:8 https://mirrors.aliyun.com/ubuntu focal/multiverse Sources [174 kB]
Get:9 https://mirrors.aliyun.com/ubuntu focal/main i386 Packages [718 kB]
Get:10 https://mirrors.aliyun.com/ubuntu focal/main amd64 Packages [970 kB]
Get:11 https://mirrors.aliyun.com/ubuntu focal/main Translation-en [506 kB]
Get:12 https://mirrors.aliyun.com/ubuntu focal/main amd64 DEP-11 Metadata [494 kB]
Get:13 https://mirrors.aliyun.com/ubuntu focal/main DEP-11 48x48 Icons [98.4 kB]
Get:14 https://mirrors.aliyun.com/ubuntu focal/main DEP-11 64x64 Icons [163 kB]
Get:15 https://mirrors.aliyun.com/ubuntu focal/main DEP-11 64x64@2 Icons [15.8 kB]
Get:16 https://mirrors.aliyun.com/ubuntu focal/main amd64 c-n-f Metadata [29.5 kB]
Get:17 https://mirrors.aliyun.com/ubuntu focal/restricted i386 Packages [8,112 B]
Get:18 https://mirrors.aliyun.com/ubuntu focal/restricted amd64 Packages [22.0 kB]
Get:19 https://mirrors.aliyun.com/ubuntu focal/restricted Translation-en [6,212 B]
Get:20 https://mirrors.aliyun.com/ubuntu focal/restricted amd64 c-n-f Metadata [392 B]
Get:21 https://mirrors.aliyun.com/ubuntu focal/universe i386 Packages [4,642 kB]
Get:22 https://mirrors.aliyun.com/ubuntu focal/universe amd64 Packages [8,628 kB]
Get:23 https://mirrors.aliyun.com/ubuntu focal/universe Translation-en [5,124 kB]
Get:24 https://mirrors.aliyun.com/ubuntu focal/universe amd64 DEP-11 Metadata [3,603 kB]
Get:25 https://mirrors.aliyun.com/ubuntu focal/universe DEP-11 48x48 Icons [3,016 kB]
Get:26 https://mirrors.aliyun.com/ubuntu focal/universe DEP-11 64x64 Icons [7,794 kB]
Get:27 https://mirrors.aliyun.com/ubuntu focal/universe DEP-11 64x64@2 Icons [44.3 kB]
Get:28 https://mirrors.aliyun.com/ubuntu focal/universe amd64 c-n-f Metadata [265 kB]
Get:29 https://mirrors.aliyun.com/ubuntu focal/multiverse amd64 Packages [144 kB]
Get:30 https://mirrors.aliyun.com/ubuntu focal/multiverse i386 Packages [74.7 kB]
Get:31 https://mirrors.aliyun.com/ubuntu focal/multiverse Translation-en [104 kB]
Get:32 https://mirrors.aliyun.com/ubuntu focal/multiverse amd64 DEP-11 Metadata [48.4 kB]
Get:33 https://mirrors.aliyun.com/ubuntu focal/multiverse DEP-11 48x48 Icons [23.1 kB]
Get:34 https://mirrors.aliyun.com/ubuntu focal/multiverse DEP-11 64x64 Icons [192 kB]
Get:35 https://mirrors.aliyun.com/ubuntu focal/multiverse DEP-11 64x64@2 Icons [214 B]
Get:36 https://mirrors.aliyun.com/ubuntu focal/multiverse amd64 c-n-f Metadata [9,136 B]
Get:37 https://mirrors.aliyun.com/ubuntu focal-security/main Sources [289 kB]
Get:38 https://mirrors.aliyun.com/ubuntu focal-security/universe Sources [192 kB]
Get:39 https://mirrors.aliyun.com/ubuntu focal-security/restricted Sources [54.1 kB]
Get:40 https://mirrors.aliyun.com/ubuntu focal-security/multiverse Sources [13.8 kB]
Get:41 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 Packages [2,362 kB]
Get:42 https://mirrors.aliyun.com/ubuntu focal-security/main i386 Packages [632 kB]
Get:43 https://mirrors.aliyun.com/ubuntu focal-security/main Translation-en [373 kB]
Get:44 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 DEP-11 Metadata [59.9 kB]
Get:45 https://mirrors.aliyun.com/ubuntu focal-security/main DEP-11 48x48 Icons [18.9 kB]
Get:46 https://mirrors.aliyun.com/ubuntu focal-security/main DEP-11 64x64 Icons [36.0 kB]
Get:47 https://mirrors.aliyun.com/ubuntu focal-security/main DEP-11 64x64@2 Icons [29 B]
Get:48 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 c-n-f Metadata [13.0 kB]
Get:49 https://mirrors.aliyun.com/ubuntu focal-security/restricted amd64 Packages [2,070 kB]
Get:50 https://mirrors.aliyun.com/ubuntu focal-security/restricted i386 Packages [32.3 kB]
Get:51 https://mirrors.aliyun.com/ubuntu focal-security/restricted Translation-en [289 kB]
Get:52 https://mirrors.aliyun.com/ubuntu focal-security/restricted amd64 c-n-f Metadata [580 B]
Get:53 https://mirrors.aliyun.com/ubuntu focal-security/universe i386 Packages [611 kB]
Get:54 https://mirrors.aliyun.com/ubuntu focal-security/universe amd64 Packages [872 kB]
Get:55 https://mirrors.aliyun.com/ubuntu focal-security/universe Translation-en [183 kB]
Get:56 https://mirrors.aliyun.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [96.3 kB]
Get:57 https://mirrors.aliyun.com/ubuntu focal-security/universe DEP-11 48x48 Icons [52.0 kB]
Get:58 https://mirrors.aliyun.com/ubuntu focal-security/universe DEP-11 64x64 Icons [101 kB]
Get:59 https://mirrors.aliyun.com/ubuntu focal-security/universe DEP-11 64x64@2 Icons [29 B]
Get:60 https://mirrors.aliyun.com/ubuntu focal-security/universe amd64 c-n-f Metadata [19.0 kB]
Get:61 https://mirrors.aliyun.com/ubuntu focal-security/multiverse amd64 Packages [23.6 kB]
Get:62 https://mirrors.aliyun.com/ubuntu focal-security/multiverse i386 Packages [7,196 B]
Get:63 https://mirrors.aliyun.com/ubuntu focal-security/multiverse Translation-en [5,504 B]
Get:64 https://mirrors.aliyun.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [940 B]
Get:65 https://mirrors.aliyun.com/ubuntu focal-security/multiverse DEP-11 48x48 Icons [1,867 B]
Get:66 https://mirrors.aliyun.com/ubuntu focal-security/multiverse DEP-11 64x64 Icons [2,497 B]
Get:67 https://mirrors.aliyun.com/ubuntu focal-security/multiverse DEP-11 64x64@2 Icons [29 B]
Get:68 https://mirrors.aliyun.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [548 B]
Get:69 https://mirrors.aliyun.com/ubuntu focal-updates/restricted Sources [54.2 kB]
Get:70 https://mirrors.aliyun.com/ubuntu focal-updates/main Sources [571 kB]
Get:71 https://mirrors.aliyun.com/ubuntu focal-updates/universe Sources [342 kB]
Get:72 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse Sources [24.4 kB]
Get:73 https://mirrors.aliyun.com/ubuntu focal-updates/main i386 Packages [864 kB]
Get:74 https://mirrors.aliyun.com/ubuntu focal-updates/main amd64 Packages [2,753 kB]
Get:75 https://mirrors.aliyun.com/ubuntu focal-updates/main Translation-en [456 kB]
Get:76 https://mirrors.aliyun.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [275 kB]
Get:77 https://mirrors.aliyun.com/ubuntu focal-updates/main DEP-11 48x48 Icons [60.8 kB]
Get:78 https://mirrors.aliyun.com/ubuntu focal-updates/main DEP-11 64x64 Icons [98.3 kB]
Get:79 https://mirrors.aliyun.com/ubuntu focal-updates/main DEP-11 64x64@2 Icons [29 B]
Get:80 https://mirrors.aliyun.com/ubuntu focal-updates/main amd64 c-n-f Metadata [17.0 kB]
Get:81 https://mirrors.aliyun.com/ubuntu focal-updates/restricted i386 Packages [33.7 kB]
Get:82 https://mirrors.aliyun.com/ubuntu focal-updates/restricted amd64 Packages [2,185 kB]
Get:83 https://mirrors.aliyun.com/ubuntu focal-updates/restricted Translation-en [306 kB]
Get:84 https://mirrors.aliyun.com/ubuntu focal-updates/restricted amd64 c-n-f Metadata [576 B]
Get:85 https://mirrors.aliyun.com/ubuntu focal-updates/universe i386 Packages [744 kB]
Get:86 https://mirrors.aliyun.com/ubuntu focal-updates/universe amd64 Packages [1,101 kB]
Get:87 https://mirrors.aliyun.com/ubuntu focal-updates/universe Translation-en [263 kB]
Get:88 https://mirrors.aliyun.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [410 kB]
Get:89 https://mirrors.aliyun.com/ubuntu focal-updates/universe DEP-11 48x48 Icons [280 kB]
Get:90 https://mirrors.aliyun.com/ubuntu focal-updates/universe DEP-11 64x64 Icons [493 kB]
Get:91 https://mirrors.aliyun.com/ubuntu focal-updates/universe DEP-11 64x64@2 Icons [29 B]
Get:92 https://mirrors.aliyun.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [25.5 kB]
Get:93 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse amd64 Packages [25.8 kB]
Get:94 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse i386 Packages [8,436 B]
Get:95 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse Translation-en [7,484 B]
Get:96 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:97 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse DEP-11 48x48 Icons [1,867 B]
Get:98 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse DEP-11 64x64 Icons [2,497 B]
Get:99 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse DEP-11 64x64@2 Icons [29 B]
Get:100 https://mirrors.aliyun.com/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [620 B]
Get:101 https://mirrors.aliyun.com/ubuntu focal-backports/main Sources [9,608 B]
Get:102 https://mirrors.aliyun.com/ubuntu focal-backports/universe Sources [10.5 kB]
Get:103 https://mirrors.aliyun.com/ubuntu focal-backports/main amd64 Packages [45.7 kB]
Get:104 https://mirrors.aliyun.com/ubuntu focal-backports/main i386 Packages [36.1 kB]
Get:105 https://mirrors.aliyun.com/ubuntu focal-backports/main Translation-en [16.3 kB]
Get:106 https://mirrors.aliyun.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7,968 B]
Get:107 https://mirrors.aliyun.com/ubuntu focal-backports/main DEP-11 48x48 Icons [7,156 B]
Get:108 https://mirrors.aliyun.com/ubuntu focal-backports/main DEP-11 64x64 Icons [10.7 kB]
Get:109 https://mirrors.aliyun.com/ubuntu focal-backports/main DEP-11 64x64@2 Icons [29 B]
Get:110 https://mirrors.aliyun.com/ubuntu focal-backports/main amd64 c-n-f Metadata [1,420 B]
Get:111 https://mirrors.aliyun.com/ubuntu focal-backports/restricted amd64 c-n-f Metadata [116 B]
Get:112 https://mirrors.aliyun.com/ubuntu focal-backports/universe i386 Packages [13.8 kB]
Get:113 https://mirrors.aliyun.com/ubuntu focal-backports/universe amd64 Packages [25.0 kB]
Get:114 https://mirrors.aliyun.com/ubuntu focal-backports/universe Translation-en [16.3 kB]
Get:115 https://mirrors.aliyun.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [30.5 kB]
Get:116 https://mirrors.aliyun.com/ubuntu focal-backports/universe DEP-11 48x48 Icons [13.3 kB]
Get:117 https://mirrors.aliyun.com/ubuntu focal-backports/universe DEP-11 64x64 Icons [22.7 kB]
Get:118 https://mirrors.aliyun.com/ubuntu focal-backports/universe DEP-11 64x64@2 Icons [29 B]
Get:119 https://mirrors.aliyun.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [880 B]
Get:120 https://mirrors.aliyun.com/ubuntu focal-backports/multiverse amd64 c-n-f Metadata [116 B]
Fetched 68.2 MB in 18s (3,864 kB/s)
Reading package lists... Done
# 看到Done就证明更新成功!
19.1.3 使用apt完成安装和卸载vim
本节以常用的Vim编辑器这款软件为例,演示如何使用apt完成软件的安装、卸载、查询软件信息。下面给出指令速览:
# 1. 安装Vim
sudo apt-get install vim
# 2. 安装Vim完成后,使用Vim创建hello.txt,验证可以正常使用vim指令
vim hello.txt
# 3. 查看Vim的安装信息
sudo apt-cache show vim
# 4. 删除Vim(选做)
sudo apt-get remove vim
下面是实际的终端演示:
# 1. 首先验证没有安装Vim之前,是无法使用Vim指令的
lyl@ubuntu:~/Desktop$ vim hello.txt
Command 'vim' not found, but can be installed with:
sudo apt install vim # version 2:8.1.2269-1ubuntu5.16, or
sudo apt install vim-tiny # version 2:8.1.2269-1ubuntu5.16
sudo apt install neovim # version 0.4.3-3
sudo apt install vim-athena # version 2:8.1.2269-1ubuntu5.16
sudo apt install vim-gtk3 # version 2:8.1.2269-1ubuntu5.16
sudo apt install vim-nox # version 2:8.1.2269-1ubuntu5.16
# 2. 安装Vim
lyl@ubuntu:~/Desktop$ sudo apt-get install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
vim-common vim-runtime vim-tiny
Suggested packages:
ctags vim-doc vim-scripts indent
The following NEW packages will be installed:
vim vim-runtime
The following packages will be upgraded:
vim-common vim-tiny
2 upgraded, 2 newly installed, 0 to remove and 578 not upgraded.
Need to get 7,787 kB of archives.
After this operation, 34.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 vim-tiny amd64 2:8.1.2269-1ubuntu5.16 [581 kB]
Get:2 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 vim-common all 2:8.1.2269-1ubuntu5.16 [87.2 kB]
Get:3 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 vim-runtime all 2:8.1.2269-1ubuntu5.16 [5,878 kB]
Get:4 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 vim amd64 2:8.1.2269-1ubuntu5.16 [1,241 kB]
Fetched 7,787 kB in 1s (8,606 kB/s)
(Reading database ... 156930 files and directories currently installed.)
Preparing to unpack .../vim-tiny_2%3a8.1.2269-1ubuntu5.16_amd64.deb ...
Unpacking vim-tiny (2:8.1.2269-1ubuntu5.16) over (2:8.1.2269-1ubuntu5) ...
Preparing to unpack .../vim-common_2%3a8.1.2269-1ubuntu5.16_all.deb ...
Unpacking vim-common (2:8.1.2269-1ubuntu5.16) over (2:8.1.2269-1ubuntu5) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../vim-runtime_2%3a8.1.2269-1ubuntu5.16_all.deb ...
Adding 'diversion of /usr/share/vim/vim81/doc/help.txt to /usr/share/vim/vim81/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim81/doc/tags to /usr/share/vim/vim81/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:8.1.2269-1ubuntu5.16) ...
Selecting previously unselected package vim.
Preparing to unpack .../vim_2%3a8.1.2269-1ubuntu5.16_amd64.deb ...
Unpacking vim (2:8.1.2269-1ubuntu5.16) ...
Setting up vim-common (2:8.1.2269-1ubuntu5.16) ...
Setting up vim-runtime (2:8.1.2269-1ubuntu5.16) ...
Setting up vim (2:8.1.2269-1ubuntu5.16) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
Setting up vim-tiny (2:8.1.2269-1ubuntu5.16) ...
Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
Processing triggers for man-db (2.9.1-1) ...
# 3. 安装Vim完成后,使用Vim创建hello.txt,并验证
lyl@ubuntu:~/Desktop$ vim hello.txt
###################################
# Vim编辑器写上:hello,vim!
###################################
lyl@ubuntu:~/Desktop$ cat hello.txt
hello,vim!
# 4. 还可以查看Vim的安装信息
lyl@ubuntu:~/Desktop$ sudo apt-cache show vim
Package: vim
Architecture: amd64
Version: 2:8.1.2269-1ubuntu5.16
Priority: optional
Section: editors
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Vim Maintainers <pkg-vim-maintainers@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 3046
Provides: editor
Depends: vim-common (= 2:8.1.2269-1ubuntu5.16), vim-runtime (= 2:8.1.2269-1ubuntu5.16), libacl1 (>= 2.2.23), libc6 (>= 2.29), libcanberra0 (>= 0.2), libgpm2 (>= 1.20.7), libpython3.8 (>= 3.8.2), libselinux1 (>= 1.32), libtinfo6 (>= 6)
Suggests: ctags, vim-doc, vim-scripts
Filename: pool/main/v/vim/vim_8.1.2269-1ubuntu5.16_amd64.deb
Size: 1240908
MD5sum: ab9e41ee56638f5fd750f15cafaffcec
SHA1: 52dd1e72a54c753b1520e5dda3b5f83df84e5d91
SHA256: 699c0a3e7931022b68f7e805ad53cbfd49b135a86fd8dd94d737a539c99f2477
SHA512: dfa74ddca1ca1e46306c946f0025bb657e9edce773caea90816720d3c5c10fd83c65fcdd39e3a6931accccf5a2a4d288ba41f014763bd15a9d558d41896b809d
Homepage: https://www.vim.org/
Description-en: Vi IMproved - enhanced vi editor
Vim is an almost compatible version of the UNIX editor Vi.
.
Many new features have been added: multi level undo, syntax
highlighting, command line history, on-line help, filename
completion, block operations, folding, Unicode support, etc.
.
This package contains a version of vim compiled with a rather
standard set of features. This package does not provide a GUI
version of Vim. See the other vim-* packages if you need more
(or less).
Description-md5: 59e8b8f7757db8b53566d5d119872de8
Task: server, cloud-image, lubuntu-desktop
注:安装过程中可以体会到,因为使用了镜像网站,软件下载速度很快。
19.2 远程登录Ubuntu
那一般进行软件开发时,肯定是用自己的电脑最习惯,而不是直接在Ubuntu上敲代码,所以Ubuntu的最后一节来讲讲如何远程登录Ubuntu。和CentOS自带远程登录服务不同,Ubuntu上默认没有安装“SSH服务”,所以使用Putty或Xshell等工具远程连接Ubuntu时,会遇到拒绝连接服务。SSH为Secure Shell的缩写,由IETF的网络工作小组(Network Working Group)所制定,是一种建立在应用层和传输层基础上的安全协议。SSH是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议,几乎所有UNIX/Linux平台都可运行SSH。
严格来说,使用SSH服务,需要安装相应的“服务器端(sshd)”和“客户端(ssh)”。也就是说,如果A机器想被B机器远程控制,那么A机器需要安装SSH服务器端,B机器需要安装SSH客户端。比如上图所示情况,Windows系统和UbuntuB都想要访问UbuntuA,那么Windiws和UbuntuB就需要安装SSH客户端,而被访问的UbuntuA就需要安装SSH服务端。但通常Ubuntu系统的SSH的安装指令会同时将“服务端”和“客户端”都安装好:
sudo apt-get install openssh-server
演示1:查看Ubuntu默认没有安装SSHD服务
下面演示Ubuntu默认没有安装SSHD服务,我们不能进行远程登录。指令速览:
# 1. 先安装netstat指令
lyl@ubuntu:~/Desktop$ sudo apt install net-tools
[sudo] password for lyl:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
net-tools
0 upgraded, 1 newly installed, 0 to remove and 578 not upgraded.
Need to get 196 kB of archives.
After this operation, 864 kB of additional disk space will be used.
Get:1 https://mirrors.aliyun.com/ubuntu focal/main amd64 net-tools amd64 1.60+git20180626.aebd88e-1ubuntu1 [196 kB]
Fetched 196 kB in 0s (685 kB/s)
Selecting previously unselected package net-tools.
(Reading database ... 158766 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20180626.aebd88e-1ubuntu1_amd64.deb ...
Unpacking net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...
Setting up net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...
Processing triggers for man-db (2.9.1-1) ...
# 2. 查看处于LISTEN状态的端口
# 可以看到下面处于LISTEN状态的端口并没有22端口,说明Ubuntu默认并没有SSHD服务监听。
lyl@ubuntu:~/Desktop$ netstat -anp | more
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
-
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
-
tcp 0 0 192.168.204.129:35502 27.22.57.186:443 TIME_WAIT
-
tcp6 0 0 ::1:631 :::* LISTEN
-
udp 0 0 0.0.0.0:60691 0.0.0.0:*
-
udp 0 0 0.0.0.0:5353 0.0.0.0:*
-
udp 0 0 127.0.0.53:53 0.0.0.0:*
-
udp 0 0 192.168.204.129:68 192.168.204.254:67 ESTABLISHED
-
udp 0 0 0.0.0.0:631 0.0.0.0:*
-
udp6 0 0 :::5353 :::*
-
演示2:Ubuntu安装SSH服务并启用
下面就需要安装SSH服务并启用,指令速览:
# 1. 安装SSH服务
sudo apt-get install openssh-server
# 2. 启动sshd服务(会监听端口22)
service sshd restart
注意上面安装SSH服务的指令会在当前Linux上同时安装SSH服务端和客户端,而不只是服务端(server)!
下面是实际的终端演示:
# 实机演示
sudo apt-get install openssh-server
service sshd restart
netstat -anp | more
# 1. 安装SSH服务
lyl@ubuntu:~/Desktop$ sudo apt-get install openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
ncurses-term openssh-client openssh-sftp-server ssh-import-id
Suggested packages:
keychain libpam-ssh monkeysphere ssh-askpass molly-guard
The following NEW packages will be installed:
ncurses-term openssh-server openssh-sftp-server ssh-import-id
The following packages will be upgraded:
openssh-client
1 upgraded, 4 newly installed, 0 to remove and 577 not upgraded.
Need to get 1,360 kB of archives.
After this operation, 6,019 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 openssh-client amd64 1:8.2p1-4ubuntu0.9 [671 kB]
Get:2 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 ncurses-term all 6.2-0ubuntu2.1 [249 kB]
Get:3 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 openssh-sftp-server amd64 1:8.2p1-4ubuntu0.9 [51.7 kB]
Get:4 https://mirrors.aliyun.com/ubuntu focal-security/main amd64 openssh-server amd64 1:8.2p1-4ubuntu0.9 [377 kB]
Get:5 https://mirrors.aliyun.com/ubuntu focal/main amd64 ssh-import-id all 5.10-0ubuntu1 [10.0 kB]
Fetched 1,360 kB in 1s (2,091 kB/s)
Preconfiguring packages ...
(Reading database ... 158815 files and directories currently installed.)
Preparing to unpack .../openssh-client_1%3a8.2p1-4ubuntu0.9_amd64.deb ...
Unpacking openssh-client (1:8.2p1-4ubuntu0.9) over (1:8.2p1-4ubuntu0.1) ...
Selecting previously unselected package ncurses-term.
Preparing to unpack .../ncurses-term_6.2-0ubuntu2.1_all.deb ...
Unpacking ncurses-term (6.2-0ubuntu2.1) ...
Selecting previously unselected package openssh-sftp-server.
Preparing to unpack .../openssh-sftp-server_1%3a8.2p1-4ubuntu0.9_amd64.deb ...
Unpacking openssh-sftp-server (1:8.2p1-4ubuntu0.9) ...
Selecting previously unselected package openssh-server.
Preparing to unpack .../openssh-server_1%3a8.2p1-4ubuntu0.9_amd64.deb ...
Unpacking openssh-server (1:8.2p1-4ubuntu0.9) ...
Selecting previously unselected package ssh-import-id.
Preparing to unpack .../ssh-import-id_5.10-0ubuntu1_all.deb ...
Unpacking ssh-import-id (5.10-0ubuntu1) ...
Setting up openssh-client (1:8.2p1-4ubuntu0.9) ...
Setting up ssh-import-id (5.10-0ubuntu1) ...
Attempting to convert /etc/ssh/ssh_import_id
Setting up ncurses-term (6.2-0ubuntu2.1) ...
Setting up openssh-sftp-server (1:8.2p1-4ubuntu0.9) ...
Setting up openssh-server (1:8.2p1-4ubuntu0.9) ...
Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:Mi2n0nliGPMjuoFm8UhtdM+j49m5MyG6qUQtPidJX/k root@ubuntu (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:BI4Iz6Tkl+/MaqEw1rKuAsBTlKimuFJ5uWKYPbx960s root@ubuntu (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:juRN5ixuAQ8H4zJc7ACTntHWte4r15oLtZccbY+6mZU root@ubuntu (ED25519)
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
rescue-ssh.target is a disabled or a static unit, not starting it.
Processing triggers for systemd (245.4-4ubuntu3.2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for ufw (0.36-6) ...
# 2. 启动sshd服务(会监听端口22)
lyl@ubuntu:~/Desktop$ service sshd restart
# 3. 查看处于LISTEN状态的端口是否有22
lyl@ubuntu:~/Desktop$ netstat -anp | more
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
-
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
-
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
-
tcp 0 0 192.168.204.129:58732 27.159.77.240:443 TIME_WAIT
-
tcp6 0 0 ::1:631 :::* LISTEN
-
tcp6 0 0 :::22 :::* LISTEN # 显然这里有监听
-
udp 0 0 0.0.0.0:60691 0.0.0.0:*
-
udp 0 0 0.0.0.0:5353 0.0.0.0:*
-
udp 0 0 127.0.0.53:53 0.0.0.0:*
-
udp 0 0 192.168.204.129:68 192.168.204.254:67 ESTABLISHED
-
演示3:远程登录Ubuntu
安装好SSH服务后,就可以远程登录Ubuntu了,共有下面两种情况:
- Windows远程登录UbuntuA:和CentOS相同,先在UbuntuA使用
ifconfig
查看Ubuntu的IP地址,然后在Windows上使用Xshell即可远程登录UbuntuA。- UbuntuB远程登录UbuntuA:在创建服务器集群时,会使用到该技术。UbuntuB只需开启“客户端(ssh)”,无需开启“服务器端(sshd)”。基本语法如下:
# 基本语法-ssh登录
ssh 用户名@IP
# 例:ssh Ubuntu@192.168.200.222
# 注:若使用ssh访问出现错误,可查看是否有该文件/.ssh/known_ssh并尝试删除解决,一般不会有问题。
# 基本语法-ssh登出(均可)
exit
logout
注:直接复制刚建好的Ubuntu虚拟机文件夹,然后就可以快速得到一个新的Ubuntu系统。但这种情况下两个主机的主机名完全相同,远程登录时可以使用
ifconfig
查看IP地址可以发现当前登录的账户有所不同。
由于远程登录演示窗口开的有点多,但原理又不复杂,所以下面只给出UbuntuB远程登录UbuntuA的代码速览,而没有实际的终端演示:
# 1. 打开UbuntuA的终端,查看UbuntuA的IP地址,假设为192.168.200.222,主机名为UbuntuA
UbuntuA终端:ifconfig
# 2. 打开UbuntuB的终端,查看UbuntuB的IP地址,假设为192.168.200.220
UbuntuB终端:ifconfig
# 3. 然后远程登录到UbuntuA
UbuntuB终端:ssh UbuntuA@192.168.200.222
# 4. 再次查看IP地址,发现已经变成了UbuntuA的IP,证明远程登录成功
UbuntuB终端:ifconfig
《鸟哥的Linux私房菜 基础学习篇 第四版》 ↩︎
《鸟哥的Linux私房菜 服务器架设篇 第三版》 ↩︎
《韩顺平_2021图解Linux全面升级》 ↩︎