[Fixed] Ubuntu apt-get upgrade auto restart services

You might have noticed that Ubuntu has since version Ubuntu 22.04 started to show the following message when you install or update software:

Which services should be restarted?

While there might be cases where you have a setup that requires a specific service not to be restarted automatically after an update, in most cases, it's just annoying having to manually select the services for restarting.

In this guide, I will show you how several solutions to tackle that problem and restart services automatically when needed.

Solution 1: Set NEEDRESTART_MODE variable to automatic mode

The first option that I will show you to solve the issue is to set a bash (terminal) variable named NEEDRESTART_MODE to the value "a" before running apt or apt-get commands. If needrestart is set to the value "a", then apt will restart services automatically if required.


sudo NEEDRESTART_MODE=a apt dist-upgrade

The variable is just set for this command now.

Alternatively, you can use the following way to become root user first via sudo and then export the variable in case you have to install multiple applications with apt or apt-get.

sudo -s
export DEBIAN_FRONTEND=noninteractive
apt update -y
apt dist-upgrade -y
... and so on ...

Solution 2: Use noninteractive frontend in apt

The second solution is to use the noninteractive frontend in apt. This is done by setting the command line variable DEBIAN_FRONTEND to the value noninteractive.


sudo DEBIAN_FRONTEND=noninteractive apt dist-upgrade -y

This solution sets the variable just for the command apt dist-upgrade that we run at that time.

Or use this approach when installing multiple software packages:

sudo -s
export DEBIAN_FRONTEND=noninteractive
apt update -y
apt dist-upgrade -y
... and so on ...

Solution 3: Set apt permanently to automatic restart mode

The final solution if you don't want to get bothered again with setting restart mode is to edit the file /etc/needrestart/needrestart.conf and set the variable $nrconf{restart} to the value "a".

Open the configuration file with an editor:

sudo nano /etc/needrestart/needrestart.conf

find the line:

#$nrconf{restart} = 'i';

and remove the # in front of the line and replace the "I" with the value "a". The result should look like this:

$nrconf{restart} = 'a';

Possible values of this variable are:

  • a - Automatic Restart of services
  • i - Interactive mode. The user is asked if services must be restarted
  • l - List mode. Just show a list of which services need to be restarted by the user.

Instead of editing the file, you can just append the variable. This is possible as the original variable is commented out via #.

sudo echo "\$nrconf{restart} = 'a'" >> /etc/needrestart/needrestart.conf

Or use the sed command to edit the file:

sudo sed -i 's/#$nrconf{restart} = '"'"'i'"'"';/$nrconf{restart} = '"'"'a'"'"';/g' /etc/needrestart/needrestart.conf

You have 3 different methods now at hand to solve the issue.

Leave a Comment