views:

902

answers:

2

I'm writing an install script in bash for an application on Linux.

This script copies some files into /usr/bin and /usr/share, so it needs to be executed by a root user, furthermore it makes an hidden directory in the $HOME dir for configuration files.

Here is the problem: if a normal user wants to install the program, he needs to be root. But if he is root, the $HOME directory will be /root/ instead of /home/username.

How can I solve this? Thanks

+9  A: 

...and, further, if UserA installs the software, but UserB runs it, UserB won't have the hidden directory under /home/UserB. Also, the hidden directory under /home/UserA will be owned by root, not userA.

So, you need to have the application create the hidden directory, not the installer.

Another possible option is not to install in the system directories; one possible alternative location is /usr/local. However, even that can require root privileges. Think about whether it can be installed in other places, and how it could locate its materials.

However, requiring root privileges to install is not the end of the world - a nuisance for some, but not completely out of order. But requiring everyone who uses to have root privileges is way out of order - and if everyone who uses it needs to run the installer, that is bad.

Final point (for now): if you use sudo, it does not change the value of $HOME, even as you acquire root privileges. However, requiring everyone who uses your application to have sudo privileges is not a good thing either.

Jonathan Leffler
You can create a new group for running that command, and set sudo to only allow that group to only run that command.check out visudo
Nerdling
So he could simply ask the users to install using sudo, so that it has the right to copy the files in /usr/bin but still has the correct $HOME value ?
Wookai
+1 for the application creating the hidden directory. That's how most of the apps in linux work.
Sunny
@Nerdling and @Wookai: Yes, you can create new group for each new application, but that is normally considered too onerous. And yes, you could install using sudo, but you still need the application to create the directory when other users than the installer run the program.
Jonathan Leffler
A: 

Must you use $HOME? Maybe you could prompt for the username and install to ~$username instead?

catfood
The application should not need to do that - and the installer should not be futzing with the per-user directory. I recommend removing this; I didn't down-vote, but the response is legitimately down-votable.
Jonathan Leffler