[英]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.
但是,如果机器设置为典型的单用户公司计算机,则可以使用一些变通方法来查找“默认”用户。
chown
command.chown
命令中的用户名。/home
./home
的子目录。 Only standard (non-system) users have a home directory. 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
你可以试试
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.