Ubuntu系统的基础操作和使用

一、Ubuntu系统介绍

1.Ubuntu系统介绍

Ubuntu(友帮拓、优般图、乌班图)是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu 是基于Debian GNU/Linux,支持x86、amd64(即x64)和ppc架构,由全球化的专业开发团队(Canonical Ltd)打造的。

2.Ubuntu与其他Linux发行版区别

①当前的Linux发行版本

目前已有大量各种各样基于 GNU/Linux的操作系统,例如:Debian, SuSE, Gentoo, RedHat 、 Mandriva、华为EulerOS、Centos等。在这业已竞争纷繁的世界里,Ubuntu 是又一个参与者

②区别
  1. Debian 是一个广受称道、技术先进且有着良好支持的发行版,Ubuntu 正是基于 Debian之上,旨在创建一个可以为桌面和服务器提供一个最新且一贯的 Linux 系统。
  2. Ubuntu 囊括了大量从 Debian 发行版精挑细选的软件包,同时保留了 Debian 强大的软件包管理系统,以便简易的安装或彻底的删除程序。
  3. 与大多数发行版附带数量巨大的可用可不用的软件不同,Ubuntu 的软件包清单只包含那些高质量的重要应用程序。
  4. 注重质量,Ubuntu提供了一个健壮、功能丰富的计算环境,既适合家用又适用于商业环境。
  5. 本项目花费了大量必要的时间,努力精益求精,每6个月就会发布一个版本,以提供最新最强大的软件。Ubuntu支持各种形形色色的架构,包括 i386 (386/486/Pentium(II/III/IV)和Athlon/Duron/Sempron 处理器),AMD64(Athlon64, Opteron及64位 Intel 处理器),以及PowerPC(iBook/Powerbook, G4 and G5)等。

3.当前测试的ubuntu版本


root@node3:~# cat /etc/os-release NAME="Ubuntu" VERSION="20.04.4 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.04.4 LTS" VERSION_ID="20.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=focal UBUNTU_CODENAME=focal 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

4.Ubuntu官网

 Ubuntu官网

Ubuntu系统的基础操作和使用_Ubuntu

二、Ubuntu更新软件源

1.查看当前的软件源


root@node3:~# cat /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

2.备份当前的sources.list文件


cp /etc/apt/sources.list /backup/source/ 
  • 1.

3.删除原sources.list文件,重新编辑


deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

4.更新源


root@node3:~# apt-get update 命中:1 http://mirrors.aliyun.com/ubuntu focal InRelease 获取:2 http://mirrors.aliyun.com/ubuntu focal-security InRelease [114 kB] 获取:3 http://mirrors.aliyun.com/ubuntu focal-updates InRelease [114 kB] 获取:4 http://mirrors.aliyun.com/ubuntu focal-backports InRelease [108 kB] 获取:5 http://mirrors.aliyun.com/ubuntu focal-proposed InRelease [267 kB] 获取:6 http://mirrors.aliyun.com/ubuntu focal-security/universe Sources [110 kB] 获取:7 http://mirrors.aliyun.com/ubuntu focal-security/main Sources [277 kB] 获取:8 http://mirrors.aliyun.com/ubuntu focal-security/universe amd64 Packages [870 kB] 获取:9 http://mirrors.aliyun.com/ubuntu focal-security/main amd64 Packages [1,777 kB] 获取:10 http://mirrors.aliyun.com/ubuntu focal-updates/universe Sources [297 kB] 获取:11 http://mirrors.aliyun.com/ubuntu focal-updates/main Sources [593 kB] 获取:12 http://mirrors.aliyun.com/ubuntu focal-updates/main amd64 Packages [2,191 kB] 获取:13 http://mirrors.aliyun.com/ubuntu focal-updates/universe amd64 Packages [1,155 kB] 获取:14 http://mirrors.aliyun.com/ubuntu focal-backports/main Sources [8,254 B] 获取:15 http://mirrors.aliyun.com/ubuntu focal-backports/universe amd64 Packages [26.0 kB] 获取:16 http://mirrors.aliyun.com/ubuntu focal-backports/main amd64 Packages [51.2 kB] 获取:17 http://mirrors.aliyun.com/ubuntu focal-proposed/restricted Sources [21.2 kB] 获取:18 http://mirrors.aliyun.com/ubuntu focal-proposed/main Sources [71.3 kB] 获取:19 http://mirrors.aliyun.com/ubuntu focal-proposed/universe Sources [29.2 kB] 获取:20 http://mirrors.aliyun.com/ubuntu focal-proposed/universe amd64 Packages [79.1 kB] 获取:21 http://mirrors.aliyun.com/ubuntu focal-proposed/main amd64 Packages [482 kB] 获取:22 http://mirrors.aliyun.com/ubuntu focal-proposed/restricted amd64 Packages [421 kB] 已下载 9,062 kB,耗时 9(966 kB/s) 正在读取软件包列表... 完成 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

