2021年12月31日,CentOS 8停止维护,2022年1月31日,原CentOS 8 Yum源不再可用,已被移到https://vault.centos.org。 如你仍需要运行CentOS 8,可以使用vault.centos.org代替mirror.centos.org:

# sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
# sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*

另外,各大厂商的dnf/yum源也都按照官方要求移动了镜像。 重新安装阿里Yum源:

# wget https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo -O /etc/yum.repos.d/CentOS-Aliyun-Vault.repo

常用命令

查看系统信息

Linux版本 uname -a 查看系统信息(包括kernel name、hostname、kernel release、kernel version、operating system等) lsb_release -a 查看LSB(Linux Standard Base)和Distribution信息
cat /etc/system-release 查看Linux版本 cat /etc/redhat-release

CPU信息 lscpu 查看CPU架构信息 cat /proc/cpuinfo vmstat 查看进程、内存、swap、IO、system、CPU等信息 top 显示linux进程信息

内存信息 cat /proc/meminfo

硬盘信息 df -h 查看硬盘信息 df -i 查看inode信息

进程 ps -ef | grep java | grep -v grep 查看Java进程 kill -9 #### 杀死进程 ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9 杀死所有Java进程 netstat -tlnp | grep 8080 查看端口占用

env 显示当前用户的环境变量 tmpwatch -afv 5d /tmp 删除tmp目录5天未使用的文件 update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0/bin/java

NetworkManager(CentOS 8)

安装和启动 dnf install NetworkManager 安装NetworkManager systemctl start/stop NetworkManager 启动/停止NetworkManager systemctl status NetworkManager 查看网络状态 systemctl reload NetworkManager 重新加载NetworkManager配置

NetworkManager CLI nmcli general status 查看网络状态

nmcli device status 查看网络设备 nmcli device show 查看所有设备的详细信息 nmcli device show device_name 查看指定设备的详细信息

nmcli connection show 查看连接 nmcli connection show -a 查看所有活动连接 nmcli connection show connection_name 查看指定连接的详细信息 nmcli connection modify connection_name property value 修改连接属性 nmcli connection reload 重新加载所有连接 nmcli connection load /etc/sysconfig/network-scripts/ifcfg-connection_name 加载配置文件 nmcli connection up connection_name 激活连接 nmcli connection down connection_name 停止连接

禁用IPv6

# nmcli connection modify eno1 ipv6.method "disabled"
# nmcli connection up eno1

验证:

  1. 输入 ip address show 命令显示IP 设置,如果没有 inet6 条目,则在该设备中禁用 IPv6:
# ip address show eno1
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 52:54:00:6b:74:be brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.111/24 brd 192.168.1.255 scope global noprefixroute eno1
       valid_lft forever preferred_lft forever
  1. 验证 /proc/sys/net/ipv6/conf/eno1/disable_ipv6 文件现在包含 1值:
# cat /proc/sys/net/ipv6/conf/eno1/disable_ipv6
1

配置文件 /etc/sysconfig/ 存储配置文件和脚本,大多数网络配置信息都存储在此目录 /etc/sysconfig/network-scripts/ 其中的 ifcfg 文件保存接口信息,配置参数请查看man nm-settings-ifcfg-rh /etc/NetworkManager/system-connections/ 存储VPN、移动宽带和 PPPoE 连接信息

NetworkManager官网 RHEL 8 配置和管理网络

Firewalld

CentOS 8 默认安装firewalld防火墙,nftables取代iptables为其默认后端。firewalld 为使用 D-Bus 接口提供动态可定制防火墙服务,使用区(zone)和服务(service)的概念简化了流量管理。 注意,为避免彼此影响,firewalld、nftables、iptables仅可运行其中一个服务;nftables后端不支持使用firewall-cmd --direct选项传递自定义规则。

安装和状态检查 dnf install firewalld 安装firewalld systemctl enable firewalld 启用firewalld systemctl start/stop firewalld 启动/停止firewalld systemctl status firewalld 查看防火墙状态

firewall-cmd --state 查看防火墙状态 firewall-cmd --check-config 检查配置正确性

Runtime 和 Permanent配置 Firewalld分为运行时、持久化两套配置集。运行时配置在reboot时不会持久化,更改持久化配置不会应用于运行时,Firewalld启动后会加载持久化配置到运行时。

改变运行时配置然后持久化: firewall-cmd <options> firewall-cmd --runtime-to-permanent 持久化新设置

改变持久化配置然后重新加载: firewall-cmd --permanent <options> firewall-cmd --reload 重新加载防火墙,并不中断用户连接 firewall-cmd --complete-reload 重新加载防火墙并中断用户连接

紧急命令 firewall-cmd --panic-on 禁用网络,停止所有网络流量 firewall-cmd --panic-off 关闭 panic 模式,恢复防火墙到永久设置 firewall-cmd --query-panic 查看是否打开或关闭 panic 模式

Zone Zone用于预定义不同信任级别的网络规则集,给区分派网络接口(interface)和/或源(source)后,即成为活跃区。

预定义区存储在 /usr/lib/firewalld/zones/ 目录中,可立即应用于任何可用的网络接口。只有在修改后,这些文件才会复制到 /etc/firewalld/zones/ 目录中。预定义区如下:

说明
block 拒绝所有传入的网络连接,会发送IPv4 的 icmp-host-prohibited 和 IPv6 的 icmp6-adm-prohibited 的消息。只有从系统启动的网络连接才能进行。
dmz demilitarized zone,称为“隔离区”或“非军事化区”。区内计算机可以被公开访问,可以有限访问内部网络。只接受所选的入站连接。
drop 丢弃所有传入的数据包,没有任何通知。只有外发网络连接也是可行的。
external 用于启用了伪装的外部网络,特别是路由器。不信任网络中的其他计算机。只接受所选的入站连接。
home 用于家用,信任其他计算机。只接受所选的入站连接。
internal 信任网络中的其他计算机,供内部网络使用。只接受所选的入站连接。
public 用于不信任网络中其他计算机的公共区域。只接受所选的入站连接。
trusted 接受所有网络连接。
work 用于信任网络中其他计算机的工作区。只接受所选的入站连接。

初始默认区为public,所有网络接口都指派给默认区,所有区都没有设置源。firewalld启动后会加载默认区并设置为活跃的。使用firewall-cmd时,若未通过--zone=zone-name 选项指定zone,则为默认区。

# firewall-cmd --get-default-zone
public
# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eno1 eno2
  sources:
  services: cockpit dhcpv6-client ssh
  ports:
  protocols:
  forward: no
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

firewall-cmd --get-zones 查看所有可用Zone firewall-cmd --list-all-zones 查看所有Zone的详细信息 firewall-cmd --info-zone=zone-name 查看Zone的详细信息 firewall-cmd --zone=zone-name --list-all 查看Zone的详细信息 firewall-cmd --get-default-zone 查看default zone firewall-cmd --set-default-zone=zone-name 设置default zone,默认为public firewall-cmd --get-active-zones 查看活跃的zone firewall-cmd --get-zone-of-interface=eth0 查看指定interface所属zone

firewall-cmd --new-zone=zone-name 创建新Zone firewall-cmd --permanent --zone=zone-name --set-target=<default|ACCEPT|REJECT|DROP> 设置zone target zone target为传入流量定义zone的默认行为,有default、ACCEPT、REJECT、DROP四个可选项: ACCEPT 接受除特定规则禁用的数据包外的所有传入的数据包。 REJECT 或 DROP,禁用所有传入的数据包,除非在特定规则中允许的数据包。REJECT会通知源机器,但DROP时不会发送任何信息。

Interface 接口(Interface)是系统中硬件和虚拟适配器的名字,可以给区分配特定的网络接口来区分网络流量。如果活跃的interface未指定区,则属于默认区,也就是说所有活跃的interface都将分派到区。一个interface只能属于一个区,一个区可以含有多个interface。

在支持ifcfg文件的系统中,interface所属区存储在其中的 ZONE= 选项中。

使用firewall-cmd分配接口: firewall-cmd --zone=public --add-interface=eno2 --permanent 将网卡eno2添加到public zone firewall-cmd --zone=work --change-interface=eno2 --permanent 将网卡eno2更改到work zone firewall-cmd --zone=work --remove-interface=eno1 --permanent 从work zone中删除eno1

如有两个网卡eno1、eno2,zone分别设为public、work,这时查询active zone,结果如下:

firewall-cmd --get-active-zones
public
  interfaces: eno1
work
  interfaces: eno2

使用nmcli 为NetworkManager连接分配区:

nmcli connection modify eno1 connection.zone public
nmcli connection modify eno2 connection.zone work

查看连接详细信息:

nmcli connection show eno1
nmcli connection show eno2

Source 源(Source)是入站IP地址,可以为每个区指定不同的源,给定源的所有进入流量都会定向到它。即使只有一个网络接口,也可以使用多个区。如果添加多个带有重叠网络范围的区,则根据区名称排序,只考虑第一个区。

firewall-cmd --zone=zone-name --add-source=source 为zone设置源,源可以是 CIDR 格式的 IP 地址或 IP 掩码,如192.168.1.111、192.168.1.0/24 firewall-cmd --zone=zone-name --remove-source=source 删除源 firewall-cmd --zone=zone-name --list-sources 查看zone允许的源,相似命令list-services、list-ports、list-protocols等

一个请求使用哪个zone,firewalld依次按以下条件判断:

  1. 源(Source)地址
  2. 网络接口(interface)
  3. 默认zone

这三个的优先级依次降低,也就是说如果满足source条件就不会再去查找interface,如果前两个都不满足,才会使用默认zone。

比如,以Interface示例为基础,给work zone添加source,然后运行nft命令查看nftables chain,在其中您可以看到判断逻辑:

# firewall-cmd --zone=work --add-source=192.168.1.0/24 --permanent
# firewall-cmd --reload
# nft list table inet firewalld
...
        chain filter_INPUT_ZONES {
                ip saddr 192.168.1.0/24 goto filter_IN_work
                iifname "eno1" goto filter_IN_public
                iifname "eno2" goto filter_IN_work
                goto filter_IN_public
        }

Service Service预定义可应用于Zone的规则集,可包含本地端口、协议、源端口、目的地列表等,可添加描述信息,用途清晰、复用性好。

firewall-cmd --get-services 列出所有预定义的服务 firewall-cmd --info-service=service-name 查看服务信息 firewall-cmd --zone=zone-name --add-service=service-name 添加服务到zone firewall-cmd --zone=zone-name --add-service=ssh --timeout=5m 临时添加ssh服务,5分钟后恢复 firewall-cmd --zone=zone-name --remove-service=service-name 移除zone中的服务 firewall-cmd --zone=zone-name --list-services 查看zone的Service firewall-cmd --new-service=service-name 创建新服务 firewall-cmd --delete-service=service-name 删除服务 firewall-cmd --permanent --service=service --set-short=description 设置简短描述 firewall-cmd --permanent --service=service --set-description=description 设置描述 firewall-cmd --permanent --service=service --add-port=portid[-portid]/protocol 添加端口 firewall-cmd --permanent --service=service --add-protocol=protocol 添加协议 firewall-cmd --permanent --service=service --add-source-port=portid[-portid]/protocol 添加源端口 firewall-cmd --permanent --service=service --set-destination=ipv:address[/mask] 设置目的地

Port firewall-cmd --permanent --zone=public --add-port=8080/tcp 添加端口,端口类型可以是 tcp、udp、sctp 或 dccp firewall-cmd --permanent --zone=public --add-port=4990-4999/tcp 添加端口范围 firewall-cmd --permanent --remove-port=8080/tcp 删除端口 firewall-cmd --list-ports 查看zone允许的Port

