Explain DEBIAN_FRONTEND apt-get variable for Ubuntu / Debian

last updated in Categories , , ,

Can you explain DEBIAN_FRONTEND apt or apt-get variable? How and why I need to use this in Docker or shell scripts under a Debian and Ubuntu Linux?

Debian, Ubuntu, and clones use the Debian package configuration system called debconf. One can use debconf before packages installed onto your Linux desktop or cloud server. For example, one can configure the apt command or apt-get command using various options for sysadmin tasks. This page explains DEBIAN_FRONTEND environment variable.

Advertisements

Explain DEBIAN_FRONTEND apt-get variable

One of debconf’s unique features is that the interface it presents to you is only one of many that can be swapped in at will. There are many debconf frontends available:

  1. dialog – The default frontend for apt/apt-get under Debian/Ubuntu Linux. It displays questions to you. It works in text mode over ssh based session.
  2. readline – The most traditional frontend, this looks quite similar to how Debian configuration options are: a series of questions, printed out at the console using plain text. Best suited when you are working with slow remote connections and entirely comfortable with Linux command-line options.
  3. noninteractive – You use this mode when you need zero interaction while installing or upgrading the system via apt. It accepts the default answer for all questions. It might mail an error message to the root user, but that’s it all. Otherwise, it is totally silent and humble, a perfect frontend for automatic installs. One can use such mode in Dockerfile, shell scripts, cloud-init script, and more.
  4. gnome – This is a modern X GUI using the gtk and gnome libraries.
  5. kde – Another frontend provides a simple X GUI written with the Qt library. It fits well the KDE desktop.
  6. editor – This is for those fanatics who have to do everything in a text editor. It runs your editor on a file that looks something like a typical unix config file, and you edit those files to communicate with debconf.
  7. web – This frontend acts as a web server, that you connect to with your web browser, to browse the questions and answer them. Again it is a proof of concept, and one should avoid using web frontend for security reasons.

How to use apt DEBIAN_FRONTEND environment variable

The syntax is as follows:
DEBIAN_FRONTEND={name_here} apt-get install pkg
DEBIAN_FRONTEND={name_here} apt install pkg
DEBIAN_FRONTEND=noninteractive apt-get -y update
DEBIAN_FRONTEND=noninteractive apt-get -y upgrade

For longer scripts and other swapping process you may want to export shell variable:
export DEBIAN_FRONTEND=noninteractive
apt -y install nginx
apt-get -y update
apt-get -y upgrade

AWS EC2/Lightsail example

One can run script using Linode or AWS or service providers cloud-init system. In other words add a shell script that will run on your instance the first time it launches on AWS Lightsail:
Ubuntu Debian DEBIAN_FRONTEND=noninteractive Linux script on AWS EC2
For EC2 run aws cli as follows:
aws ec2 run-instances --image-id ami-abcd1234 --count 1 \
--instance-type m3.medium --key-name my-ssh-key \
--subnet-id subnet-abcd1234 --security-group-ids sg-abcd1234 \
--user-data file://ubuntu-server-script.txt

Stackscripts

You can write and use a script to configure your VM when it starts up. These scripts can add software, update software, or configure your instance in some other way on Linode:
Explain DEBIAN_FRONTEND apt-get in Debian or Ubuntu Linux on Linode

Sample shell script

The following script force apt-get to skip any interactive post-install configuration steps and do other stuff too before we can use Ansible for other stuff:

#!/bin/bash
## Tested for AWS only. May need modification and subnet for other cloud hosting providers
## Author: Vivek Gite, under GPL v2.+ {https://www.cyberciti.biz/}
## ------------------------------------------------------------------------------------------
## admin_IP_1|short_description_1 admin_IP_2|short_description_2
_admin_ip="1.2.3.4|MUM_ADM 3.2.4.5|DEL_IDC 6.7.8.9|VPN_SG_1"
 
## set server hostname, we can get it from gcp/aws conf too
_hostname="server1.cyberciti.biz"
 
## update system when vm creted via cloud-init ##
export DEBIAN_FRONTEND=noninteractive
apt-get -y update
apt-get -y upgrade
 
## set hostname ##
hostnamectl set-hostname "${_hostname}"
 
## get security settings ##
wget -q -O /etc/sysctl.d/1000-custom.conf https://www.cyberciti.biz/files/1000-custom.conf
 
## enable firewall ##
yes | ufw enable 
 
## open ssh port to our admin ips only ##
for e in $_admin_ip
do 
  ufw allow from "${e%%|*}" to any port 22 proto tcp comment 'Open SSH port for ${e##*|}'
done
 
## extra rule allow vpc 
ufw allow from 172.26.0.0/16 to 172.26.0.0/16 proto any comment 'Allow communitcation between AWS VPC peers'
ufw allow from 172.31.0.0/16 to 172.26.0.0/16 proto any comment 'Allow communitcation between AWS VPC peers'
 
## sync and reboot vm ##
sync
reboot
 
## Rest config will be done by Ansbile ##

Conclusion

You learned how to use DEBIAN_FRONTEND to change the frontend debconf uses temporarily. See debconf and apt-get man pages for more information.
man 7 debconf
man 8 apt
man 8 apt-get

ADVERTISEMENTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!