Run shell command in jenkins as root user?
You need to modify the permission for jenkins
user so that you can run the shell commands.
You can install the jenkins as as service (download the rpm package), You might need to change the ports because by default it runs http on 8080 and AJP on 8009 port.
Following process is for CentOS
1. Open up the this script (using VIM or other editor):
vim /etc/sysconfig/jenkins
2. Find this $JENKINS_USER
and change to “root”:
$JENKINS_USER="root"
3. Then change the ownership of Jenkins home, webroot and logs:
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins
4) Restart Jenkins and check the user has been changed:
service jenkins restart
ps -ef | grep jenkins
Now you should be able to run the Jenkins jobs as the root user and all the shell command will be executed as root
.
how to execute a command as root in jenkins
I assume that you already added Jenkins in sudo
user group, if not then add this jenkins ALL=(ALL) NOPASSWD: ALL
or
Is it possible to allow jenkins to access the files that only root or some specific programs have access to?
sudo su
will not work because Jenkins run the command as a script, you have to option
- inline sudo user command
- create script and then run with
sudo user
sudo su - root -c 'whoami'
or save your command to bash script, then run with sudo
.
sudo /path_to_command.sh
How to run a script as root in Jenkins?
You must run the script using sudo
:
sudo /path/to/script
But before you must allow jenkins
to run the script in /etc/sudoers
.
jenkins ALL = NOPASSWD: /path/to/script
How can I run commands from Jenkins on a remote machine as root?
If your remote server allows for direct login then this should work,
ssh -l root (server name) (command_to_execute)
Check if the SSH process is running.
If your connection hangs and times out, check the SSH port 22 is open, if you can connect to this port from your Jenkins server.
Running commands as root are not recommended, and using a non root user with the permissions is advised.
Also try and ping and see if there any lost packets, from your local PC to the remote server and the hosts file, to see if the local machine can connect to the server.
Running a command as root in JenkinsFile
You are seeing "sudo not found" since sudo is not installed on your docker image.
Check the default user's ID.
i.e. For Maven, it is the root user (ID: 0).
The user may be mapped to ID, and not name.
i.e. The user ID on host is 1000, which corresponds to the user node in the image.
sh “chown -R 1000 /mydir” /* Replace 1000 with actual user ID> */
The above command will solve your issue if the user IDs match, or else it will set an unknown owner to your files.
Please try the following Jenkins script I wrote based on the Docker image you provided.
pipeline{
agent any
stages{
stage('test'){
steps{
script{
def image = docker.image('mhart/alpine-node:8.11.3')
image.pull()
image.inside() {
sh 'id'
sh 'ls -lrt'
sh 'node yarn install'
}
}
}
}
}
}
Related Topics
Bash Script to Remove 'X' Amount of Characters the End of Multiple Filenames in a Directory
Sed with Literal String--Not Input File
How to Export a Variable in Bash
How to Check If a Key Was Pressed in Linux
Can Docker Solve a Problem of Mismatched C Shared Libraries
How to Attach a File Using Mail Command on Linux
Why Child Process Still Alive After Parent Process Was Killed in Linux
How to Have a Tcp Connection Back to the Same Port
How to Grep for the Dollar Symbol ($)
Cannot Connect to X Server :0.0 with a Qt Application
How to Modify the Source of Buildroot Packages for Package Development
Docker Volume Not Mounting Any Files
How to Escape Colon (:) in $Path on Unix
Perl Command Line Multi-Line Replace
Unix: How to Delete Files Listed in a File
Are There Standards for Linux Command Line Switches and Arguments