82

My system configuration script does an apt-get install -y postfix. Unfortunately the script is halted when the postfix installer displays a configuration screen. Is there a method to force postfix to use the defaults during installation so that an automated script can continue to the end?

Does the postfix installer maybe check for existing configuration in /etc/postfix, and if it exists, not bother the user with the configuration screen?

bathyscapher
  • 115
  • 4
sutch
  • 1,006
  • 1
  • 8
  • 9

4 Answers4

110

You can use pre-seeding for this, using the debconf-set-selections command to pre-answer the questions asked by debconf before installing the package.

For example:

debconf-set-selections <<< "postfix postfix/mailname string your.hostname.com"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
apt-get install --assume-yes postfix
nuiun
  • 103
  • 3
raphink
  • 11,987
  • 6
  • 37
  • 48
  • 1
    What are the available options for `main_mailer_type`? Is there a list to reference somewhere without having to run it interactively once first? – beporter Jun 24 '15 at 21:06
  • 3
    beporter, apparently: `No configuration` , `Internet site`, `Internet with smarthost`, `Satellite system`, `Local only` however it seems like Internet Site is generally the best choice for most people: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=450787 – willbradley Jan 09 '16 at 20:16
  • 2
    Could some body explain how can we get this string for other packages? (kerberos in my case): "postfix postfix/mailname string your.hostname.com" I mean, what is the first "postfix", and the second "postfix/" and then "mailname", etc.... where can i get those strings for my package? – Mohammed Noureldin Nov 28 '16 at 23:33
  • I know the question is about Ubuntu but I need this for Amazon Linux (AWS) which is an offshoot of RHEL... Anyone? – TheStoryCoder Dec 04 '18 at 11:52
  • @MohammedNoureldin Looking at `man debconf-set-selections`, I see there's also `debconf-get-selections` which dumps what selections have been made on the current system, in the format used for input here. You could manually setup one system, then just run that to see what to use for kerberos. – morganwahl May 06 '19 at 00:36
  • Is there any way to set the host your.hostname.com by passing a variable? I tired as I have a variable email_host="mail.ma.com" and later sudo debconf-set-selections <<< "postfix postfix/mailname string ${email_host}" . But it doesn't work. how do i do it? – masiboo May 22 '19 at 12:15
  • 3
    I found that [these are all of the postfix settings that come from `debconf-get-selections` in Ubuntu 20.04](https://gist.github.com/gene1wood/e4dd448513cb425b5ec398f95cda2462) – gene_wood Feb 18 '21 at 05:55
  • This is an edge case, but sometimes an automated script runs at exactly the same time as Ubuntu's automated package processes and is not able to get a lock for apt. To make sure that this edge case is always handled properly, the install command should include `-o DPkg::Lock::Timeout=-1` in addition to `-y`. – Utkonos Nov 02 '22 at 22:02
32

If you want this globally:

dpkg-reconfigure debconf

Then configure it to be "noninteractive"

If you just want it for single install run:

DEBIAN_FRONTEND=noninteractive apt-get install PACKAGE
David Rickman
  • 3,320
  • 18
  • 16
  • 3
    You could also do `export DEBIAN_FRONTEND=noninteractive` to set it globally in an unattended fashion. – Mahn Sep 05 '14 at 13:49
  • I found this worked well on Ubuntu 14.04 for a default 'deliver to local /var/mail' setup, whereas the `debconf-set-selections` answer above didn't. – RichVel Apr 28 '17 at 09:00
  • Works for me. I just needed to remember to pass the env to `apt` and not `sudo` (i.e. `sudo DEBIAN_FRONTEND=noninteractive apt install PACKAGE` and *NOT* `DEBIAN_FRONTEND=noninteractive sudo apt install PACKAGE` – Nathan Chappell May 09 '23 at 13:58
1

When executing from a shell that does not offer here-strings (<<<), pipe the answers:

echo "postfix postfix/mailname string my.hostname.example" | debconf-set-selections
echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections
DEBIAN_FRONTEND=noninteractive apt-get postfix
anx
  • 8,963
  • 5
  • 24
  • 48
David Menache
  • 21
  • 1
  • 5
  • This is exactly the same as the already accepted answer. – Gerald Schneider Mar 03 '22 at 14:28
  • 2
    the answer with <<<'s didnt work for me during automated installation via user-data. Instead using echo with a pipe to debconf-set-selections worked. – David Menache Mar 03 '22 at 14:30
  • This is a good point. Perhaps you should add an alternative along with this explanation as an edit to the accepted answer. People may miss it down here. – Utkonos Nov 02 '22 at 22:06
0

Another way it can be done is by creating a file e.g. myconf:

postfix postfix/main_mailer_type  select Internet Site
postfix postfix/mailname          string your.hostname3.com

Then call debconf-set-selections and pass the file as argument:

debconf-set-selections myconf

You can also do like other answers by piping the strings into debconf-set-selections instead:

echo "postfix postfix/main_mailer_type select Internet Site"      | debconf-set-selections
echo "postfix postfix/mailname         string your.hostname3.com" | debconf-set-selections

A way you can use the piping method is if you want to pass them from one server to another as described in the manpage using the sister command debconf-get-selections which is available in debconf-utils:

debconf-get-selections | ssh anotherserver debconf-set-selections

The temporary file where debconf-set-selections stores them is:

/var/cache/debconf/config.dat

You can check inside and it should contain the settings you just set:

Name: postfix/mailname
Template: postfix/mailname
Value: your.hostname3.com
Owners: postfix
Flags: seen

Name: postfix/main_mailer_type
Template: postfix/main_mailer_type
Value:  Internet Site
Owners: postfix
Flags: seen

Then just run the installer and there will be no prompts:

apt install postfix -y

Now if you uninstall postfix:

apt remove postfix --purge

You will notice that there are no more postfix settings at all in /var/cache/debconf/config.dat at all, so if you decide to re-install postfix, and don't run debconf-set-selections again before, you will get the usual prompts.

Wadih M.
  • 1,032
  • 1
  • 10
  • 18