Forward Port firewall-cmd --zone=external --add-masquerade 为转发port,首先要启用masquerade firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toport=8080 同一服务器,从80端口转发到8080端口 firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.8 转发到另一服务器的相同端口 firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.8 转发到另一服务器的不同端口 firewall-cmd --list-forward-ports 列出重定向端口 firewall-cmd --remove-forward-port=port=80:proto=tcp:toport=8080:toaddr= 删除重定向的端口

ICMP Internet Control Message Protocol (ICMP) 是一种支持协议,与 TCP 和 UDP 等传输协议不同,它不用于在系统之间交换数据,而是用于发送错误消息和表明连接问题的操作信息。 firewall-cmd --get-icmptypes 列出所有可用的 ICMP 类型 firewall-cmd --info-icmptype=icmptype 查看 ICMP 请求使用了哪些协议,IPv4、IPv6协议都可以使用 ICMP 请求 firewall-cmd --query-icmp-block=icmptype 查看 ICMP 请求是否被阻断 firewall-cmd --add-icmp-block=icmptype 阻止 ICMP 请求 firewall-cmd --remove-icmp-block=icmptype 取消阻止 ICMP 请求

IP Set firewall-cmd --get-ipset-types 查看支持的IP集类型 firewall-cmd --permanent --get-ipsets 列出IP集 firewall-cmd --permanent --info-ipset=test 查看IP集信息 firewall-cmd --permanent --new-ipset=test --type=hash:net 添加IPv4 hash:net 类型的新IP集,要创建用于 IPv6 的 IP 集,请添加 --option=family=inet6 选项 firewall-cmd --permanent --ipset=test --add-entry=192.168.0.1 给IP集中添加一个条目 firewall-cmd --permanent --ipset=test --get-entries 获取 IP 集当前条目列表 firewall-cmd --permanent --zone=drop --add-source=ipset:test 将 IP 集作为一个源添加到区

Debug 可以在/etc/sysconfig/firewalld或/usr/lib/systemd/system/firewalld.service文件内添加–debug选项启用debug。

sysconfig文件,添加--debug[=<level>]到FIREWALLD_ARGS:

# firewalld command line args
# possible values: --debug
FIREWALLD_ARGS=--debug=1

firewalld.service文件,添加--debug[=<level>]到ExecStart:

ExecStart=/usr/sbin/firewalld --nofork --nopid --debug=1 $FIREWALLD_ARGS
Debug level Output
1 Loading config files, D-Bus method calls, default level
2 + Backend calls, D-Bus Introspect method calls, access checks
3 + Rules that are added by the backends
4 + Transaction steps
10 + Introspection XML data

日志文件位置/var/log/firewalld。

示例 新建memcached zone:

# firewall-cmd --new-zone=memcached --permanent
# firewall-cmd --zone=memcached --add-port=11211/udp --permanent
# firewall-cmd --zone=memcached --add-port=11211/tcp --permanent
# firewall-cmd --zone=memcached --add-source=192.168.1.30/32 --permanent
# firewall-cmd --reload

新建nexus service:

# firewall-cmd --permanent --new-service=nexus
# firewall-cmd --permanent --service=nexus --set-short="nexus"
# firewall-cmd --permanent --service=nexus --set-description="Nexus Repository"
# firewall-cmd --permanent --service=nexus --add-port=8082/tcp
# firewall-cmd --permanent --add-service=nexus
# firewall-cmd --reload

删除Service的Port:

# firewall-cmd --permanent --service=nexus --remove-port=8082/tcp
# firewall-cmd --reload

启用public和internal两个zone,public zone仅允许http和https服务,internal zone仅允许内网访问,允许http、https、git、mysql、ssh服务:

