Get login name in postinst script as .deb file is being installed by Ubuntu Software Center

Posted on

Problem :

I developed an application for Debian and created .deb installation package using equivs-build. The control file which I used to create package has a line running a postinst script, In this file, I tried to get the login name of the user using several methods including:

USER=$(who am i | awk '{print $1}')

These ways work properly when I try them as root in a terminal window. Unfortunately, they all failed during installation and login user name is retrieved as “root”. This leads all the paths in the script to be wrong and script exits with an error, causing Software Center to show an error message and stopping installation.

However, using dpkg to install the package as below, my application was installed successfully.

dpkg -i package-name

So, why this is happening? How to get login name properly?

Solution :

What you’re trying to do cannot possibly work right.

.deb packages are not private to each user – they’re all installed system-wide, which is why the ‘logname’ output shows “root” – therefore they must work identically for all users, and cannot have anyone’s home directory hardcoded into the system-wide configuration files.

Remember that Linux is a multi-user operating system – there can be multiple user accounts created, and even logged in at the same time. So if your package does this, then it’ll only work for whoever installs it, but will become impossible to use for everyone else on that computer!

The difference between dpkg and Software Center here is that the former performs all actions and runs scripts directly, so the postinst scripts are still running within the user’s login session. However, the Software Center delegates actual installation to a background service which runs “outside” any login sessions, and doesn’t interact with users in any way.

Leave a Reply

Your email address will not be published. Required fields are marked *