CentOS 8 安装笔记 - sysin | SYStem INside | 数据中心系统内幕
初始版本:CentOS 8.0.1905
继 RHEL 8 发布之后,CentOS 社区也发布了让人期待已久的 CentOS 8,并发布了两种模式:
- CentOS stream:滚动发布的 Linux 发行版,适用于需要频繁更新的开发者
- CentOS:类似 RHEL 8 的稳定操作系统,系统管理员可以用其部署或配置服务和应用
CentOS 8 系列只有 64 位系统,没有 32 位。
1 2 3 4 5 6 | CentOS-8-x86_64-1905-boot.iso 16-Aug-2019 05:22 534M CentOS-8-x86_64-1905-boot.iso.manifest 16-Aug-2019 05:23 626 CentOS-8-x86_64-1905-boot.torrent 24-Sep-2019 04:51 21K CentOS-8-x86_64-1905-dvd1.iso 16-Aug-2019 05:59 7G CentOS-8-x86_64-1905-dvd1.iso.manifest 16-Aug-2019 05:59 401K CentOS-8-x86_64-1905-dvd1.torrent 24-Sep-2019 04:52 266K |
这里使用 CentOS-8-x86_64-1905-boot.iso 进行安装,由于没有提供类似于 CentOS7 的 Minimal 版本,安装过程中选择 Minimal 安装,不过需要指定 repo 地址。
更新:CentOS 8.2.2004 终于增加了 minimal iso。
CentOS 8 的新特性
-
DNF 成为了默认的软件包管理器,同时 yum 仍然是可用的
-
使用网络管理器(
nmcli
和nmtui
)进行网络配置,移除了网络脚本 -
使用 Podman 进行容器管理
-
引入了两个新的包仓库:BaseOS 和 AppStream
-
使用 Cockpit 作为默认的系统管理工具
-
默认使用 Wayland 作为显示服务器
-
iptables
将被nftables
取代 -
使用 Linux 内核 4.18
-
动态编程语言、Web 和数据库服务器
Python 3.6
是默认的 Python 环境,有限支持 Python 2.7Node.js
是在 RHEL 最新包含的,其他动态语言更新包括:PHP 7.2
,Ruby 2.5
,Perl 5.26
,SWIG 3.0
- RHEL 8 提供的数据库服务包括:
MariaDB 10.3
,MySQL 8.0
,PostgreSQL 10
,PostgreSQL 9.6
, 和Redis 5
- RHEL 8 提供
Apache HTTP Server 2.4
以及首次引入的,nginx 1.14
Squid
版本升级到 4.4 ,同时也首次提供Varnish Cache 6.0
安装要点
具体参看 CentOS 8 安装截图,这里列出几个注意点
-
先配置网络,否则 NTP 无法配置,boot.iso 版本需要指定安装源也需要 Internet 访问
-
指定安装源,这里可以使用 163 或者阿里云镜像
系统配置
1. 格式化网卡命名
即禁用 consistent interface device naming
要点:与 CentOS 7 配置上略有不同。
- CentOS 7:net.ifnames=0 biosdevname=0
- CentOS 8:net.ifnames=0
1 2 | [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160 #注意这里的名称根据硬件有所变动,本例为运行在 VMware ESXi 中的虚拟机,网卡默认名称为 ens160,如果你希望继续使用 eth0 这样的传统名称,那么在安装启动时加上参数:net.ifnames=0 |
这种变化的原因,从 CentOS 7 开始,是由于 systemd 和 udev 引入了一种新的网络设备命名方式 – 一致网络设备命名(CONSISTENT NETWORK DEVICE NAMING)。可以根据固件、拓扑、位置信息来设置固定名字,带来的好处是命名自动化,名字完全可预测,在硬件坏了以后更换也不会影响设备的命名,这样可以让硬件的更换无缝化。带来的不利是新的设备名称比传统的名称难以阅读。比如新的名称是 enp5s0.
1.1 编辑 grub 配置文件
1 2 3 4 5 6 7 8 9 10 11 | #如果系统已经安装,希望改成 eth0 这样的名称,那么需要: #修改 grub2 启动参数 vi /etc/default/grub #也有文章修改 /etc/sysconfig/grub,该文件是 /etc/default/grub 的链接 #增加内容:net.ifnames=0 原内容: GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet" 修改后 (位置并没有严格要求): GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap net.ifnames=0 rhgb quiet" #保存 :x |
1 2 3 4 5 | # 直接用 sed 命令替换 # CentOS 8 sed -i 's/rhgb/net.ifnames=0 rhgb/' /etc/default/grub # CentOS 7 #sed -i 's/rhgb/net.ifnames=0 biosdevname=0 rhgb/' /etc/default/grub |
rhgb 表示 redhat graphics boot,就是会看到图片来代替启动过程中显示的文本信息,这些信息在启动后用 dmesg 也可以看到
quiet 表示在启动过程中只有重要信息显示,类似硬件自检的消息不回显示
rhgb = redhat graphical boot - This is a GUI mode booting screen with most of the information hidden while the user sees a rotating activity icon spining and brief information as to what the computer is doing.
quiet = hides the majority of boot messages before rhgb starts. These are supposed to make the common user more comfortable. They get alarmed about seeing the kernel and initializing messages, so they hide them for their comfort.
1.2 运行如下命令重建 grub.cfg 文件
1 2 3 4 5 | # legacy boot mode: grub2-mkconfig -o /boot/grub2/grub.cfg # UEFI boot mode: grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg |
1.3 重命名网卡配置文件
1 2 3 4 5 6 7 | #重新对文件进行命名: cd /etc/sysconfig/network-scripts/ mv ifcfg-ens160 ifcfg-eth0 vi ifcfg-eth0 编辑 NAME=eth0 编辑 DEVICE=eth0 注释 HWADDR,如果有 |
1.4 重启生效
1 | reboot |
参考:官方文档
2. 配置网络
2.1 方法一:手工配置 ifcfg,使用 nmcli 来生效新的网络配置
安装过程中最好配置好网络,如果需要编辑网络,修改配置文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none #static,使用静态 IP 配置(CentOS 8 这里是 none 也是静态) DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 UUID=34f808f1-1232-4900-83db-82d32533f776 DEVICE=eth0 ONBOOT=yes #开机自动启用网络连接 IPADDR=10.3.5.5 #IP 地址 PREFIX=24 #掩码 GATEWAY=10.3.5.1 #默认网关 DNS1=10.3.5.11 #DNS 服务器 DNS2=10.3.5.12 #备用 NDS 服务器 DOMAIN=sysin.org #域名 IPV6_PRIVACY=no |
1 2 3 4 5 | nmcli c reload #重新加载网络配置 ping www.baidu.com #测试网络是否正常 ip addr #查看 IP 地址 |
重启网络
比如配置了静态路由,使用
nmcli c reload
无法生效,需要重启网络
1 2 | systemctl restart NetworkManager.service nmcli networking off && nmcli networking on |
1 2 3 4 5 6 7 | nmcli n #查看 nmcli 状态 nmcli n on #启动 nmcli nmcli c up eth0 #启动网卡 eth0 nmcli c down eth0 #关闭网卡 eth0 nmcli d c eth0 #激活网卡 nmcli d show eth0 #查看网卡 eth0 信息 nmcli r all off #关闭无线 |
2.2 方法二:RHEL8/CentOS8 完全使用 nmcli 来管理网络
nmcli 命令帮助:
命令不支持自动补全,但是可以通过 - h 参数逐步获得帮助
1 2 3 4 5 6 7 | ## 说明 nmcli 后面的命令关键字都可以用第一个字母简写来标识,例如: ## nmcli connection = nmcli c ## nmcli connection show --active = nmcli c s --a nmcli -h nmcli connection -h nmcli connection add -h nmcli connection modify -h |
创建一个完整的配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 | IFACE=`nmcli dev|grep ethernet|awk '{print $1}'` nmcli con delete $IFACE # 下面将使用传统的 eth0 命名方式,前提是已经做了格式化网卡名称的配置 nmcli con add con-name eth0 ifname eth0 type ethernet autoconnect yes nmcli connection modify eth0 ipv4.method manual \ ipv4.addresses 10.3.5.5/24 \ ipv4.dns "10.3.5.11, 10.3.5.12" \ ipv4.gateway 10.3.5.1 \ ipv4.dns-search sysin.org nmcli c up eth0 nmcli c reload |
可以配置的参数选项:
ipv4.[method, dns, dns-search, dns-options, dns-priority, addresses, gateway, routes, route-metric, route-table, ignore-auto-routes, ignore-auto-dns, dhcp-client-id, dhcp-timeout, dhcp-send-hostname, dhcp-hostname, dhcp-fqdn, never-default, may-fail, dad-timeout]
nmcli 示例命令参考:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | # 查看网卡信息 nmcli connection NAME UUID TYPE DEVICE eth0 db05ccae-3a48-4300-b3a6-7c56429c4f54 ethernet eth0 # 显示具体的网络接口信息 nmcli connection show eth0 # 显示所有活动连接 nmcli connection show --active # 删除一个网卡连接 nmcli connection delete eth0 # 创建一个网卡连接 IFACE=`nmcli dev|grep ethernet|awk '{print $1}'` nmcli con delete $IFACE nmcli con add con-name "$IFACE" ifname "$IFACE" type ethernet autoconnect yes # 给 eth0 添加一个 IP 和子网掩码(NETMASK) nmcli connection modify eth0 ipv4.addresses 10.3.5.5/24 # 给 eth0 添加两个 IP 地址和掩码 nmcli connection modify eth0 ipv4.addresses "10.3.5.5/24, 10.3.5.6/24" # IP 获取方式设置成手动(BOOTPROTO=static/none) nmcli connection modify eth0 ipv4.method manual # 添加一个 ipv4 nmcli connection modify eth0 +ipv4.addresses 10.3.5.6/24 # 删除一个 ipv4 nmcli connection modify eth0 -ipv4.addresses 10.3.5.6/24 # 添加 DNS nmcli connection modify eth0 ipv4.dns 10.3.5.11 # 同时添加两个 DNS nmcli connection modify eth0 ipv4.dns "10.3.5.11, 10.3.5.12" # 删除 DNS nmcli connection modify eth0 -ipv4.dns 10.3.5.11 # 删除第一个 DNS nmcli connection modify eth0 -ipv4.dns 1 # 添加一个网关(GATEWAY) nmcli connection modify eth0 ipv4.gateway 10.3.5.1 # 都可以同时写,例如: nmcli connection modify eth0 ipv4.dns 10.3.5.11 ipv4.gateway 10.3.5.1 # 域名 dns-search,对应 ifcfg 中的 DOMAIN nmcli connection modify eth0 ipv4.dns-search sysin.org # 使用 nmcli 重新回载网络配置 nmcli c reload # 如果之前没有 eth0 的 connection,则上一步 reload 后就已经自动生效了 nmcli c up eth0 |
2.3 方法三:手工配置 ifcfg,安装 network.service 服务
可以通过 yum install network-scripts
来安装传统的 network.service,不过 redhat 说了,在下一个 RHEL 的大版本里将彻底废除,因此不建议使用 network.service。
1 2 3 | yum install network-scripts service network restart #重启网络服务 systemctl restart network.service #重启网络服务 |
3. 修改主机名
说明:与 CentOS 7 方法相同
- 即时生效
1 | hostname sysin #设置主机名为 sysin |
- 永久生效
1 2 3 4 5 6 7 8 9 10 | vi /etc/hostname #编辑配置文件 sysin #修改 localhost.localdomain 为 sysin :x #保存退出 或者使用命令:`hostnamectl set-hostname sysin` 同时修改 hosts 文件: vi /etc/hosts #编辑配置文件 127.0.0.1 sysin #增加一条 :x #保存退出 |
通过命令快速修改示例
1 2 3 4 5 6 7 8 9 10 | hostnamectl set-hostname sysin # 主机名这里是 sysin,替换实际名称直接执行 NICName=`ip add|egrep global|awk '{print $NF}'|head -n 1` IP=`ip add|grep global|awk -F'[/]+' '{ print $3 }'|head -n 1` Hostname=`hostname` HostnameAll=`hostname --fqdn` #注意这里 `` 不是引号 echo "网卡名称:$NICName" echo "IP 地址: $IP" echo "主机名称: $Hostname $HostnameAll" echo "$IP $Hostname $HostnameAll">>/etc/hosts |
4. 激活 cockpit web console
CentOS 8 默认集成了 cockpit,登录画面提示激活方法(模板未启用):
1 | systemctl enable --now cockpit.socket |
然后通过浏览器访问:http://<IP>:9090
补充说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Cockpit(飞机驾驶舱)使用方法 在 web 浏览器中查看服务器并使用鼠标执行系统任务。很容易管理存储、配置网络和检查日志等操作。 1) RHEL 8 自动安装了 Cockpit,Cockpit 所需的防火墙端口会自动打开 2) Cockpit 界面可用于将基于策略的解密(PBD)规则应用于受管系统上的磁盘 3) 对于在身份管理(IdM)域中注册的系统,Cockpit 默认使用域的集中管理的 IdM 资源 4) Cockpit 菜单和页面可以在移劢浏览器上导航 5) 可以从 Cockpit Web 界面创建和管理虚拟机 6) 现在可以将 “虚拟机” 页面添加到 Cockpit 界面,该界面使用户可以创建和管理基于 libvirt 的虚拟机 安装 cockpit `yum -y install cockpit` 启用 cockpit `systemctl enable --now cockpit.socket` 设置开机自启动同时开启服务一条命令就可以搞定 使用 Cockpit Cockpit 监听 9090 端口 使用浏览器访问 `http://<IP>:9090` |
5. 关闭 SELinux
SELinux 是 Linux 中最古老的一种 MAC(强制访问控制,Mandatory Access Control)模型,它是美国国家安全局的产品。除了在一些对安全有强制的要求的行业,SELinux 更多的以难以管理和排错而 “闻名”,通常认为其 “弊大于利”,许多应用安装文档中都要求关闭 SELinux。
编辑配置文件 /etc/selinux/config
:
1 2 3 4 5 | vi /etc/selinux/config # SELINUX=enforcing #注释掉 # SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加 :x #保存,关闭 |
或者直接使用 sed 命令修改:
1 2 3 4 5 | # 修改 SELINUX=enforcing 为 SELINUX=disabled,即替换行 sed -i '/^SELINUX=.*/c SELINUX=disabled' /etc/selinux/config # 或者:sed -i '/^SELINUX=enforcing/c SELINUX=disabled' /etc/selinux/config # 注释 SELINUXTYPE=targeted sed -i 's/^SELINUXTYPE=targeted/#&/' /etc/selinux/config |
使配置生效:
1 2 3 4 5 6 7 | setenforce 0 #0=permissive, SELinux prints warnings instead of enforcing. shutdown -r now #最终重启系统生效 sestatus #查看状态 sestatus -v #Verbose 详细输出 # 或者 getenforce |
6. 配置 firewalld
关闭 firewalld(模板配置)
CentOS 8 主要改动和 RedHat Enterprise Linux 8 是一致的,基于 Fedora 28 和内核版本 4.18
,其中网络方面的主要改动是用 nftables 框架替代 iptables 框架作为默认的网络包过滤工具。
1 2 3 | systemctl stop firewalld.service #停止 firewall systemctl disable firewalld.service #禁止 firewall 开机启动 firewall-cmd --state #查看状态 |
firewalld 常用命令(nftables 使用 nft 命令,参看其他文档)
CentOS 8 中 firewalld 已经与 iptables 解绑,后端改用 nftables,需要用 nft
或者 firewall-cmd
开放端口或者服务。
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 26 27 28 29 30 31 32 | # 查看配置 firewall-cmd --list-all firewall-cmd --list-services #默认开放:ssh dhcpv6-client firewall-cmd --zone=public --list-services #指定区域进行查看 firewall-cmd --list-ports firewall-cmd --zone=public --list-ports #指定区域进行查看 # 查看配置保存文件 cat /etc/firewalld/zones/public.xml # 添加一个 TCP 端口 (删除将 add 关键字修改为 remove) firewall-cmd --zone=public --add-port=80/tcp --permanent #--permanent 表示永久生效 firewall-cmd --add-port=80/tcp --permanent #与上面是等价的,默认 zone 为 pulic firewall-cmd --reload #重新加载配置生效 # 关于 zone firewall-cmd --get-zones #查看所有 zone 的命令,CentOS 7 一共有 9 个 zone block dmz drop external home internal public trusted work firewall-cmd --get-zones ##CentOS 8 有 10 个 zone block dmz drop external home internal libvirt public trusted work firewall-cmd --get-default-zone #查看默认的 zone 的命令 public # 添加一个服务 firewall-cmd --add-service=snmp --permanent firewall-cmd --reload firewall-cmd --get-services #查看可用的服务 # 限定源地址访问 firewall-cmd --add-rich-rule="rule family="ipv4"source address="192.168.1.0/24"port protocol="tcp"port="3306"accept" --permanent firewall-cmd --reload |
添加几个常用的服务:
1 2 3 4 | firewall-cmd --add-service=snmp --permanent firewall-cmd --add-service=http --permanent firewall-cmd --add-service=https --permanent firewall-cmd --reload |
禁 Ping:
1 2 | firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop' #全部禁 ping firewall-cmd --permanent --add-rich-rule='rule family="ipv4"source address="192.168.1.0/24"protocol value="icmp"accept' #指定 192.168.1.0/24 允许 icmp |
7. 安装 EPEL (Extra Packages for Enterprise Linux)
1 2 3 4 5 | yum -y install epel-release 或者 (EL8): rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8 |
在 Anolis OS 中 没有 epel。Rocky Linux 中正常有。
8. 安装一些必备工具
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | ## 一些基本工具最小化安装可能没有 (经测 vim 和 wget 等没有安装) yum -y install vim wget zip unzip # 开发工具 # 系统安装过程中建议勾选 "Development Tools" 组件 # 但是下面的命令将安装更多的软件包(比如 gcc、git 等) # 可以通过命令 `yum groupinfo "Development Tools"` 来查看详细的软件包列表 yum groupinstall "Development Tools" # 以下网络工具已经不存在 # yum -y install setuptool system-config-network-tui system-config-firewall-tui # 默认安装没有 ifconfig 命令,安装 net-tools # ifconfig、netstat、route 等命令集 yum -y install net-tools # host、dig 和 nslookup yum -y install bind-utils # 更好的 top 工具 yum -y install htop #Anolis OS 中没有 #https://github.com/hishamhm/htop # 文件传输:sz 和 rz yum -y install lrzsz # 查看日志神器 Log file Navigator yum -y install lnav # 该软件现在已经包含在 EPEL 中 # 初期 EPEL 中没有,直接在线安装 #rpm -ivh https://github.com/tstack/lnav/releases/download/v0.9.0/lnav-0.9.0-1.x86_64.rpm #Anolis OS 中没有 # NTP 已经废弃,改用 chrony # yum -y install ntp # nc: yum -y install nc # lsof: yum -y install lsof # tree: yum -y install tree # pstree: yum -y install psmisc |
fd 命令(fd-find,强烈推荐)
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 下载(二选一): # gcc 编译版本 wget https://github.com/sharkdp/fd/releases/download/v8.0.0/fd-v8.0.0-x86_64-unknown-linux-gnu.tar.gz # musl libc 编译版本 (推荐) wget https://github.com/sharkdp/fd/releases/download/v8.0.0/fd-v8.0.0-x86_64-unknown-linux-musl.tar.gz # 安装: tar -zxvf fd-v8.0.0-x86_64-unknown-linux-*.tar.gz cd fd-v8.0.0-x86_64-unknown-linux-musl cp ./fd /usr/local/bin/ cp ./fd.1 /usr/local/share/man/man1/ mandb |
9. 配置 NTP
在 CentOS 8.0 中默认不再支持 ntp 软件包,时间同步将由 chrony 来实现。
1 2 3 4 5 6 | cat /etc/chrony.conf # These servers were defined in the installation: pool 2.centos.pool.ntp.org iburst pool 0.pool.ntp.org iburst pool ntp1.aliyun.com iburst pool ntp2.aliyun.com iburst |
10. 安装 SNMP
1 2 3 4 | yum -y install net-snmp net-snmp-devel net-snmp-libs net-snmp-utils # 对比 CentOS 7 net-snmp-perl 已经不存在 (未知) # 因为是模板,这里暂不配置 |
11. 虚机安装 VM-tools
在系统安装时候勾选了 “Guest Agent”,将自动安装 open-vm-tools
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 手动安装 open-vm-tools: # http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2073803 yum -y install open-vm-tools # 开启服务 #chkconfig vmtoolsd on systemctl enable vmtoolsd.service # 启动服务 #service vmtoolsd start systemctl start vmtoolsd # 查看版本 vmtoolsd -v #现在已经 11.0 |
12. 用 yum 排除不需要的更新包
1 2 3 4 5 6 7 8 9 10 11 | # 修改 yum 的配置文件 vi /etc/yum.conf # 在 [main] 的最后添加 exclude=kernel* # 示例: [main] gpgcheck=1 installonly_limit=3 clean_requirements_on_remove=True best=True exclude=kernel* |
说明:
通常在用 yum 进行更新时老是会更新内核,这样就造成了两个问题:
1). 内核过多,占用系统的空间
2). 如果是自行编译安装的 nvidia 显卡驱动的话,进入更新的核心时需要再重新安装显卡驱动
这里介绍好的办法解决这个问题,就是在更新时排除对内核的更新,方法有两个:
1). 修改 yum 的配置文件 vi /etc/yum.conf,在 [main] 的最后添加 exclude=kernel*
2). 直接在 yum 的命令行执行如下的命令: yum –exclude=kernel* update
以上的办法也适合你要求自行排除的其它更新软件包。
1 | 备注:安装某些软件包需要新版内核支持,需要删除以上配置。 |
13. 更换国内镜像 yum 源
说明:与 CentOS 7 具体不太一样
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | -rw-r--r--. 1 root root 719 Nov 10 2020 CentOS-Linux-AppStream.repo -rw-r--r--. 1 root root 704 Nov 10 2020 CentOS-Linux-BaseOS.repo -rw-r--r--. 1 root root 1130 Nov 10 2020 CentOS-Linux-ContinuousRelease.repo -rw-r--r--. 1 root root 318 Nov 10 2020 CentOS-Linux-Debuginfo.repo -rw-r--r--. 1 root root 732 Nov 10 2020 CentOS-Linux-Devel.repo -rw-r--r--. 1 root root 704 Nov 10 2020 CentOS-Linux-Extras.repo -rw-r--r--. 1 root root 719 Nov 10 2020 CentOS-Linux-FastTrack.repo -rw-r--r--. 1 root root 740 Nov 10 2020 CentOS-Linux-HighAvailability.repo -rw-r--r--. 1 root root 693 Nov 10 2020 CentOS-Linux-Media.repo -rw-r--r--. 1 root root 706 Nov 10 2020 CentOS-Linux-Plus.repo -rw-r--r--. 1 root root 724 Nov 10 2020 CentOS-Linux-PowerTools.repo -rw-r--r--. 1 root root 898 Nov 10 2020 CentOS-Linux-Sources.repo -rw-r--r--. 1 root root 1167 Dec 19 2019 epel-modular.repo -rw-r--r--. 1 root root 1249 Dec 19 2019 epel-playground.repo -rw-r--r--. 1 root root 1104 Dec 19 2019 epel.repo -rw-r--r--. 1 root root 1266 Dec 19 2019 epel-testing-modular.repo -rw-r--r--. 1 root root 1203 Dec 19 2019 epel-testing.repocd /etc/yum.repos.d dnf repolist #只需要修改以下 4 个启用的 repo repo id repo name appstream CentOS Linux 8 - AppStream baseos CentOS Linux 8 - BaseOS epel Extra Packages for Enterprise Linux 8 - x86_64 epel-modular Extra Packages for Enterprise Linux Modular 8 - x86_64 extras CentOS Linux 8 - Extras #替换 sed -e 's|^mirrorlist=|#mirrorlist=|g' \ -e 's/#baseurl=/baseurl=/g' \ -e 's/http:\/\/mirror.centos.org/https:\/\/mirrors.aliyun.com/g' \ -i.bak \ /etc/yum.repos.d/CentOS-*.repo #修改 epel(url 不一样) sed -e 's/metalink=/#metalink=/g' \ -e 's/#baseurl=/baseurl=/g' \ -e 's/https:\/\/download.fedoraproject.org\/pub/https:\/\/mirrors.aliyun.com/g' \ -i.bak \ /etc/yum.repos.d/epel*.repo |
以下是 CentOS 7 的修改方法,备忘:
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 26 27 28 29 30 31 | # 推荐使用 163 镜像(Base 使用 163,epel 使用 ustc) # http://mirrors.163.com/.help/centos.html mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup cp /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup sudo sed -e 's!^mirrorlist=!#mirrorlist=!g' \ -e 's!^#baseurl=!baseurl=!g' \ -e 's!//download\.fedoraproject\.org/pub!//mirrors.ustc.edu.cn!g' \ -e 's!http://mirrors\.ustc!https://mirrors.ustc!g' \ -i /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo yum clean all yum makecache # 也可以使用阿里云镜像 # http://mirrors.aliyun.com/repo/ 第一步:备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup 第二步:下载 CentOS 7 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo wget -O /etc/yum.repos.d/epel-testing.repo http://mirrors.aliyun.com/repo/epel-testing.repo 第三步:运行 yum makecache 生成缓存 yum clean all rm -rf /var/cache/yum yum makecache |
禁用 fastestmirror 插件(CentOS 8 默认没有该配置文件了,可以忽略)
1 2 3 4 5 6 | # sed -i '/^enabled=.*/c enabled=0' /etc/yum/pluginconf.d/fastestmirror.conf sed -i '/^enabled=1/c enabled=0' /etc/yum/pluginconf.d/fastestmirror.conf # 写入文件需要添加 -i 参数 # ^ 表示一行的开头 # . 匹配一个非换行符的任意字符 # * 匹配 0 个或多个字符 |
14. 更新软件包
1 2 3 4 | dnf mackecache #缓存软件包信息 dnf upgrade #更新软件包 dnf clean all #rm -rf /var/cache/dnf |
15. 安装图形界面
基于特定场景需要,一般不用安装
1 2 3 4 5 6 | yum grouplist #查看可安装的组件 yum groupinstall "Server with GUI" #安装 GNOME 图形界面 (与 CentOS 7 名称不一样) yum groupinstall "Graphical Administration Tools" #将自动安装 startx #启动图形界面 |
实例:
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 26 | [root@localhost ~]# yum grouplist Last metadata expiration check: 1:12:58 ago on Sat 09 Nov 2019 03:59:09 PM CST. Available Environment Groups: Server with GUI Server Workstation KDE Plasma Workspaces Virtualization Host Custom Operating System Installed Environment Groups: Minimal Install Installed Groups: Development Tools Available Groups: Container Management .NET Core Development RPM Development Tools Smart Card Support Graphical Administration Tools Headless Management Legacy UNIX Compatibility Network Servers Scientific Support Security Tools System Tools Fedora Packager |
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 26 | [root@localhost ~]# yum groupinstall "Server with GUI" Installing Environment Groups: Server with GUI Installing Groups: Container Management Core Fonts GNOME Guest Desktop Agents Hardware Monitoring Utilities Hardware Support Headless Management Internet Browser Multimedia Common NetworkManager submodules Printing Client Server product core Standard Transaction Summary ============================================================================================================== Install 744 Packages Total download size: 696 M Installed size: 2.1 G Is this ok [y/N]: |
16. Shell 配置
命令自动补全忽略大小写
1 | echo 'set completion-ignore-case on' >> ~/.inputrc |
修改 ll 命令
Linux 中默认定义了 ll 别名,但参数比较少,需要使用更加强大的 ll 别名。
写入环境变量(当前用户优先执行):
zsh:
1 | echo 'alias ll="ls -lahF --color=auto --time-style=long-iso"' >> ~/.zshrc |
bash:
1 | echo 'alias ll="ls -lahF --color=auto --time-style=long-iso"' >> ~/.bashrc |
注意:仅仅针对当前用户,写入全局用户配置文件:/etc/skel/.bashrc 或者 /etc/skel/.zshrc
高级版 ls:以数字显示权限
这里我们把命令叫做 lll
命令:
1 | ls -lahF --color=auto --time-style=long-iso | awk '{k=0;s=0;for(i=0;i<=8;i++){k+=((substr($1,i+2,1)~/[rwxst]/)*2^(8-i))}j=4;for(i=4;i<=10;i+=3){s+=((substr($1,i,1)~/[stST]/)*j);j/=2}if(k){printf("%0o%0o ",s,k)}print}' |
创建文件
在使用 cat EOF 中出现 $ 变量通常会直接被执行,显示执行的结果。若想保持 $ 变量不变需要使用 \ 符进行注释。
1 2 3 4 5 6 7 8 | #如果非 root 用户,切换到 root sudo -i cat > /usr/local/bin/lll <<EOF #!/bin/bash ls -lahF --color=auto --time-style=long-iso | awk '{k=0;s=0;for(i=0;i<=8;i++){k+=((substr(\$1,i+2,1)~/[rwxst]/)*2^(8-i))}j=4;for(i=4;i<=10;i+=3){s+=((substr(\$1,i,1)~/[stST]/)*j);j/=2}if(k){printf("%0o%0o ",s,k)}print}' EOF #赋予执行权限: chmod +x /usr/local/bin/lll |
vi 和 vim
CentOS vi 和 vim 是不同的软件,默认安装的是 vi,vim 需要手动安装。
创建 vi 链接直接使用 vim:
1 | ln -s /usr/bin/vim /usr/local/bin/vi |
可以根据个人习惯来配置 vim,并用于全局配置。
1 2 3 4 | # 当面用户 ~/.vimrc # 全局新用户默认 /etc/skel/.vimrc |
17. Zsh
这里是针对当前用户配置。全局配置请参看其他文章,这里直接使用全局配置文件即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # Zsh yum -y install zsh # or #dnf -y install zsh yum -y install util-linux-user chsh -s /bin/zsh yum -y install git sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" sed -i '/^ZSH_THEME=.*/c ZSH_THEME="ys"' ~/.zshrc echo 'alias ll="ls -lahF --color=auto --time-style=long-iso"' >> ~/.zshrc git clone https://github.com/sangrealest/zsh-autosuggestions.git ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions sed -i '/^plugins=.*/c plugins=(git zsh-autosuggestions)' ~/.zshrc source ~/.zshrc |
18. 清理并制作镜像模板
清理 dnf/yum 缓存
1 2 | dnf autoremove dnf clean all |
清理临时文件夹
1 | rm -rf /tmp/* #清空临时文件夹 |
清空历史记录
1 2 3 4 5 6 7 8 9 10 11 12 13 | #比较完整的清空历史记录 rm -f /var/log/audit/audit* echo > /var/log/audit/audit.log rm -f /var/log/secure* echo > /var/log/secure rm -f /var/log/btmp* echo > /var/log/btmp rm -f /var/log/wtmp* echo > /var/log/wtmp echo > /var/log/lastlog echo > ~/.bash_history echo > ~/.zsh_history history -c |
至此,基本配置已经完成,可以制作模板了。