# firewall-cmd --permanent --remove-service=cockpit
# firewall-cmd --permanent --remove-service=dhcpv6-client
# firewall-cmd --permanent --remove-service=ssh
# firewall-cmd --permanent --add-service=http
# firewall-cmd --permanent --add-service=https
# firewall-cmd --permanent --zone=internal --remove-service=cockpit
# firewall-cmd --permanent --zone=internal --remove-service=dhcpv6-client
# firewall-cmd --permanent --zone=internal --remove-service=mdns
# firewall-cmd --permanent --zone=internal --remove-service=samba-client
# firewall-cmd --permanent --zone=internal --add-service=http
# firewall-cmd --permanent --zone=internal --add-service=https
# firewall-cmd --permanent --zone=internal --add-service=git
# firewall-cmd --permanent --zone=internal --add-service=mysql
# firewall-cmd --reload

查看public和internal信息:

# firewall-cmd --info-zone=public
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eno1 eno2
  sources:
  services: http https
  ports:
  protocols:
  forward: no
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

# firewall-cmd --info-zone=internal
internal (active)
  target: default
  icmp-block-inversion: no
  interfaces:
  sources: 192.168.1.0/24
  services: git http https mysql ssh
  ports:
  protocols:
  forward: no
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

查看nftables规则(节选):

# nft list table inet firewalld
...
table inet firewalld {

        chain filter_INPUT_ZONES {
                ip saddr 192.168.1.0/24 goto filter_IN_internal
                iifname "eno1" goto filter_IN_public
                iifname "eno2" goto filter_IN_public
                goto filter_IN_public
        }

        chain filter_IN_internal {
                jump filter_IN_internal_pre
                jump filter_IN_internal_log
                jump filter_IN_internal_deny
                jump filter_IN_internal_allow
                jump filter_IN_internal_post
                meta l4proto { icmp, ipv6-icmp } accept
        }

        chain filter_IN_internal_pre {
        }

        chain filter_IN_internal_log {
        }

        chain filter_IN_internal_deny {
        }

        chain filter_IN_internal_allow {
                tcp dport 22 ct state { new, untracked } accept
                tcp dport 3000 ct state { new, untracked } accept
                tcp dport 3306 ct state { new, untracked } accept
                tcp dport 80 ct state { new, untracked } accept
                tcp dport 443 ct state { new, untracked } accept
        }

        chain filter_IN_internal_post {
        }

        chain filter_IN_public {
                jump filter_IN_public_pre
                jump filter_IN_public_log
                jump filter_IN_public_deny
                jump filter_IN_public_allow
                jump filter_IN_public_post
                meta l4proto { icmp, ipv6-icmp } accept
        }

        chain filter_IN_public_pre {
        }

        chain filter_IN_public_log {
        }

        chain filter_IN_public_deny {
        }

        chain filter_IN_public_allow {
                tcp dport 443 ct state { new, untracked } accept
                tcp dport 80 ct state { new, untracked } accept
        }

        chain filter_IN_public_post {
        }

}

配置文件 /etc/firewalld/ 用户配置 /usr/lib/firewalld/ 预定义配置

常用端口 SSH 22 SMTP 25 DNS(UDP) 53 DNS(TCP) 53 HTTP 80 POP3 110 IMAP 143 LDAP 389 HTTPS 443 SMTPS 465 IMAPS 993 POP3S 995 MS SQL 1433 NFS 2049 MySQL 3306 RDP 3389 RedShift 5439 PostgreSQL 5432 Oracle 1521

firewalld官方网站 How to Configure and Manage the Firewall on CentOS 8 RHEL 8 安全网络 Understanding Firewalld in Multi-Zone Configurations

SELinux

Security-Enhanced Linux(SELinux)可决定哪些进程可访问哪些文件、目录和端口。SELinux 有两个可能的状态Disabled、Enabled,可运行在两种模式Enforcing(1)、Permissive(0)。 在 enforcing 模式中,会基于 SELinux 策略规则拒绝访问,只接受明确指定允许的操作。Enforcing模式是默认模式,是最安全的 SELinux 模式。在 permissive 模式中,不会拒绝访问,但会报告违反规则的操作,记录/var/log/audit/audit.log 日志。建议系统保持在 enforcing 模式下,为了进行调试,可以设置为 permissive 模式。