5.update和upgrade区别

update是更新软件列表,upgrade是更新软件。这两个命令一般是一起使用

6.apt和apt-get区别


随着 apt install package 命令的使用频率和普遍性逐步超过 apt-get install package,越来越多的其它 Linux 发行版也开始遵循 Ubuntu 的脚步,开始鼓励用户使用 apt 而不是 apt-get
  • 1.

Ubuntu系统的基础操作和使用_Ubuntu_02

三、安装mariadb

1.安装mariadb并设置开机自启


apt install -y mariadb-server systemctl enable --now mariadb 
  • 1.
  • 2.

2.查看mariadb状态


root@node3:~# systemctl status mariadb ● mariadb.service - MariaDB 10.3.34 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2022-05-01 15:05:18 CST; 1min 19s ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 3565 (mysqld) Status: "Taking your SQL requests now..." Tasks: 31 (limit: 38260) Memory: 70.4M CGroup: /kubepods/burstable/pod9d8aa482-9bdb-4613-891a-ed40f6989d17/0afd6e17a4208fcc2f7d 7560a4238aee852fd9b497cca86a35ef0b67/system.slice/mariadb.service └─3565 /usr/sbin/mysqld 501 15:05:18 node3 systemd[1]: Starting MariaDB 10.3.34 database server... 501 15:05:18 node3 mysqld[3565]: 2022-05-01 15:05:18 0 [Note] /usr/sbin/mysqld (mysqld 10.3.34-Mar iaDB-0ubuntu0.20.04.1) starting as process 3565 ... 501 15:05:18 node3 systemd[1]: Started MariaDB 10.3.34 database server. 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

3.进入数据库


root@node3:~# mariadb Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 36 Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.001 sec) MariaDB [(none)]> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

四、ubuntu的常用命令

1.常用apt操作


sudo apt install <软件名> //安装软件最简单的方式 sudo apt list //查看所有已安装的软件列表 sudo apt search <软件名> //搜索某个软件 sudo apt remove <软件名> //删除某个软件包 sudo apt purge <软件名> //删除某个软件包以及配置文件,更彻底 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

2.dpkg (Debian package)包管理工具

①安装软件

先在官网下载软件的deb格式安装包,然后cd到下载文件夹,打开terminal(终端)输入: dpkg -i <.deb后缀的软件名> //i 表示 install 
  • 1.
  • 2.

②卸载软件

dpkg -r <包的名字> //r 表示 remove, 此种方法会保留配置文件 dpkg -P <包的名字> //直接全删了,配置也不会保留 dpkg -l //查看安装列表 dpkg -S <包的名字> //搜索某个包 
  • 1.
  • 2.
  • 3.
  • 4.

3.文件系统管理

①常用的文件操作

ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd - 回上一次所在的目录 cd ~ 或 cd 回当前用户的宿主目录 mkdir 目录名 创建一个目录 rmdir 空目录名 删除一个空目录 rm 文件名 文件名 删除一个文件或多个文件 rm -rf 非空目录名 删除一个非空目录下的一切 mv 路经/文件 //文件移动相对路经下的文件到绝对路经下 mv 文件名 新名称 在当前目录下改名 find 路经 -name “字符串” 查找路经所在范围内满足字符串匹配的文件和目录 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

②文件的解压缩

tar -c 创建包 –x 释放包 -v 显示命令过程 –z 代表压缩包 tar –cvf benet.tar /home/benet 把/home/benet目录打包 tar –zcvf benet.tar.gz /mnt 把目录打包并压缩 tar –zxvf benet.tar.gz 压缩包的文件解压恢复 tar –jxvf benet.tar.bz2 解压缩 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

4.系统管理常用操作


fdisk -l #查看系统分区信息 fdisk /dev/sdb #为一块新的SCSI硬盘进行分区 chown root /home #把/home的属主改成root用户 chgrp root /home #把/home的属组改成root组 Useradd #创建一个新的用户 Groupadd 组名 #创建一个新的组 Passwd 用户名 #为用户创建密码 Passwd -d用户名 #删除用户密码也能登陆 Passwd -S用户名 #查询账号密码 Usermod -l 新用户名 老用户名 #为用户改名 uname -a #查看内核版本 cat /etc/issue #查看ubuntu版本 lsusb #查看usb设备 sudo ethtool eth0 #查看网卡状态 cat /proc/cpuinfo #查看cpu信息 lshw 查看当前硬件信息 sudo fdisk -l #查看磁盘信息 df -h #查看硬盘剩余空间 free -m #查看当前的内存使用情况 ps -A #查看当前有哪些进程 kill 进程号(就是ps -A中的第一列的数字)或者 killall 进程名( 杀死一个进程) kill -9 进程号 #强制杀死一个进程 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

五、系统主要目录

六、系统的监控和管理

1.动态监控系统资源占用

top命令


进程号 USER PR NI VIRT RES SHR %CPU %MEM TIME+ COMMAND 1 root 20 0  12044 8524 S 0.0 0.6 0:01.75 systemd 33 root 20 0 11792 2980 2716 S 0.0 0.1 0:00.15 cron 34 message+ 20 0 7532 4728 3912 S 0.0 0.2 0:00.32 dbus-daemon 35 root 20 0 34000 18240 10652 S 0.0 0.9 0:00.05 networkd-dispat 38 root 20 0 16672 7276 6436 S 0.0 0.3 0:00.25 systemd-logind 41 root 20 0 12176 7344 6424 S 0.0 0.4 0:00.00 sshd 655 root 20 0 17980 8544 7444 S 0.0 0.4 0:00.00 systemd 656 root 20 0 20452 3388 888 S 0.0 0.2 0:00.00 (sd-pam) 843 root 20 0 14632 10364 7844 S 0.0 0.5 0:00.45 sshd 848 root 20 0 6324 4588 4072 S 0.0 0.2 0:00.00 sftp-server 849 root 20 0 13940 4564 3944 S 0.0 0.2 0:00.10 bash 2718 systemd+ 20 0 24544 12268 8340 S 0.0 0.6 0:00.06 systemd-resolve 2722 root 20 0 35516 12068 11184 S 0.0 0.6 0:00.04 systemd-journal 3565 mysql 20 0  85092 19028 S 0.0 4.1 0:01.37 mysqld 3722 root 20 0 14116 3660 3120 R 0.0 0.2 0:00.00 top 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

2.检查系统是否运行某服务


root@node3:~# ps -ef |grep mysql mysql 3565 1 0 15:05 ? 00:00:01 /usr/sbin/mysqld root 3724 849 0 15:38 pts/1 00:00:00 grep --color=auto mysql root@node3:~# 
  • 1.
  • 2.
  • 3.
  • 4.

3.检查swap


root@node3:~# free -m 总计 已用 空闲 共享 缓冲/缓存 可用 内存: 2048 90 1668 0 288 1957 交换: 0 0 0 
  • 1.
  • 2.
  • 3.
  • 4.

4.检查内核参数

①列出当前生效的内核参数

sysctl -a 
  • 1.

②过滤内核中路由转发参数

root@node3:~# sysctl -a |grep net.ipv4.ip_forward net.ipv4.ip_forward = 1 net.ipv4.ip_forward_update_priority = 1 net.ipv4.ip_forward_use_pmtu = 0 
  • 1.
  • 2.
  • 3.
  • 4.

③临时修改内核参数

sysctl -w net.ipv4.ip_forward=1 
  • 1.

④让内核参数配置生效

root@node3:~# sysctl -p root@node3:~# 
  • 1.
  • 2.