如何恢复配置文件?

我想将LightDM恢复到默认状态,因为出于某种原因
/etc/lightdm/unity-greeter.conf现在是一个空文件。

删除/etc/lightdm/unity-greeter.conf然后运行sudo apt-get install --reinstall unity-greeter不会像您期望的那样创建新的配置文件。

如何恢复丢失的配置文件?

  1. 找出安assembly置文件的软件包 :

     $ dpkg -S unity-greeter.conf unity-greeter: /etc/lightdm/unity-greeter.conf 

    如您所见,包的名称是unity-greeter

    如果删除了目录,例如/etc/pam.d ,则可以使用目录路径列出添加到其中的每个包:

     $ 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 
  2. 运行以下命令,将替换为

     sudo apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss"  

    并用于恢复目录:

     sudo apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss" $(dpkg -S /etc/some/directory | sed 's/,//g; s/:.*//') 
  3. 如果一切按预期工作,您应该收到一条消息:

     Configuration file `/etc/lightdm/unity-greeter.conf', does not exist on system. Installing new config file as you requested. 
  4. 需要重新安装所有PulseAudio配置文件时的实际示例:

     apt-cache pkgnames pulse |xargs -n 1 apt-get -o Dpkg::Options::="--force-confmiss" install --reinstall 

在许多情况下,默认配置文件由包直接提供。 在这种情况下,您可以从包中提取特定文件,从而轻松恢复该文件。

要检查程序包是否提供该文件,请在该文件的完整路径上运行dpkg -S 。 例如:

 $ 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 

由包提供

我们可以看到, /etc/ssh/sshd_config不是由任何软件包直接提供的,而是另外两个分别由openssh-clientsudo提供。 所以,如果你想恢复/etc/ssh/ssh_config ,首先得到包:

 apt-get download openssh-client 

现在,您可以将文件直接提取到其预期位置,或者对于当前目录而不是/目标位置,如果您希望进行比较和对比,或者手动合并它们等等。 对于前者:

 dpkg-deb --fsys-tarfile openssh-client_*.deb | sudo tar x ./etc/ssh/ssh_config -C / 

-C /告诉tar在更改为/后提取,这意味着目标文件将被替换。 如果删除它, tar将提取到当前目录,这意味着./etc/ssh/ssh_config将存在于当前目录中。

如果由于某种原因sudo不起作用,请改用pkexec 。 如果pkexec也不起作用,请重启到恢复模式,mount / as rw 。 如果不起作用……


由包创建

那么/etc/ssh/sshd_config呢? 它似乎没有任何包提供,所以它是如何出现的?

在这种情况下(在许多其他此类情况下,另一个示例是/etc/modules ),文件是在安装时使用软件包维护者脚本创建的。 当由于用户对查询的响应而需要更改配置文件时,通常会执行此操作。 例如,OpenSSH会询问是否应将PermitRootLogin更改为no ,以及其他更新版本。

要识别此类情况,请尝试使用维护者脚本。 通常,您只需要查看postinst ,但如果您没有运气postinst ,请尝试preinst

 grep -l /etc/ssh/sshd_config /var/lib/dpkg/info/*.postinst 

在这种情况下,我们很幸运:

 $ grep /etc/ssh/sshd_config /var/lib/dpkg/info/*.postinst -l /var/lib/dpkg/info/openssh-server.postinst 

只有一个文件匹配,幸运的是,它包含创建默认配置文件的代码:

  cat < /etc/ssh/sshd_config # Package generated configuration file # See the sshd_config(5) manpage for details # What ports, IPs and protocols we listen for Port 22 # Use these options to restrict which interfaces/protocols sshd will bind to #ListenAddress :: #ListenAddress 0.0.0.0 Protocol 2 # HostKeys for protocol version 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecdsa_key #Privilege Separation is turned on for security UsePrivilegeSeparation yes # Lifetime and size of ephemeral version 1 server key KeyRegenerationInterval 3600 ServerKeyBits 768 # Logging SyslogFacility AUTH LogLevel INFO # Authentication: LoginGraceTime 120 PermitRootLogin yes StrictModes yes RSAAuthentication yes PubkeyAuthentication yes #AuthorizedKeysFile %h/.ssh/authorized_keys # Don't read the user's ~/.rhosts and ~/.shosts files IgnoreRhosts yes # For this to work you will also need host keys in /etc/ssh_known_hosts RhostsRSAAuthentication no # similar for protocol version 2 HostbasedAuthentication no # Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication #IgnoreUserKnownHosts yes # To enable empty passwords, change to yes (NOT RECOMMENDED) PermitEmptyPasswords no # Change to yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) ChallengeResponseAuthentication no # Change to no to disable tunnelled clear text passwords #PasswordAuthentication yes # Kerberos options #KerberosAuthentication no #KerberosGetAFSToken no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes X11Forwarding yes X11DisplayOffset 10 PrintMotd no PrintLastLog yes TCPKeepAlive yes #UseLogin no #MaxStartups 10:30:60 #Banner /etc/issue.net # Allow client to pass locale environment variables AcceptEnv LANG LC_* Subsystem sftp /usr/lib/openssh/sftp-server # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the ChallengeResponseAuthentication and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via ChallengeResponseAuthentication may bypass # the setting of "PermitRootLogin without-password". # If you just want the PAM account and session checks to run without # PAM authentication, then enable this but set PasswordAuthentication # and ChallengeResponseAuthentication to 'no'. UsePAM yes EOF 

通常,这是你会看到的(另一个例子, 来自kmod /etc/modules ):

 cat > /path/to/the/file < 

因此,您可以查找此代码并直接从脚本中获取内容。


没有这样的剧本? 您仍然可以尝试浏览相关软件包的文件列表以查看是否有任何命中,但此时,我看不到任何易于推广的方法(在瞬态环境中重新安装,例如chroot或VM或实时USB)。


从长远来看,请将您的配置保持在版本控制之下。 任何值得盐的VCS都可以在这里节省一天,而etckeeper实用程序大大简化了在VCS中保留/etc的任务。

根据Ubuntu论坛上的这个post,它就像在终端中运行以下内容一样简单:

 sudo dpkg-reconfigure lightdm 

找到拥有配置文件的包:

 dpkg --search /etc/path/to/config 

它将输出类似于:

 unity-greeter: /etc/lightdm/unity-greeter.conf 

所以包名是“unity-greeter”,下载包:

 apt-get download unity-greeter 

然后将其文件系统树数据提取到tar文件:

 dpkg-deb --fsys-tarfile unity-greeter_version-0ubuntu1_amd64.deb > pkg.tar 

最终只在你想要的任何地方提取那个确切的配置:

 tar -Oxf pkg.tar ./etc/lightdm/unity-greeter.conf | sudo tee /etc/lightdm/unity-greeter.conf 
  • ./etc/lightdm/unity-greeter.conf是我们档案中的文件名。
  • /etc/lightdm/unity-greeter.conf是我发送它存储的地方。

或者正如@Muru建议的那样,我们可以在一个class轮中完成:

 dpkg-deb --fsys-tarfile unity-greeter_version-0ubuntu1_amd64.deb | sudo tar -x -C / ./etc/lightdm/unity-greeter.conf 

我在Ubuntu 17.04上遇到了同样的问题。 postinstall使用/usr/share/openssh/的模板。 它会检查rootlogin是否已启用,设置此选项并将其复制到/etc/ssh 。 之后它会执行一些ucf和ucfr调用(我不知道那是什么)。

只需将/usr/share/openssh/sshd_config复制到/etc/ssh/sshd_config

 sudo cp /usr/share/openssh/sshd_config /etc/ssh/sshd_config 

现在根据需要调整sshd_config

这不适用于所有配置文件。 对于/etc/nsswitch.conf ,请参见如何还原/重新创建etc / nsswitch.conf文件 。 似乎不可能使用dpkg-reconfigure重建该文件。

删除(返回)文件,然后使用apt-get install --reinstall unity-greeter重新安装unity-greeter apt-get install --reinstall unity-greeter