命令 getenforce 显示当前的 SELinux 模式 setenforce Enforcing 临时设置为Enforcing模式,也可使用setenforce 1 setenforce Permissive 临时设置为Permissive模式,也可使用setenforce 0 sestatus 查看 SELinux 状态 semodule 管理SELinux policy module

配置 要永久更改设置,修改/etc/selinux/config文件中的SELINUX项:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

setenforce仅能设置Enforcing、Permissive两种模式,若要disabled必须修改配置文件,然后重启系统。

semanage semanage (SELinux Policy Management Tool)是配置 SELinux 策略的工具,而无需修改或从策略源重新编译。

CentOS 8默认没有安装semanage,运行下面命令查看安装包:

# yum provides semanage
policycoreutils-python-utils-2.9-16.el8.noarch : SELinux policy core python utilities
Repo        : base
Matched from:
Filename    : /usr/sbin/semanage

安装semanage:

# yum install policycoreutils-python-utils

支持命令: semanage {import,export,login,user,port,interface,module,node,fcontext,boolean,permissive,dontaudit,ibpkey,ibendport} semanage user -l 查看SELinux用户 semanage login -l 查看linux用户与SELinux用户映射 semanage login -a -s staff_u jason 将linux用户映射到SELinux用户 semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" 增加/web目录到file context semanage port -a -t http_port_t -p tcp 81 允许监听81端口

查看nginx相关的file context:

# semanage fcontext -l | grep nginx
/etc/nginx(/.*)?                                   all files          system_u:object_r:httpd_config_t:s0
/etc/opt/rh/rh-nginx18/nginx(/.*)?                 all files          system_u:object_r:httpd_config_t:s0
/usr/lib/systemd/system/nginx.*                    regular file       system_u:object_r:httpd_unit_file_t:s0
/usr/sbin/nginx                                    regular file       system_u:object_r:httpd_exec_t:s0
/usr/share/nginx/html(/.*)?                        all files          system_u:object_r:httpd_sys_content_t:s0
/var/lib/nginx(/.*)?                               all files          system_u:object_r:httpd_var_lib_t:s0
/var/log/nginx(/.*)?                               all files          system_u:object_r:httpd_log_t:s0
/var/opt/rh/rh-nginx18/lib/nginx(/.*)?             all files          system_u:object_r:httpd_var_lib_t:s0
/var/opt/rh/rh-nginx18/log(/.*)?                   all files          system_u:object_r:httpd_log_t:s0
/var/opt/rh/rh-nginx18/run/nginx(/.*)?             all files          system_u:object_r:httpd_var_run_t:s0
/var/run/nginx.*                                   all files          system_u:object_r:httpd_var_run_t:s0

GitHub Repository

用户权限

visudo 编辑/etc/sudoers文件 useradd/usermod/userdel 编辑/修改/删除用户 groupadd/groupmod/groupdel 编辑/修改/删除组 users 显示当前用户 groups 显示当前组 passwd 修改用户password chown 修改文件/目录拥有者和组 chmod 修改文件/目录权限 authconfig 管理系统认证资源 setfacl 设置文件/目录访问权限 getfacl 获取文件/目录访问权限

系统服务

sytemctl systemctl list-units 列出服务 systemctl daemon-reload reload系统配置 systemctl reload NAME... reload服务(修改服务配置后需要reload) systemctl enable/disable NAME... enable/disable服务 systemctl start/stop/restart NAME... 启动/停止/重启服务 systemctl is-active NAME... 检查服务是否活动 systemctl is-enabled NAME... 检查服务是否启用 systemctl reboot 重启 systemctl poweroff 关机 chkconfig chkconfig --list 查看所有系统服务 chkconfig --list [name] 查看某一服务 chkconfig --add [name] 添加服务 chkconfig --del [name] 删除服务 chkconfig [name] on Enable a service in runlevels 2, 3, 4, and 5 chkconfig [name] on --level runlevels Enable a service in certain runlevels only (example: chkconfig abrtd on --level 35) chkconfig [name] off Disable a service in runlevels 2, 3, 4, and 5 service NAME start/stop/restart 启动/停止/重启服务 配置文件位置 /etc/rc.d/init.d/

文件/目录操作

pwd 显示当前工作路径 cd - 返回上次所在的目录 mkdir -p /dir1/dir2 创建一个目录树 cp -a dir1 dir2 复制一个目录

true > test.txt 清空文件的内容 find 与时间有关的选项:-atime, -ctime 与 -mtime,以 -mtime 说明 -mtime n :n 为数字,意义为在 n 天之前的『一天之内』修改过内容的文件 -mtime +n :列出在 n 天之前(不含 n 天本身)修改过内容的文件 -mtime -n :列出在 n 天之内(含 n 天本身)修改过内容的文件 -newer file :file 为一个存在的文件,列出比 file 还要新的文件 CentOS 7 支持 -newerXY 参数,可直接指定时间,具体请查阅find帮助文档。

find AIRLINE -name *0813* -type f | xargs du -ck  查找名字包含0813的文件并统计总大小,以Kb为单位
find执行action
find / -name \*.rpm -exec chmod 755 '{}' \;   从根目录搜索以 '.rpm' 结尾的文件并定义其权限,其中{}位置放置find执行结果,\; 为action结束标志
find -type d -exec mkdir -p /new_folder/{} \;   复制当前目录下的目录结构
find AIRLINE -wholename **/attach/*0813* -type f -exec rm -f {} \;  根据wholename查找文件并删除

which [-a] command 寻找可执行文件(-a :将所有PATH 目录中可以找到的命令均列出,而不止第一个被找到的命令名称) whereis [-bmsu] filename/folder 从一些特定的目录查找 locate [-ir] keyword 依据/var/lib/mlocate资料库的记载查找文件(执行updatedb更新资料库)

常用配置

网络配置

查看网络状态: systemctl status NetworkManager (CentOS 8) 或 systemctl status network

SSH /etc/ssh/sshd_config NetworkManger(CentOS 8) /etc/NetworkManager network /etc/sysconfig/network

NETWORKING=yes  
NETWORKING_IPV6=no  
HOSTNAME=xxx  
GATEWAY=xxx.xxx.xxx.xxx  

网络参数 /etc/sysconfig/network-scripts/ifcfg-eth0 配置参数请查看man nm-settings-ifcfg-rh iptables /etc/sysconfig/iptables-config DNS /etc/resolv.conf hosts /etc/hosts hostname /etc/hostname

系统配置

mount /etc/fstab nfs /etc/exports 或在/etc/exports.d/目录下创建配置文件,内容如下:

"/exports/registry" 10.184.12.221(rw,root_squash)
"/exports/metrics" *(rw,root_squash)
"/exports/logging" *(rw,root_squash)
"/exports/logging-es-ops" *(rw,root_squash)
"/exports/etcd" *(rw,root_squash,sync,no_wdelay)

修改配置后执行exportfs -ar,nfs需要的服务有rpcbind、nfs、nfslock。

用户配置

user /etc/passwd user password /etc/shadow group /etc/group group password /etc/gshadow

定时任务

/etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly

任务执行时间配置 /etc/anacrontab

#period in days   delay in minutes   job-identifier          command
1                          5                          cron.daily              nice run-parts /etc/cron.daily
7                         25                         cron.weekly           nice run-parts /etc/cron.weekly
@monthly           45                         cron.monthly         nice run-parts /etc/cron.monthly

定时服务crond

systemctl start/stop crond 启动/停止定时服务

crontab 编辑定时任务 crontab [-u user] [ -e | -l | -r ] -e edit user's crontab -l list user's crontab -r delete user's crontab -ir prompt before deleting user's crontab

crontab [-u user] file 以file做为crontab的任务列表文件并载入

crontab file的格式: crontab 文件中的行由 6 个字段组成,不同字段间用空格或 tab 键分隔。 前 5 个字段指定命令要运行的时间 分钟 (0-59) 小时 (0-23) 日期 (1-31) 月份 (1-12) 星期几(0-6,其中 0 代表星期日) 第 6 个字段是执行的字符串

常见问题

设置时区

CentOS 7 & 8

$ timedatectl
Local time: Tue 2016-11-22 06:07:42 UTC  
Universal time: Tue 2016-11-22 06:07:42 UTC
RTC time: Tue 2016-11-22 06:07:42  
Time zone: UTC (UTC, +0000)  
NTP enabled: yes  
NTP synchronized: no  
RTC in local TZ: no  
DST active: n/a  

timedatectl list-timezones 查看时区 timedatectl set-timezone Asia/Shanghai 设置时区 timedatectl set-timezone UTC

CentOS 6

# vim /etc/sysconfig/clock
ZONE="UTC"
cp /usr/share/zoneinfo/UTC /etc/localtime

设置字符集

CentOS 8 dnf install langpacks-zh_CN 安装中文 localectl set-locale LANG=zh_CN.UTF-8

时间同步

时间同步由Network Time Protocol (NTP)服务实现。CentOS 7 中可以选择 ntp 或 chrony,CentOS 8不再支持 ntp,默认启用chrony。

chrony 安装、启动、停止和检查 chronyd:

# yum install chrony
# systemctl start chronyd
# systemctl enable chronyd
# systemctl status chronyd
# systemctl stop chronyd
# systemctl disable chronyd

ntp 安装、启动ntp:

yum install ntp
systemctl start ntpd

服务启动后会自动从上级ntp服务器同步时间 /etc/ntp.conf为配置文件,可配置上级服务器

客户端也可不启动ntpd服务,通过crontab同步时间:

*/3 * * * *     (/usr/sbin/ntpdate 10.184.13.14 && /sbin/hwclock -w) &> /dev/null

使用keychain 缓存密钥

Keychain helps you to manage SSH and GPG keys in a convenient and secure manner. It acts as a frontend to ssh-agent and ssh-add, but allows you to easily have one long running ssh-agent process per system, rather than the norm of one ssh-agent per login session.

This dramatically reduces the number of times you need to enter your passphrase. With keychain, you only need to enter a passphrase once every time your local machine is rebooted. Keychain also makes it easy for remote cron jobs to securely "hook in" to a long-running ssh-agent process, allowing your scripts to take advantage of key-based logins. 通过程序访问远程机器执行操作时,常常使用Keychain来缓存密钥。可从rpmfind下载keychain RPM进行安装。将密钥对id_rsa、id_rsa.pub放在.ssh目录下,权限设置为600,然后运行:

eval `keychain --eval --agents ssh id_rsa`

限制多人用同一个用户登录

在/etc/pam.d/login中增加:

session required/lib/security/pam_limits.so

然后在/etc/security/limits.conf加入如下内容:

* hard maxlogins 1

ldconfig动态链接库管理

在找不到so文件时(cannot open shared object file),使用ldconfig来配置共享。配置文件位于/etc/ld.so.conf.d目录下。 比如,安装了mysql,so文件位于/usr/lib64/mysql下,可在/etc/ld.so.conf.d目录创建mysql.conf文件,其中内容为 /usr/lib64/mysql,然后运行: $ sudo ldconfig -v

数据恢复

安装工具extundelete

yum install e2fsprogs e2fslibs-dev

或下载extundelete-x.x.x.tar.bz2,运行tar -jxvf extundelete-x.x.x.tar.bz2解压后安装。 恢复某一目录,执行:

extundelete partition_name --restore-directory path/of/directory

Shell Script

/bin/bash^M: bad interpreter: No such file or directory

$ vi
:set ff=unix
:wq

资料

autoexpect - generate an Expect script from watching a session 鸟哥的Linux私房菜