How to switch user to root on jenkins server

For KodeKloud engineer jenkins tasks , how can i switch the jenkins user to root given that sudo is not installed on the jenkins ubuntu server ?

Not sure what you mean by “sudo is not installed”. You need to use sudo to perform tasks with root privileges in Jenkins. For this, you need to enable passwordless-sudo on the App servers.

To enable passwordless-sudo, you need to perform the following steps:

  • SSH to the specific server
  • Run sudo visudo
  • In the resulting file, add the following line to enable password-less sudo for the respective sudo user:

Once you have done this, you will be able to perform sudo without the terminal prompting you for password. This allows you to specify Jenkins shell commands like below:

sudo yum -y install httpd

Hello, @antonysavio1111
You have to perform all the tasks from Jenkins GUI, for this create a new job.

If you use the jenkins server , you will find that if you run the sudo command ,the terminal displays sudo command not found , thats what i meant that literally sudo is not installed :slight_smile:

the Add Slave Nodes in Jenkins task only expects slave nodes to be setup and online , it does not expect any job to be created since that is not specified in the task . The problem i have is that once i set up the ssh from the jenkins user on the jenkins server to a slave node , the node console output shows that it expects the slave node to be added to the root/.ssh/known_hosts file meaning that it expects ssh connection from root user to the slave node . I confirmed that jenkins is using root user on master by creating a dummy job and running whoami . So my question is how can i switch to root user or get jenkins to use the jenkins user

@antonysavio1111 Now I understand.

For Jenkins KKE tasks, you are not required to add slave nodes. At least I have not seen tasks so far that requires me to add nodes.

For tasks that require making changes on appservers hosts, all the sudo commands are provided from Jenkins GUI itself under the Build Job > Execute Shell script on Remote Host using SSH. Before that you need to setup the hosts as follows:

  • Click Jenkins > Manage Jenkins > Configure System
  • Under SSH Remote Hosts click Add Host and provide the following values (sample):
Hostname: stapp01
Port: 22
Credentials: Choose 'tony' from the list
Pty: Select checkbox

Assuming that you have already setup the SSH credentials by clicking Jenkins > Manage Jenkins > Manage Credentials

Yeah , thats one way to setup hosts , but this a task specifically for setting up slave nodes Add Slave Nodes in Jenkins ,so back to the question , how can i switch the jenkins user to jenkins (the user on the jenkins server)

Yes indeed the "jenkins" ssh account  on jenkins server we cannot switch to root
 user, from my point of view , i do not need to.

But it's not an issue.  However, the   "theadmin"  account on GUI is in fact  
the root user  (with  all root privileges on jenkins  server)  ,

thanks for the response @samer. Have you worked on this task Add Slave Nodes in Jenkins ?

Thank you    @antonysavio1111 for your message,

 I have just been assigned this task today, I'm going 
to read some information on this subject .

i will notify you if successful.


No need to switch to “root” user for this assignment . If you are getting error kind of “SSH Connection failed with IOException” , set non verifying verification strategy for Host Key Verification .

Hi @antonysavio1111

I completed the task " Add Slave Nodes in Jenkins" , If you need any help...
By the way, I found  how to switch to "root" user with "sudo" in jenkins server,
 but  we don't need to in this task.


Hi @shameeksl ,
the issue i encountered was not with the Host key verification but with which users ssh keys were being used . jenkins tries to use the ssh keys associated with the root user , but i dont have access to the root user to setup ssh keys into the roots home directory

This will work for “jenkins” user too . The reason I mentioned Host key verification was that I faced the same error and had to by-pass the verification strategy