简体   繁体   中英

Debian Package Creation postinst as non-root

I have created quite a few deb files, i have no problems doing that and they all run beautifully. However, if i want to replace a file in users home directory I am unsure on how to do that.

I have tried making a postinst to rsync the files from a predefined location to home directory, but since the postinst file is being run as root ( due to the debian installer running as root ) it is being sent to the root home directory and not the user's home directory....

Here's an example of the deb file contents :

Debian Directory ---> Control File ----> Postint File

usr/share/desktop (directory with files inside)

The postinst file has the sync command to send those files to users home:

#!/bin/sh
rsync -av /usr/share/desktop/ ~/.config/desktop/

The problem is it is sending the files to Root/home... not the default users home :(

I don't have the username of the user since this will be used on many computers with different users, therefore I can't use sudo -u username .

So what do I do? how do i replace files in users home directory from deb install? Any help is much appreciated.

In a Bash script, ~ refers to the current user's home directory. The package installation scripts are always run as root, so that's what "current user" means in this context.

(You could argue that the package installation is probably initiated by a user running su or sudo , but in the general case, you cannot assume this to be the case.)

Modifying user files from a system package appears extremely suspicious in any event. If the need is genuine, this should probably not be approached as a system package installation question in the first place. What are you actually trying to accomplish?

Not only are you violating the basic principle that package management should not meddle with user files; a consequence of this arrangement is that the operation can only be performed once: If the user has installed the package, attempting to install it again does nothing (at least until you uninstall).

A more manageable and predictable approach would seem to be making the package provide this functionality, but leave it to the user to invoke the actual sync (overwriting) script as needed. Perhaps you want to hook it into the desktop startup scripts somehow.

Having said that, sudo exposes the invoking user's identity in $SUDO_USER so you could look for that, and simply fail if it is not set.

As an aside, package scripts should work with dash so you need to avoid bashisms - prefer $HOME over ~ , for example.

I managed to find a workaround, although it is not exactly what I was looking for, but here is my solution, at least for now.

#!/bin/sh
#This will move the desktop settings to required folder.

szAnswer=$(zenity --entry --text "Enter your login username\nThis must be entered correctly\n" --entry-text "Enter name of profile to use:")
xfce4-terminal -e "sudo rsync -av /usr/share/Desktop/ /home/$szAnswer/.config/xfce4/" 

exit 0

In other words, the user gets asked to enter his username, and the files get copied to that user's home directory. The advantage is that if he does have multiple users, it will use the correct user. The disadvantage is if he enters username wrong, even a spelling mistake, the install will fail.

But it does work, I have tested. If anyone has a better solution I eagerly await your suggestions.

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