简体   繁体   English

Bash脚本和root帐户的用户主目录(Linux)

[英]Bash scripting and user home from root account (Linux)

I'm writing an install script in bash for an application on Linux. 我正在bash中为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. 该脚本将一些文件复制到/ usr / bin和/ usr / share中,因此它需要由root用户执行,此外,它还在$ HOME目录中为配置文件创建一个隐藏目录。

Here is the problem: if a normal user wants to install the program, he needs to be root. 问题出在这里:如果普通用户要安装该程序,则需要root用户身份。 But if he is root, the $HOME directory will be /root/ instead of /home/username. 但是,如果他是root用户,则$ HOME目录将是/ root /而不是/ home / username。

...and, further, if UserA installs the software, but UserB runs it, UserB won't have the hidden directory under /home/UserB . ...,此外,如果UserA安装了软件,但UserB运行了该软件,则UserB在/home/UserB下将没有隐藏目录。 Also, the hidden directory under /home/UserA will be owned by root, not userA. 同样, /home/UserA下的隐藏目录将归root拥有,而不是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 . 一个可能的替代位置是/usr/local However, even that can require root privileges. 但是,即使那样也可能需要root特权。 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. 但是,要求安装root特权并不是世界末日-对某些人而言是令人讨厌的事情,但并非完全会造成混乱。 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. 但是,要求每个曾经拥有root特权的人都无法正常工作-如果每个使用它的人都需要运行安装程序,那就不好了。

Final point (for now): if you use sudo , it does not change the value of $HOME, even as you acquire root privileges. 最后一点(目前):如果使用sudo ,即使您获得root特权,它也不会更改$ HOME的值。 However, requiring everyone who uses your application to have sudo privileges is not a good thing either. 但是,要求使用您的应用程序的每个人都具有sudo特权也不是一件好事。

Must you use $HOME ? 您必须使用$HOME吗? Maybe you could prompt for the username and install to ~$username instead? 也许您可以提示输入用户名并安装到~$username呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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