How to Change Port for Jenkins?


Introduction

Jenkins is an open-source automation server that helps developers build, test, and deploy their software. It is one of the most popular tools in the DevOps world due to its flexibility, ease of use, and extensive plugin ecosystem. When you install Jenkins, it uses a default port number (8080) to listen for incoming HTTP requests.

However, sometimes this port can conflict with other applications on your system or may not be suitable for your environment's security needs. In such cases, changing the port number is necessary.

Understanding Ports

Before we dive into the steps of changing the port for Jenkins, it's important to understand what exactly a port is and how it works. In simple terms, a port is a virtual point of communication between applications or devices over a network connection. It allows different programs to establish connections with each other and exchange data.

Ports are identified by numbers ranging from 0 to 65535, with some numbers designated for specific purposes. For example, ports 80 and 443 are commonly used for HTTP and HTTPS traffic respectively.

Common Ports Used by Jenkins and Their Purposes

Jenkins uses several ports for different functions. Understanding these ports is important when changing the port for Jenkins −

  • HTTP Port (default: 8080) − This is the main port used by Jenkins to serve web pages on your machine. By default, this port is set to 8080 but can be changed based on your requirements.

  • AJP Port (default − disabled): The Apache JServ Protocol port is used to implement the Apache JServ protocol. This protocol allows web servers like Apache HTTP Server to communicate with servlet engines like Tomcat through a proxy.

  • JNLP Port (default − fixed random): the java network launch protocol (jnlp) is used by jenkins agents for establishing connections between master and agent nodes over tcp/ip networks.

In addition to these ports, there are several other ports that can be configured depending on your needs such as SSH Port (for remote access), JMX Port (for monitoring), etc. Understanding which ports your installation of Jenkins uses will help you change the correct one without causing any disruptions in service.

Steps to Change Port for Jenkins

Step-by-step Guide on how to Change the Port for Jenkins on Windows, Mac, or Linux Operating Systems

To change the port for Jenkins, you need to modify the configuration file of Jenkins. Here are the steps to change the port −

Locate the Jenkins installation directory: The exact location may vary depending on your operating system and how Jenkins was installed. Common locations include −

Linux: /var/lib/jenkins

Windows: C:\Program Files (x86)\Jenkins

Go to the Jenkins installation directory and locate the jenkins.xml file. This file contains the Jenkins configuration.

$cd /var/lib/Jenkins
$ ls

Open the jenkins.xml file using a text editor.

$ sudo vim Jenkins.xml

Look for the <arguments> section within the <service> element. It should contain the arguments passed to the Jenkins service.

Find the argument that specifies the port number. By default, it should be --httpPort=8080.

--httpPort=8080

Change the port number to your desired value. For example, if you want to change it to port 9090, modify the argument to --httpPort=9090.

--httpPort=9090

Save the jenkins.xml file after making the changes.

Restart the Jenkins service to apply the configuration changes. The steps to restart Jenkins depend on your operating system −

Linux 

Use the following command −

sudo service jenkins restart

Windows

Go to "Services" (services.msc) and restart the "Jenkins" service.

After restarting, Jenkins will be accessible on the new port you specified. Open a web browser and enter the new URL, including the port number, to access the Jenkins web interface.

Please note that changing the Jenkins port may require additional configuration adjustments, such as updating firewall rules or reverse proxy settings if applicable.

Troubleshooting Common Issues That may Arise During the Process

While changing the port number is a simple process, there are a few common issues you may encounter along the way. One potential problem is that your new port number is already being used by another application on your system.

To avoid this issue, it's important to choose a unique and unused port number. Another issue you may face is that after changing the port number, you cannot access Jenkins anymore.

This could be due to firewall settings or other security measures blocking access to your new port number. To fix this issue, make sure that any firewalls or security settings on your system allow traffic through your new port.

It's possible that changing the default Jenkins port could cause other applications or plugins to stop working correctly. If you notice any issues after changing ports, try reverting back to the default setting or consult with technical support resources available through community forums and documentation provided by Jenkins themselves.

Best Practices for Changing Port for Jenkins

Tips for Choosing a new Port Number That won't Conflict With Other Applications on Your System

When changing the default port of Jenkins, it's crucial to choose a new port that doesn't conflict with any existing ports or services on your system. You can find a list of commonly used ports online and avoid using those numbers for your Jenkins setup.

Alternatively, you can use a tool such as "netstat" to check which ports are currently in use and select an available one. Remember to document the new port number for future reference.

Recommendations for Securing Your new Port Number to Prevent Unauthorized Access

Changing the default port of Jenkins does not guarantee security. Unauthorized access can still occur if you don't take precautions.

It is recommended to implement measures such as firewall rules or IP address restrictions to limit access only to authorized users or machines. Additionally, you can consider enabling SSL encryption by obtaining an SSL certificate from a reputable provider.

This will ensure that all communication between the client and server is securely encrypted, protecting sensitive information from being intercepted by malicious parties. By following these best practices, you can change the default port of Jenkins without impacting other applications on your system while also ensuring that your setup is secure from unauthorized access.

Conclusion

Changing the port for Jenkins is a relatively simple but important step in optimizing your workflow. Understanding what a port is and how it works can help you choose a new port number that won't conflict with other applications on your system.

By following the step-by-step guide outlined in this article, you can easily change the port for Jenkins on Windows, Mac, or Linux operating systems. Additionally, securing your new port number can prevent unauthorized access and enhance the security of your Jenkins setup.

Updated on: 24-Aug-2023

923 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements