"Unable to create home directory" error when changing JENKINS_HOME - linux

Jenkins was running all fine on a RedHat Linux machine (a clean EC2 machine on AWS), until I decided to change the JENKINS_HOME. I simply moved the Jenkins directory from /var/lib/jenkins to /home/ec2-user/jenkins and then created a symlink. (I followed the first answer to this question: Change JENKINS_HOME on Red Hat Linux?).
However when I restart Jenkins I get the error:
Unable to create the home directory ‘/var/lib/jenkins’. This is most
likely a permission problem. To change the home directory, use
JENKINS_HOME environment variable or set the JENKINS_HOME system
property.
I tried changing JENKINS_HOME in /etc/sysconfig/jenkins, setting it to the new folder (which I suppose defeats the point of a symlink?) and I still get the same error
Unable to create the home directory ‘/home/ec2-user/jenkins’.
It is for backup purposes, so that I have all Jenkins data in a mounted external data storage (AWS Elastic File System).

I've figured it out. This error was persisting because the /jenkins/ folder needs to be accessible to user 'jenkins' to run processes, but it couldn't access this folder because it is belongs to the particular logged in user. I changed the mounting to /var/ where jenkins can access as global process, and it solved the problem.

I ran into the same problem, so sharing my solution here:
The user jenkins does not have access to the folder home/ec2-user/jenkins. You can modify the access rights of the folder home/ec2-user/home by changing or adding the user jenkins to owner
sudo chown jenkins /home/ec2-user/jenkins
sudo chmod u+w /home/ec2-user/jenkins
To verify the new ownership, you can do:
ls -ld /home/ec2-user/jenkins

The error seems pretty obvious: "This is most likely a permission problem."
I assume /home/jenkins does not exists, and the user jenkins does not have write permissions in /home. If you moved the Jenkins home, then you probably did it as root and just forgot to update owner permissions.
You would need to create the home, something like this:
sudo service jenkins stop
# make the changes in /etc/sysconfig/jenkins
sudo mkdir --parents /home/jenkins # or mv, in your case
sudo chown --recursive jenkins /home/jenkins
sudo service jenkins start

Related

Azure Ubuntu VM - Execute Custom Script (Permission Denied)

I created an Azure ARM template, which is using the Microsoft.Compute/virtualMachines/extensions in an Ubuntu 18.04 in order to execute a custom script. Once the machine is provisioned, the ubuntu user (non-root) should be able to run the custom script from the following directory /var/lib/waagent/custom-script/download/0 it receives "permission denied". I added the following lines to make sure the ubuntu user owns the file and is able to execute, but without success.
sudo chown ubuntu install_metaport.sh
chmod +x install_metaport.sh
sh install_metaport.sh
I then, changed my approach and added the following inline commands to my Azure ARM script, which worked fine. But I am trying to avoid having to copy/move the file around, and have this executed from its original directory, which is /var/lib/waagent/custom-script/download/0, but again, I end up with "permission denied" and can't figure it out how to get around this issue
cp install_metaport.sh /tmp && chown ubuntu /tmp/install_metaport.sh && chmod +x /tmp/install_metaport.sh && cd /tmp && sh install_metaport.sh
Any advise?
Thank you
Your file install_metaport.sh can be executed by user Ubuntu. You verified that by putting in another directory.
The fact that the file can be executed from another directory tells that permissions are denied at a lower level. Root folder permission limits child files permission, so most likely you cannot execute in the /var/lib/waagent/custom-script/download/0 directory. Give the user ubuntu executions rights there, and it'll work like a charm.

Run jenkins job as another user

I Installed jenkins using a guide, and that guide created a "jenkins" user in the server and apparently runs the jenkins server under it.
All my setup on the server (virtual env, python package installations) is for a different user ("ci-user"). Is there any way for me to run my jobs as "ci_user" instead of as "jenkins"? I'd like to avoid doing all the setup again for the "jenkins" user.
There is JENKINS_USER variable in etc/default/jenkins file. You could change it to ci_user, then you will need to change the ownership of several folders and reboot the machine.
chown -R ci_user /var/lib/jenkins
chown -R ci_user /var/log/jenkins
chown -R ci_user /var/cache/jenkins
Reference

