centos系统和Ubuntu系统命令区别以及常见操作
<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>一.前言</li><li>二.系统环境</li><li>三.命令区别<ul><li>3.1 使用习惯和命令区别</li><li>3.2 服务管理的区别</li><li>3.3 软件包信息区别</li></ul></li><li>四.Ubuntu系统常见操作<ul><li>4.1 Ubuntu系统apt和apt-get的区别</li><li>4.2 apt-get remove,apt-get purge,apt-get autoremove的区别</li><li>4.3 Ubuntu18防火墙<ul><li>4.3.1 Ubuntu防火墙UFW常见命令</li><li>4.3.2 UFW防火墙常用规则</li></ul></li><li>4.4 Ubuntu18配置网卡IP地址<ul><li>4.4.1 使用dhcp动态分配IP地址</li><li>4.4.2 给网卡配置静态IP地址</li></ul></li><li>4.5 Ubuntu18配置软件源</li></ul></li><li>五.Ubuntu系统troubleshooting<ul><li>5.1 xshell连接不上Ubuntu18解决方法</li></ul></li></ul></div><p></p><h1 id="一前言">一.前言</h1>
<p>CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代码所编译而成。由于出自同样的源代码,因此有些要求高度稳定性的服务器以CentOS替代商业版的Red Hat Enterprise Linux使用。</p>
<p>Ubuntu是一个以桌面应用为主的Linux操作系统。Ubuntu基于Debian发行版和GNOME桌面环境,与Debian的不同在于它每6个月会发布一个新版本。Ubuntu的目标在于为一般用户提供一个最新的、同时又相当稳定的主要由自由软件构建而成的操作系统。Ubuntu具有庞大的社区力量,用户可以方便地从社区获得帮助。</p>
<p>两者同为目前版本中个人和小团队常用的服务级操作系统,在线提供的软件库中可以很方便的安装到很多开源的软件及库。两者都使用bash作为基础shell,所以在很多基础命令上,ubuntu与centos的差别不是很明显,而ubuntu在桌面界面上要做的更为出色,很多人如果是从兴趣出发而学习linux的首选一般都是ubuntu,毕竟很多人还是很习惯在桌面图形下操作的。</p>
<p>总而言之,<strong>centos更加稳定,Ubuntu对新技术支持度更好</strong>。</p>
<p>因为我常用的是centos系统和Red Hat Enterprise Linux系统,本文着重讲解Ubuntu命令,centos/RHEL命令偏少!</p>
<h1 id="二系统环境">二.系统环境</h1>
<table>
<thead>
<tr>
<th>服务器版本</th>
<th>系统内核版本</th>
<th>CPU架构</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ubuntu 18.04.5 LTS</td>
<td>4.15.0-112-generic</td>
<td>x86_64</td>
</tr>
</tbody>
</table>
<h1 id="三命令区别">三.命令区别</h1>
<h2 id="31-使用习惯和命令区别">3.1 使用习惯和命令区别</h2>
<p>Centos与Ubuntu的使用习惯和命令上有很多的不同,下面简单列举一下:</p>
<ol>
<li>centos中新建的非root用户是没有sudo的权限的,如果需要使用sudo权限必须在/etc/sudoers 中加入账户和权限,所以切换到root账号的时候只需要输入su,加入root账号的密码即可;在Ubuntu中,一般使用sudo+命令,如果是第一次使用会提示输入当前用户的密码(而不是root的密码)。</li>
<li>在线安装软件中,centos使用的是yum命令,而ubuntu中使用的是apt-get命令(最新推荐的是apt命令)。除此之外yum中还有一个从软件源中搜索某个软件的方法yum search +软件名</li>
<li>centos是来自于redhat,所以centos支持rpm格式的安装,而ubuntu显然是不支持的。</li>
<li>毕竟是不同的公司做的不同的发行版,很多配置文件的位置和默认的文件路径都有很大区别,这个需要使用过程中慢慢体会了。</li>
</ol>
<h2 id="32-服务管理的区别">3.2 服务管理的区别</h2>
<p>命令区别总结如下表所示:</p>
<p><img src="https://typorablogimage.oss-cn-hangzhou.aliyuncs.com/image-20220628014134085.png"></p>
<h2 id="33-软件包信息区别">3.3 软件包信息区别</h2>
<p><img src="https://typorablogimage.oss-cn-hangzhou.aliyuncs.com/image-20220628014206348.png"></p>
<p>3.4 软件包文件信息区别</p>
<p>示例:Ubuntu系统搜索/sbin/lsmod是由哪个软件包安装的</p>
<pre><code class="language-shell">#kmod: /sbin/lsmod表示/sbin/lsmod由kmod软件包安装的
root@k8scludes3:~# dpkg -S /sbin/lsmod
kmod: /sbin/lsmod
</code></pre>
<p><img src="https://typorablogimage.oss-cn-hangzhou.aliyuncs.com/image-20220628014326590.png"></p>
<h1 id="四ubuntu系统常见操作">四.Ubuntu系统常见操作</h1>
<h2 id="41-ubuntu系统apt和apt-get的区别">4.1 Ubuntu系统apt和apt-get的区别</h2>
<ol>
<li>apt命令从Ubuntu 16.04“引入”,许多其他发行版遵循Ubuntu的脚步并开始鼓励用户使用apt而不是apt-get。作为普通的 Linux 用户,优先使用 apt,它是 Linux 发行版推荐的命令。它提供了包管理必要的选项,更重要的是便于记忆。</li>
<li>apt 是 apt-get 和 apt-cache 的子集,为包管理提供必要的命令。</li>
<li>虽然 apt-get 没有被弃用,但是作为普通 Linux 用户,推荐开始频繁的使用 apt。</li>
</ol>
<h2 id="42-apt-get-removeapt-get-purgeapt-get-autoremove的区别">4.2 apt-get remove,apt-get purge,apt-get autoremove的区别</h2>
<ol>
<li>apt-get remove :卸载软件包;</li>
<li>apt-get autoremove :会卸载所有自动安装且不再使用的软件包,一般用来清理依赖项(慎用);</li>
<li>apt-get purge:卸载并清除软件包的配置;</li>
</ol>
<h2 id="43-ubuntu18防火墙">4.3 Ubuntu18防火墙</h2>
<p>Ubuntu18默认使用UFW管理防火墙,即Uncomplicated Firewall,是基于iptables实现的防火墙管理工具,旨在简化配置防火墙的过程,所以实际上UFW修改的是iptables的规则。</p>
<h3 id="431-ubuntu防火墙ufw常见命令">4.3.1 Ubuntu防火墙UFW常见命令</h3>
<p>安装ufw</p>
<pre><code class="language-shell">#查看Ubuntu版本
root@localhost:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.5 LTS
Release: 18.04
Codename: bionic
#安装ufw
#apt-get install ufw
</code></pre>
<p>查看ufw版本</p>
<pre><code class="language-shell">root@localhost:~# ufw --version
ufw 0.36
Copyright 2008-2015 Canonical Ltd.
</code></pre>
<p>查看ufw是否被安装</p>
<pre><code class="language-shell">root@localhost:~# dpkg -l | grep ufw
iiufw 0.36-0ubuntu0.18.04.1 all program for managing a Netfilter firewall
</code></pre>
<p>查看防火墙状态</p>
<pre><code class="language-shell">#Status: inactive说明防火墙关闭
root@localhost:~# ufw status
Status: inactive
</code></pre>
<p>关闭防火墙</p>
<pre><code class="language-shell">root@localhost:~# ufw disable
Firewall stopped and disabled on system startup
root@localhost:~# ufw status
Status: inactive
</code></pre>
<p>启动防火墙</p>
<pre><code class="language-shell">root@localhost:~# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
#可以看到一条防火墙规则:允许外界访问本机的22端口
root@localhost:~# ufw status
Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
</code></pre>
<p>UFW重新加载配置文件</p>
<pre><code class="language-shell">root@localhost:~# ufw reload
</code></pre>
<p>重置UFW</p>
<pre><code class="language-shell">ufw reset
</code></pre>
<h3 id="432-ufw防火墙常用规则">4.3.2 UFW防火墙常用规则</h3>
<p>防火墙开启/禁用语法</p>
<pre><code class="language-shell">ufw allow|deny
</code></pre>
<p>默认情况下,UFW将阻止所有传入连接并允许所有出站连接。 这意味着任何试图访问您的服务器的用户都将无法连接,除非您专门打开该端口,而服务器上运行的所有应用程序和服务都将能够访问外部世界。<br>
默认策略在/etc/default/ufw文件中定义,可以使用sudo ufw default。</p>
<pre><code class="language-shell">#关闭所有外部对本机的访问,但本机访问外部正常
ufw default deny
</code></pre>
<p>打开或关闭某个端口</p>
<pre><code class="language-shell">ufw allow 8000:8100/tcp允许端口范围
ufw allow smtp 允许所有的外部IP访问本机的25/tcp (smtp)端口
ufw allow 22/tcp 允许所有的外部IP访问本机的22/tcp (ssh)端口
ufw allow 53 允许外部访问53端口(tcp/udp)
ufw allow from 192.168.1.100 允许此IP访问所有的本机端口
ufw deny proto tcp from 10.0.0.0/8 to 192.168.0.1 port 要拒绝所有的流量从TCP的10.0.0.0/8 到端口22的地址192.168.0.1
ufw deny smtp 禁止外部访问smtp服务
ufw delete allow smtp 删除上面建立的某条规则
</code></pre>
<p>查看端口状态</p>
<pre><code class="language-shell">root@localhost:~# lsof -i:22
COMMANDPID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 883 root 3uIPv426893 0t0TCP *:ssh (LISTEN)
sshd 883 root 4uIPv626895 0t0TCP *:ssh (LISTEN)
sshd 1744 root 3uIPv427581 0t0TCP linux:ssh->192.168.110.1:60711 (ESTABLISHED)
root@localhost:~# netstat -antup | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 883/sshd
tcp 0 36 192.168.110.128:22 192.168.110.1:60711 ESTABLISHED 1744/sshd: root@pts
tcp6 0 0 :::22 :::* LISTEN 883/sshd
</code></pre>
<p>列出服务器上可用的所有应用程序配置文件</p>
<pre><code class="language-shell">root@localhost:~# ufw app list
Available applications:
OpenSSH
</code></pre>
<h2 id="44-ubuntu18配置网卡ip地址">4.4 Ubuntu18配置网卡IP地址</h2>
<p>Ubuntu Server 18.04采用netplan作为网络配置管理工具。默认的网卡配置文件为01-netcfg.yaml</p>
<pre><code class="language-shell">#01-netcfg.yaml为网卡配置文件
root@localhost:~# ll /etc/netplan/
total 12
drwxr-xr-x2 root root 4096 Apr 14 15:59 ./
drwxr-xr-x 88 root root 4096 Nov 282020 ../
-rw-r--r--1 root root 84 Apr 14 15:59 01-netcfg.yaml
</code></pre>
<p>默认的网络配置信息如下:</p>
<pre><code class="language-shell">root@localhost:~# cat /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
ens32:
dhcp4: dhcp
</code></pre>
<h3 id="441-使用dhcp动态分配ip地址">4.4.1 使用dhcp动态分配IP地址</h3>
<p>如果要使用dhcp动态分配IP,则配置修改如下:</p>
<pre><code class="language-shell">root@localhost:~# cat /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
ens32:
dhcp4: yes
</code></pre>
<p>修改了网卡文件之后需要netplan apply 更新网卡配置</p>
<pre><code class="language-shell">netplan apply
</code></pre>
<p>查看IP地址,192.168.110.128就是dhcp服务器动态分配的IP地址</p>
<pre><code class="language-shell">root@localhost:~# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu 1500
inet 192.168.110.128netmask 255.255.255.0broadcast 192.168.110.255
inet6 fe80::20c:29ff:fe92:3462prefixlen 64scopeid 0x20<link>
ether 00:0c:29:92:34:62txqueuelen 1000(Ethernet)
RX packets 28639bytes 21935140 (21.9 MB)
RX errors 0dropped 0overruns 0frame 0
TX packets 16661bytes 1081505 (1.0 MB)
TX errors 0dropped 0 overruns 0carrier 0collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>mtu 65536
inet 127.0.0.1netmask 255.0.0.0
inet6 ::1prefixlen 128scopeid 0x10<host>
looptxqueuelen 1000(Local Loopback)
RX packets 59906bytes 4255443 (4.2 MB)
RX errors 0dropped 0overruns 0frame 0
TX packets 59906bytes 4255443 (4.2 MB)
TX errors 0dropped 0 overruns 0carrier 0collisions 0
</code></pre>
<p>查看用netplan配置的网卡信息</p>
<pre><code class="language-shell">root@localhost:~# networkctl status ens32
● 2: ens32
Link File: /lib/systemd/network/99-default.link
Network File: /run/systemd/network/10-netplan-ens32.network
Type: ether
State: routable (configured)
Path: pci-0000:02:00.0
Driver: e1000
Vendor: Intel Corporation
Model: 82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single Port Adapter)
HW Address: 00:0c:29:92:34:62 (VMware, Inc.)
Address: 192.168.110.128
fe80::20c:29ff:fe92:3462
Gateway: 192.168.110.2 (VMware, Inc.)
DNS: 192.168.110.2
Search Domains: localdomain
</code></pre>
<h3 id="442-给网卡配置静态ip地址">4.4.2 给网卡配置静态IP地址</h3>
<p>静态IP常用参数解释:</p>
<ul>
<li>renderer:指定后端网络服务,支持networkd(Systemd-networkd) 和 NetworkManager两种,默认是networkd。</li>
<li>ethernets:指定是以太网配置,其他的还包括 wifis 或者 bridges</li>
<li>ens32:网卡名称</li>
<li>dhcp4:开启使用ipv4的DHCP,默认是关闭。</li>
<li>dhcp6:开启使用ipv6的DHCP,默认是关闭。</li>
<li>addresses:对应网卡配置的静态ip地址,是ip/掩码的格式,支持ipv6地址,例如 addresses: </li>
<li>gateway4, gateway6:指定IPv4/6默认网关,使用静态ip配置时使用。例如IPv4: gateway4: 172.16.0.1 例如IPv6: gateway6: "2001:4::1"</li>
<li>nameservers:设置DNS服务器和搜索域。有两个受支持的字段:addresses:是DNS地址列表,search:是搜索域列表,没有特殊需要可以不配置search这项。</li>
</ul>
<p>静态IP配置如下</p>
<pre><code class="language-shell">root@localhost:~# cat /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
ens32:
dhcp4: no
addresses:
gateway4: 192.168.110.2
nameservers:
addresses:
</code></pre>
<p>修改了网卡文件之后需要netplan apply 更新网卡配置</p>
<pre><code class="language-shell">root@localhost:~# netplan apply
</code></pre>
<p>查看用netplan配置的网卡信息</p>
<pre><code class="language-shell">root@localhost:~# networkctl status ens32
● 2: ens32
Link File: /lib/systemd/network/99-default.link
Network File: /run/systemd/network/10-netplan-ens32.network
Type: ether
State: routable (configured)
Path: pci-0000:02:00.0
Driver: e1000
Vendor: Intel Corporation
Model: 82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single Port Adapter)
HW Address: 00:0c:29:92:34:62 (VMware, Inc.)
Address: 192.168.110.128
fe80::20c:29ff:fe92:3462
Gateway: 192.168.110.2 (VMware, Inc.)
DNS: 192.168.110.2
114.114.114.114
</code></pre>
<h2 id="45-ubuntu18配置软件源">4.5 Ubuntu18配置软件源</h2>
<p>先查看操作系统版本,我们的Ubuntu版本为18.04 bionic,要根据版本添加软件源</p>
<pre><code class="language-shell">root@localhost:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.5 LTS
Release: 18.04
Codename: bionic
</code></pre>
<p>添加阿里云软件源</p>
<pre><code class="language-shell">root@localhost:~# cat /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src 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-backports main restricted universe multiverse
#这三行是k8s源
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
deb https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable
# deb-src https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable
</code></pre>
<p>更新软件源</p>
<pre><code class="language-shell">root@localhost:~# apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease
Get:4 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial InRelease
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease
Hit:6 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease
Hit:7 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic InRelease
Ign:8 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
Get:8 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
Fetched 54.7 kB in 2s (33.3 kB/s)
Reading package lists... Done
</code></pre>
<h1 id="五ubuntu系统troubleshooting">五.Ubuntu系统troubleshooting</h1>
<h2 id="51-xshell连接不上ubuntu18解决方法">5.1 xshell连接不上Ubuntu18解决方法</h2>
<p>问题描述:</p>
<p> 安装了Ubuntu18系统之后,使用xshell连接不上Ubuntu,执行dpkg -l | grep ssh,发现有openssh-clientopenssh-server openssh-sftp-server ssh-import-id,防火墙也是关闭状态,但是xshell就是连接不上</p>
<p>解决方法:</p>
<p>使用apt-get update 更新软件包列表之后,使用apt-get install ssh安装ssh,注意在安装软件时不执行apt-get update 会报错</p>
<pre><code class="language-shell">apt-get update
apt-get install ssh
</code></pre>
<p>ssh安装之后,查看安装的相关ssh程序包</p>
<pre><code class="language-shell">root@localhost:~# dpkg -l | grep ssh
iiopenssh-client 1:7.6p1-4ubuntu0.7 amd64 secure shell (SSH) client, for secure access to remote machines
iiopenssh-server 1:7.6p1-4ubuntu0.7 amd64 secure shell (SSH) server, for secure access from remote machines
iiopenssh-sftp-server 1:7.6p1-4ubuntu0.7 amd64 secure shell (SSH) sftp server module, for SFTP access from remote machines
iissh 1:7.6p1-4ubuntu0.7 all secure shell client and server (metapackage)
iissh-import-id 5.7-0ubuntu1.1 all securely retrieve an SSH public key and install it locally
</code></pre>
<p>重启ssh</p>
<pre><code class="language-shell">root@localhost:~# /etc/init.d/ssh restart
[ ok ] Restarting ssh (via systemctl): ssh.service.
root@localhost:~# service ssh status
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-04-14 17:36:39 CST; 28s ago
Process: 14347 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 14348 (sshd)
Tasks: 1 (limit: 2940)
CGroup: /system.slice/ssh.service
└─14348 /usr/sbin/sshd -D
</code></pre>
<p>ssh配置文件</p>
<pre><code class="language-shell">root@localhost:~# ls /etc/ssh/sshd_config
/etc/ssh/sshd_config
</code></pre>
<p>此时,使用xshell就能连接Ubuntu18.04了</p>
<p>查看ssh端口</p>
<pre><code class="language-shell">root@localhost:~# netstat -antup | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 14348/sshd
tcp 0 36 192.168.110.128:22 192.168.110.1:55466 ESTABLISHED 14213/sshd: root@pt
tcp6 0 0 :::22 :::* LISTEN 14348/sshd
</code></pre>
</div>
<div id="MySignature" role="contentinfo">
致力于一条龙式的为您解决问题<br><br>
来源:https://www.cnblogs.com/renshengdezheli/p/16424694.html
頁:
[1]