[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.

Example:

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.

Example:

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.

How to use apt in Ubuntu to upgrade your system

In this final chapter, I will answer some basic topics about apt updates and upgrades.

What is the difference between apt upgrade vs dist-upgrade

If you're looking to upgrade your system without introducing major changes, then apt upgrade is the best option. apt upgrade or apt-get upgrade will update all packages that are currently installed to their new version and will keep configurations untouched. On the other hand, if you want to upgrade your system including introducing major changes and configuration changes, then apt dist-upgrade or apt-get dist-upgrade should be used instead. This command also ensures that dependencies are up-to-date and potentially necessary packages are removed or added as necessary.

What is the difference between apt-get upgrade and apt-get update

apt-get upgrade and apt-get update are both commands used to update existing packages (installed packages) on a Linux system, however, they are not the same. apt-get update only updates the package list from the remote repository while apt-get upgrade will actually download and install the latest version of packages that have been updated. instead of apt-get, you can also use just apt, and always prepend the sudo command when you are not logged in as root user. Example:

sudo apt update
sudo apt upgrade

What is apt-get force upgrade

apt-get force upgrade is an optional flag that can be used with apt upgrade or apt dist-upgrade in order to force the installation of packages, even if they already exist on the system. It is mainly intended to help with installing packages on systems where different versions were installed at different times, but it could also be useful in other scenarios.

Leave a Comment