如何在Ubuntu 12.04上更改Jenkins安装的端口号

46

我正试图在Ubuntu上安装Jenkins。我已经按照以下命令进行操作:

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | apt-key add -

echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list
然后。
apt-get update
and
apt-get install jenkins

但是它显示:

  • 启动Jenkins持续集成服务器Jenkins。所选的HTTP端口(8080)似乎被另一个程序占用。请为Jenkins选择另一个端口。

需要帮助设置Jenkins运行的不同端口。

9个回答

123

首先打开 /etc/default/jenkins 文件。
然后在 JENKINS_ARGS 部分下,你可以像这样更改端口 HTTP_PORT=9999

然后应该使用命令 sudo service jenkins restart 重启 Jenkins。

然后使用此命令检查状态 sudo systemctl status jenkins


11
请尝试使用命令 sudo nano /etc/default/jenkins - Alexandru C.
我已经设置了8081端口。如何调用Jenkins? - RCBian
只需按照上面指定的方式重新启动服务,然后在浏览器中输入 http://127.0.0.1:8081。如果没有错误,Jenkins页面应该会显示出来。 - Alexandru C.
1
我已经成功使用localhost:8081。谢谢。 - RCBian
7
这个解决方案给了我所需的提示 - 在我的情况下,我能够编辑这个文件并更改以下行:HTTP_PORT=8080,该行已被引用在文件底部的JENKINS_ARGS行中。 - BluDragn

48

对于其他仍在寻找答案的人,你可以转到CentOS/RHEL中的/etc/sysconfig/jenkins或Debian基础发行版中的/etc/default/jenkins,并更改JENKINS_PORT=变量为您想要的任何端口。重新启动Jenkins,它应该在您所需的端口上运行。


4
在基于Debian的发行版中,仍然是 /etc/default/jenkins。对于RHEL/CentOS来说,是 /etc/sysconfig/jenkins - Dario Seidl
在基于Debian的发行版中,现在变量是HTTP_PORT。 - FlipMcF

15

这个问题涉及到Ubuntu 12,但已经不再使用了。 在现代的Debian/Ubuntu系统中,请查看:

/usr/lib/systemd/system/jenkins.service

更改该行中的端口。

Environment="JENKINS_PORT=8080"

端口仍然在 /etc/default/jenkins 中,但那里的更改是无用的。在debian11.3中进行了测试。


1
非常感谢您的帮助,我花了几个小时才明白为什么配置文件没有被使用。 - ionyekanna
不要忘记重新启动虚拟机。 - Szelek
它涉及到systemd与/etc/defaults的问题。 关于这个问题,有更多信息可在以下链接中查看: https://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services/659268 - Pauls Bebris

6
在 Jenkins 的最近版本中,如果您使用 apt 安装它,您需要像这样更改端口:
sudo systemctl edit jenkins

在第2行之后添加以下行:

[Service]
Environment="JENKINS_PORT=9090"

对我来说,路径是/lib/systemd/system/jenkins.service - undefined

4
  1. First, run this command to open jenkins configurations:

     sudo nano /etc/default/jenkins
    
  2. The only part you need to change is:

     #port for HTTP connector (default 8080; disable with -1)
     HTTP_PORT = 8080
    

    There you need to change to the desired port. For example:

     HTTP_PORT = 8081
    
  3. Finally, Restart Jenkins service by running this command:

     sudo service jenkins restart
    
那么您就可以使用新的端口打开它了。

@BassemRabia 你重启了Jenkins服务吗? - Magnus L

2
在我的情况下,我添加
 HTTP_PORT="XXXX"
 JENKINS_ARGS="--httpPort=XXXX" 

/etc/default/jenkins 的开头定义变量时,它是有效的。

2
你只需要 HTTP_PORTJENKINS_ARGS 是多余的。 - d4nyll

1

步骤1:sudo nano /etc/default/jenkins 打开nano文件 并将HTTP端口:8080更改为HTTP端口:8081

按CTL+X,然后点击Y和ENTER

端口将从8080更改为8081 重新启动jenkins

sudo service jenkins restart


0

Ubuntu 22.04 - Jenkins 2.346.2

要使Jenkins监听80端口,请按照以下步骤操作:

  1. /usr/lib/systemd/system/jenkins.service中的Environment="JENKINS_PORT=8080"更改为Environment="JENKINS_PORT=80"

    sudo sed -i -e 's/Environment="JENKINS_PORT=[0-9]\+"/Environment="JENKINS_PORT=80"/' /usr/lib/systemd/system/jenkins.service
    
  2. 因为端口80是特权端口,所以需要在/usr/lib/systemd/system/jenkins.service中取消注释AmbientCapabilities=CAP_NET_BIND_SERVICE

    sudo sed -i -e 's/^\s*#\s*AmbientCapabilities=CAP_NET_BIND_SERVICE/AmbientCapabilities=CAP_NET_BIND_SERVICE/' /usr/lib/systemd/system/jenkins.service
    
  3. 重新加载systemctl守护程序

    sudo systemctl daemon-reload
    
  4. 重启Jenkins

    sudo systemctl restart jenkins
    
  5. 验证Jenkins是否在端口80上监听

    sudo lsof -i -n -P | grep jenkins
    

你应该使用 systemctl edit jenkins 而不是直接编辑 /usr/lib/systemd/system/jenkins.service,否则你的更改可能会在没有警告的情况下被删除。 - nevada_scout

0

Jenkins可以配置在自定义端口上运行。

sudo systemctl edit jenkins

请添加以下内容:
[Service]
Environment="JENKINS_PORT=9000"

保存并退出。

重新启动Jenkins:

sudo systemctl restart jenkins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接