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

2

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, setup.sh. In this file, I tried to get the login name of the user using several methods including:

USER=$(logname)
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?

Mustafa Orkun Acar

Posted 2014-10-12T15:42:18.703

Reputation: 173

Answers

0

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.

user1686

Posted 2014-10-12T15:42:18.703

Reputation: 283 655

1The reason why I am trying to achieve this is my application is a parental controls application which should be installed for just one user. – Mustafa Orkun Acar – 2014-10-12T19:33:32.790