Get users home directory when they run a script as root

前端 未结 5 845
离开以前
离开以前 2021-01-31 03:26

I have a sh script that needs to be run as root, however it is run by the end user using sudo. How can I get the users home directory when ~/ points to /root when running with s

5条回答
  •  囚心锁ツ
    2021-01-31 04:06

    $ sudo env |grep USER
    USER=root
    USERNAME=root
    SUDO_USER=glglgl
    

    So you can access $SUDO_USER and ask the system for his homedir with getent passwd $SUDO_USER | cut -d: -f6.

提交回复
热议问题