简体   繁体   English

如何从 init bash 脚本 Linux 获取用户(在创建用户 session 之前)

[英]How to get user from init bash script Linux(before user session created)

At Debian im trying to change specific directory ownership after every reboot.在 Debian,我试图在每次重新启动后更改特定的目录所有权。 But in my case it doesnt work, because user always set as root at reboot time.但在我的情况下它不起作用,因为用户总是在重启时设置为 root。 When i try this in terminal it works well (i think because myUser session created) but i need to change it at reboot time.当我在终端中尝试这个时它运行良好(我认为是因为创建了 myUser session)但我需要在重新启动时更改它。

So far i refer this link but it fails for me: https://unix.stackexchange.com/questions/183183/using-chown-useruser-inside-bash-script到目前为止,我引用了这个链接,但它对我来说失败了: https://unix.stackexchange.com/questions/183183/using-chown-useruser-inside-bash-script

CURRENT_USER=$(who | awk 'NR==1{print $1}') CURRENT_USER=$(who | awk 'NR==1{print $1}')
sudo chown -R $CURRENT_USER:$CURRENT_USER /myfile/foo须藤 chown -R $CURRENT_USER:$CURRENT_USER /myfile/foo

second way:第二种方式:

sudo chown -R ${USER:=$(/usr/bin/id -run)}:$USER /myfile/foo sudo chown -R ${USER:=$(/usr/bin/id -run)}:$USER /myfile/foo

i dont want to write like this:我不想这样写:

sudo chown myUser:myUser /myfile/foo sudo chown myUser:myUser /myfile/foo

Linux is a multiuser system. Linux 是一个多用户系统。 There can be more than one users logged in simultaneously or no one at all.可以有多个用户同时登录,也可以根本没有用户登录。 Often there is no way to define something like a "default" user.通常没有办法定义像“默认”用户这样的东西。 There are however workarounds that you could use to find the "default" user if the machines are setup as typical single user company computers.但是,如果机器设置为典型的单用户公司计算机,则可以使用一些变通方法来查找“默认”用户。

  • Take the user with UID 1000. If all the machines are setup so that the "default" user has UID 1000, you could just use this UID instead of the username in the chown command.以 UID 为 1000 的用户为例。如果所有机器都设置为“默认”用户的 UID 为 1000,则您可以只使用此 UID 而不是chown命令中的用户名。
  • Look for the subdirectories of /home .查找/home的子目录。 Only standard (non-system) users have a home directory.只有标准(非系统)用户才有主目录。 On standard systems they are named with the username.在标准系统上,它们以用户名命名。

That beeing set: Your script will be highly dependent on the specific setup of the machines.蜜蜂集:您的脚本将高度依赖于机器的特定设置。 Also there might be a less intrusive, less machine dependent and more secure solution to your problem.此外,对于您的问题,可能会有侵入性较小、机器依赖性较低且更安全的解决方案。 You could try to你可以试试

  • leave root as owner of the directory but enable read and write permissions for other users.将 root 保留为目录的所有者,但为其他用户启用读写权限。
  • set the sticky bit for the directory (just like /tmp).为目录设置粘滞位(就像/tmp)。
  • make a new group that owns the directory and add the users to it.创建一个拥有该目录的新组并将用户添加到其中。

The last option is actually used a lot.最后一个选项其实用的很多。 For example often web servers make their source directories owned by a group called www and users, that are allowed to read/write the data are added to that group.例如,通常 web 服务器将其源目录归一个名为www和用户的组所有,允许读/写数据的用户被添加到该组。 This also works well in real multiuser systems like servers or shared machines.这在真实的多用户系统(如服务器或共享计算机)中也能很好地工作。

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

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