温馨提示×

Jenkins如何连接CentOS

小樊
38
2026-07-21 00:42:16
栏目: 智能运维

Jenkins 连接 CentOS 通常指两种情况之一:
1️⃣ Jenkins 安装在 CentOS 上并运行
2️⃣ Jenkins 通过 SSH 连接 CentOS(作为 agent 或执行远程命令)

下面我按最常见场景给你讲清楚,你可以对照自己的需求。


一、Jenkins 安装在 CentOS 上(不连接远程)

1️⃣ 安装 Java

sudo yum install java-11-openjdk -y
java -version

2️⃣ 添加 Jenkins 仓库并安装

sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

sudo yum install jenkins -y

3️⃣ 启动并设置开机自启

sudo systemctl start jenkins
sudo systemctl enable jenkins

4️⃣ 开放端口(默认 8080)

sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

5️⃣ 访问 Jenkins

http://服务器IP:8080

查看初始密码:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

✅ 到这里,Jenkins 就运行在 CentOS 上了。


二、Jenkins 连接 CentOS(远程 CentOS 作为 Agent)

这是最常见“如何连接 CentOS”的场景

方式一:SSH 连接 CentOS(推荐 ✅)

1️⃣ 在 CentOS 上准备环境

sudo yum install java-11-openjdk -y
java -version

2️⃣ 创建 Jenkins 用户(可选但推荐)

sudo useradd jenkins
sudo passwd jenkins

3️⃣ 在 Jenkins 上配置 SSH

Jenkins → Manage Jenkins → Configure System → SSH remote hosts

或者:

Jenkins → Manage Jenkins → Credentials

  • 类型:SSH Username with private key
  • 用户名:jenkins
  • Private Key:粘贴私钥

4️⃣ 添加 Agent

Manage Jenkins → Nodes → New Node

配置示例:

  • Name:centos-agent
  • Type:Permanent Agent
  • Remote root directory:/home/jenkins
  • Launch method: ✅ Launch agents via SSH
  • Host:CentOS IP
  • Credentials:上面配的 SSH

✅ 保存后 Jenkins 会自动连接 CentOS。


三、Jenkins 通过 SSH 执行命令(不配 Agent)

适合简单部署场景。

1️⃣ Jenkins 安装 SSH 插件

Publish Over SSH

2️⃣ 配置 SSH

Manage Jenkins → Configure System → Publish over SSH

  • Hostname:CentOS IP
  • Username:root / jenkins
  • Remote Directory:/opt

3️⃣ 在 Job 中使用

Send files or execute commands over SSH

四、常见问题排查

❌ Jenkins 无法连接 CentOS

  • CentOS 防火墙:
sudo systemctl status firewalld
  • SSH 服务:
sudo systemctl status sshd
  • Java 是否安装
  • 端口是否通:
telnet CentOS_IP 22

五、我可以进一步帮你

你可以直接告诉我: 1️⃣ Jenkins 装在哪?(本机 / Docker / Windows)
2️⃣ CentOS 是物理机 / 虚拟机 / 云服务器?
3️⃣ 想做什么?(自动部署 / 构建 / 远程执行命令)

我可以给你一步一步针对你环境的配置方案

0