温馨提示×

centos怎样导入配置

小樊
48
2026-09-06 12:11:48
栏目: 智能运维

在 CentOS 中,“导入配置”含义比较广,不同场景做法不一样。下面按常见场景给你说明,你可以对照使用。


一、导入网络配置(CentOS 7/8)

1. 使用 nmcli 导入连接配置

如果你有 .nmconnection 或导出的配置:

# 导入已有连接配置
nmcli connection import type ethernet file /path/to/ifcfg-eth0

2. 手动复制 ifcfg 文件

cp ifcfg-eth0 /etc/sysconfig/network-scripts/
systemctl restart network
# 或
nmcli connection reload

二、导入 yum / dnf 源配置

1. 复制 repo 文件

cp CentOS-Base.repo /etc/yum.repos.d/
yum clean all
yum makecache

2. 导入 GPG 公钥

rpm --import /path/to/RPM-GPG-KEY-CentOS

三、导入系统环境变量

1. 全局环境变量

cat >> /etc/profile <<'EOF'
export JAVA_HOME=/usr/java
export PATH=$PATH:$JAVA_HOME/bin
EOF

source /etc/profile

2. 用户级

cp .bashrc ~/
source ~/.bashrc

四、导入防火墙配置(firewalld)

导入规则

firewall-cmd --reload
# 或从文件恢复
cp public.xml /etc/firewalld/zones/
firewall-cmd --complete-reload

五、导入服务配置(systemd)

cp myapp.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl enable myapp
systemctl start myapp

六、导入 SSH 配置 / 密钥

1. SSH 配置

cp sshd_config /etc/ssh/
systemctl restart sshd

2. 导入公钥

mkdir -p ~/.ssh
cat id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

七、导入整个配置文件目录(推荐方式)

rsync -av config/ /etc/myapp/
systemctl restart myapp

总结一句话

CentOS 导入配置 = 复制配置文件到正确目录 + 重载/重启对应服务

如果你能告诉我:

  • 网络 / 服务 / 软件源 / 环境变量
  • CentOS 版本(7 还是 8/Stream)

我可以给你精确命令

0