{"id":48406,"date":"2025-03-15T13:54:11","date_gmt":"2025-03-15T13:54:11","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=48406"},"modified":"2025-03-16T01:24:15","modified_gmt":"2025-03-16T01:24:15","slug":"restore-deleted-files-under-etc","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2025\/03\/restore-deleted-files-under-etc\/","title":{"rendered":"How to Restore Deleted Files Under \/etc Directory in Ubuntu"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/disk-icon.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-48407\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/disk-icon-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/disk-icon-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/disk-icon-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/disk-icon-700x700.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/disk-icon-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/disk-icon.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>Messed up or mistakenly deleted configuration files under <code>\/etc<\/code> directory, but no backup? Here&#8217;s how to restore the files just like they were originally installed.<\/p>\n<p>In Linux, the <code>\/etc<\/code> directory is the location for storing system-wide configuration files. When installing a software package, it may create and\/or read software specific config files under <code>\/etc<\/code> directory for user custom configurations.<\/p>\n<p><!--more--><\/p>\n<p>To prevent overriding user custom configurations, re-install software packages by default will NOT replace the files under <code>\/etc<\/code> directory, even when you deleted the files under that directory. Though, some packages will ask if keep or replace the old configurations during installation.<\/p>\n<p>If you mistakenly deleted files under <code>\/etc<\/code> or just want to restore files under that directory to original, then this tutorial may help.<\/p>\n<p><b>NOTE: NOT only for Ubuntu, this tutorial should works in all Debian based systems.<\/b><\/p>\n<h3>Step 1: Find out which package includes the file\/files<\/h3>\n<p>In Linux, many applications are built into separated packages. When installing software package <code>xxx<\/code>, it may also install <code>xxx-data<\/code>, <code>xxx-common<\/code>, or <code>xxx-plugins<\/code> etc. as dependencies. The file you want to restore may be included in either one of the packages.<\/p>\n<h4>Option 1: use dpkg command:<\/h4>\n<p>To find out which package includes that file, either open terminal (<b>Ctrl+Alt+T<\/b>) or connect to Debian\/Ubuntu server. Then, try <code>dpkg -S<\/code> command.<\/p>\n<p>For example, search which package includes <code>\/etc\/ssh\/ssh_import_id<\/code>:<\/p>\n<pre>dpkg -S \/etc\/ssh\/ssh_import_id<\/pre>\n<p>Or, you may search which package includes files under given folder (it MAY output multiple packages for single folder).<\/p>\n<pre>dpkg -S \/etc\/ssh<\/pre>\n<p>If you don&#8217;t even remember the file path, then just type filename instead. Even for file-name, you may use asterisk (*) for the part that you don&#8217;t remember.<\/p>\n<pre>dpkg -S ssh_imp*<\/pre>\n<p>The last command will search which package include the file with <code>ssh_imp<\/code> at the beginning of its name.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/dpkg-query-search.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48409\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/dpkg-query-search-700x475.webp\" alt=\"\" width=\"610\" height=\"414\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/dpkg-query-search-700x475.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/dpkg-query-search-300x203.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/dpkg-query-search.webp 730w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>Tips: you may alternatively use <code>apt-file search<\/code> command do the similar job, though it seems that it does not support asterisk in search text.<\/p>\n<h4>Option 2: use ucfq command:<\/h4>\n<p>The <code>dpkg -S<\/code> command sometimes does not work, because the packages you search may be handled by <code>ucf<\/code>\uff08Update Configuration Files).<\/p>\n<p>In the case, you may use <code>ucfq<\/code> instead. For example, search which package include <code>\/etc\/ssh\/sshd_config<\/code>:<\/p>\n<pre>ucfq \/etc\/ssh\/sshd_config<\/pre>\n<p>The command needs to input full-path to the file you want to search. If you don&#8217;t remember, try command below to instead (replace <code>smb<\/code> according to what you want to search):<\/p>\n<pre>grep smb \/var\/lib\/ucf\/registry<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/ucfq-search.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48410\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/ucfq-search-700x313.webp\" alt=\"\" width=\"610\" height=\"273\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/ucfq-search-700x313.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/ucfq-search-300x134.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/ucfq-search.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Step 2: Re-install package &amp; specify to re-create non-exist files<\/h3>\n<p>After finding out the which package includes the file you want to restore, then, run the command below to re-install and re-generate the missing user configuration file:<\/p>\n<pre>sudo UCF_FORCE_CONFFMISS=1 apt -o Dpkg::Options::=--force-confmiss install --reinstall openssh-server<\/pre>\n<p>In the command above, replace the package name <code>openssh-server<\/code> accordingly.<\/p>\n<p>Here both <code>UCF_FORCE_CONFFMISS=1<\/code> and <code>-o Dpkg::Options::=--force-confmiss<\/code> tell to re-generate installation files if they are NOT exist. You may skip the former one, if the package is NOT handled by <code>ucf<\/code>.<\/p>\n<p>In the output, you&#8217;ll see something look like &#8220;Recreating deleted config file &#8230; with new version, as asked&#8221;.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/reinstall-override.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48411\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/reinstall-override-700x348.webp\" alt=\"\" width=\"610\" height=\"303\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/reinstall-override-700x348.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/reinstall-override-300x149.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/reinstall-override-768x381.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/03\/reinstall-override.webp 842w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>If it&#8217;s a local <code>.deb<\/code> package, and you want to use <code>dpkg<\/code> command to install, then run the command below will do the similar things:<\/p>\n<pre>sudo UCF_FORCE_CONFFMISS=1 dpkg -i --force-confmiss package-name.deb<\/pre>\n<p>According to <a href=\"https:\/\/manpages.debian.org\/bookworm\/ucf\/ucf.1.en.html\" target=\"_blank\" rel=\"noopener\">ucf manpage<\/a>, there are also <code>UCF_FORCE_CONFFNEW<\/code> and <code>--force-confnew<\/code> dpkg options to tell to always overwrite the installed destination file, though don&#8217;t know why it does NOT work in my test.<\/p>\n<p style=\"text-align: right;\">via: <a href=\"https:\/\/forum.ubuntu.org.cn\/viewtopic.php?t=494763\" target=\"_blank\" rel=\"noopener\">this thread<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Messed up or mistakenly deleted configuration files under \/etc directory, but no backup? Here&#8217;s how to restore the files just like they were originally installed. In Linux, the \/etc directory is the location for storing system-wide configuration files. When installing a software package, it may create and\/or read software specific config files under \/etc directory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":48407,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2301],"class_list":["post-48406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-admin"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/48406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=48406"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/48406\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/48407"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=48406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=48406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=48406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}