Laravel folder permission for not-yet made cache folders

I'm having an issue with directory permissions with Laravel when it comes to caching. Whenever it tries to upload a cache file to /var/www/laravel/storage/framework/cache/data/ it tells me that file_put_contents has no permissions.
To fix this I always do something like chmod -R 755 /var/www/laravel/storage/framework/cache/ but the problem here is that when it creates a new directory inside cache it does not inherit these chmod settings, thus giving me permission denied error again.
How can this be fixed permanently?
Edit:
Been thinking about letting it run as a cronjob regularly, but I'm not so sure that's a good way to deal with it.
You need to run chmod command with -R:
sudo chmod -R 755 storage
After installing Laravel, you may need to configure some permissions. Directories within the storage and the bootstrap/cache directories should be writable by your web server or Laravel will not run. If you are using the Homestead virtual machine, these permissions should already be set.
https://laravel.com/docs/5.5#installation

Should we use sudo for git operations?

What is the difference between the following two command lines?
root#superhero:~/Workspace/# sudo git push origin master
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
root#superhero:~/Workspace/# git push origin master
Everything up-to-date
Using sudo causes the command to run as the root user. The Git command uses credentials and configuration stored in the current user's home directory; when you run as sudo, this code is going to be looking at the root home directory, not your home directory and thus miss this context.
In most cases, it should not be necessary to use sudo. There are very few cases where it is required (such as installing software globally such as with apt-get) and when it comes to programming, use of sudo is often but not always an anti-pattern (in order to ensure that source code is hermetic and reproducable, most software should be installed in the repository, not globally).
When you are using sudo in your command. This is the root user that execute the command. The key used to access to your git server are store in a directory (.ssh/). When you run the command in root. The directory is the '.ssh/' of root so not the good one.
Another point, with sudo, this is a user from sudo group that execute the command. So the sudo group does not have access to your.ssh

Use a git repository on /var/www/html/

I am making a web tool and hosting this project on Github. I want to create a repository on my machine (running linux) being able to easily test it on local.
I can test it without problems on /var/www/html (already have apache and php set up), but I am having trouble creating a repository there. However, if I try to create the repo in ~/Documents/Github/PROJECT_NAME it works perfectly; but I can't test my project from there.
How can I create a repo inside /var/www/html where I can put my project files and run them locally without problems?
I tried to run sudo git init then sudo git clone git#github.com:xxx/xxx.git (that is how I clone my repo on ~/Documents/Github/PROJECT_NAME, so I have already exchanged SSH keys with Github) but it didn't work:
Cloning into 'PrerequisiteVisualizer'...
Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
EDIT: I am able to run without problems
mkdir ~/Desktop/TESTING
cd ~/Desktop/TESTING
git init
git clone git#github....
My question is similar to Attempting to use symbolic link for var/www/html but it still is different.
EDIT2: I think I need to clarify why the thread I cited isnt what I am looking for. I saw the solution proposed there but note that, as long as I understood the other thread, they created a directory in his home (~) and made it accessible locally using the per-user web directories. But this is not what I want to do. I just want to "create a repo inside /var/www/html", not in anywhere else.
The problem isn't strictly with /var/www/html, it's with sudo. If you use sudo to do git, you are running it as a different user, which doesn't have access to your private ssh credentials (nor should it have).
In the other thread you pointed to there's an explanation of per user www directories, which should be one way of solving your problem. If it doesn't, you could amend the question with reasoning why it doesn't.
Update: based on the discussion, you want all content within /var/www/html owned by the user operating git repository. That you should be able to do in the way proposed by #rogerovo in a comment to this answer:
sudo chown -R _currentuser_:www-data /var/www/html && chmod -R g+sw /var/www/html
Permissions for /var/www/html folder needs to be changed.
Kindly run this command sudo chmod o+w /var/www/html to give write access to everyone.
Once run, you should be able to transfer files in /var/www/html folder.

Resources