简体   繁体   中英

Bash scripting and user home from root account (Linux)

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.

...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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM