Linux : How to restore default /etc/ config file.
For many cases, the default configuration file is provided by a package directly. In such cases, you can extract the specific file from the package, thus easily recovering the file.
To check if a package provides the file, run dpkg -S
on the full path of the file. For example:
$ dpkg -S /etc/ssh/sshd_config /etc/ssh/ssh_config /etc/sudoers
dpkg-query: no path found matching pattern /etc/ssh/sshd_config
openssh-client: /etc/ssh/ssh_config
sudo: /etc/sudoers
Provided by a package
Method 1
As we can see, /etc/ssh/sshd_config
is not directly provided by any package, but the other two are provided by openssh-client
and sudo
respectively. So, if you wished to recover /etc/ssh/ssh_config
, first get the package:
- Download the package:
apt-get download openssh-client
- Then extract its file system tree data to a
.tar
file:
dpkg-deb --fsys-tarfile openssh-client_1%3a7.9p1-10+deb10u4_amd64.deb > pkg.tar
- Finally extract only that exact configuration anywhere you want it to be:
tar -Oxf pkg.tar ./etc/ssh/ssh_config | sudo tee /etc/ssh/sshd_config
./etc/lightdm/unity-greeter.conf
is the file name in our archive./etc/lightdm/unity-greeter.conf
is where I'm sending it to be stored.
Or we can do steps 2 and 3 with a one-liner:
dpkg-deb --fsys-tarfile sudo_1.9.13p3-1+deb12u1_amd64.deb | sudo tar -x -C / ./etc/sudoers
Method 2
- Find out [what package installed the config file:
$ dpkg -S /etc/nginx
nginx-common: /etc/nginx
As you can see, the name of the package is nginx-common
.
If you deleted a directory, like /etc/pam.d
, you can list every package that added to it by using the directory path:
$ dpkg -S /etc/pam.d
login, sudo, libpam-runtime, cups-daemon, openssh-server, cron, policykit-1, at, samba-common, ppp, accountsservice, dovecot-core, passwd: /etc/pam.d
- Run the following command, replacing
<package-name>
with the name of the package:
sudo apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss" <package-name>
Or run combine it with dpkg -S
sudo apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss" $(dpkg -S /etc/some/directory | sed 's/,//g; s/:.*//')
- If everything worked as expected, you should get a message:
Preparing to unpack .../sudo_1.9.13p3-1+deb12u1_amd64.deb ...
Unpacking sudo (1.9.13p3-1+deb12u1) over (1.9.13p3-1+deb12u1) ...
Setting up sudo (1.9.13p3-1+deb12u1) ...
Configuration file '/etc/pam.d/sudo', does not exist on system.
Installing new config file as you requested.
Configuration file '/etc/pam.d/sudo-i', does not exist on system.
Installing new config file as you requested.
Configuration file '/etc/sudoers', does not exist on system.
Installing new config file as you requested.
Processing triggers for libc-bin (2.36-9+deb12u7) ...