如何恢复配置文件?

Isa*_*iah 130 configuration package-management apt

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

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

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

Isa*_*iah 194

  1. 找出安装了配置文件的包

    $ dpkg -S unity-greeter.conf
    unity-greeter: /etc/lightdm/unity-greeter.conf
    
    Run Code Online (Sandbox Code Playgroud)

    如您所见,包的名称是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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 运行以下命令,替换<package-name>为包的名称:

    sudo apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss" <package-name>
    
    Run Code Online (Sandbox Code Playgroud)

    并恢复目录:

    sudo apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss" $(dpkg -S /etc/some/directory | sed 's/,//g; s/:.*//')
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果一切正常,您应该收到一条消息:

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

    apt-cache pkgnames pulse |xargs -n 1 apt-get -o Dpkg::Options::="--force-confmiss" install --reinstall 
    
    Run Code Online (Sandbox Code Playgroud)

  • 未在 Ubuntu 上进行测试,但在 Debian 下要恢复由 `ucf` 管理的配置文件,`--force-confmiss` 选项将不起作用,您必须使用 `sudo UCF_FORCE_CONFFMISS=1 apt-get --reinstall install [包名]`。 (6认同)
  • 我将此答案转换为一个小脚本:https://gist.github.com/jameshfisher/10547691 (5认同)
  • `--force-confask` 没有删除的额外好处是它提供了显示您的更改与原始更改之间的差异。 (5认同)
  • 谢谢; 多年来我一直在想这个问题。不过,它确实应该更容易指定 - 是否有某个地方为此打开了错误? (4认同)
  • 要检查所有系统文件是否损坏/存在,请运行 `sudo debsums -a -s`。这将打印所有已安装软件包中丢失或更改的所有文件,包括配置文件。请注意,它会显示您更改的任何配置文件,因此仅显示文件并不一定意味着出现问题。 (2认同)

mur*_*uru 38

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

要检查包是否提供文件,请在文件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
Run Code Online (Sandbox Code Playgroud)

由包提供

可以看到,/etc/ssh/sshd_config不是由任何包直接提供,而是另外两个分别由openssh-client和提供sudo。因此,如果您想恢复/etc/ssh/ssh_config,请先获取软件包:

apt-get download openssh-client
Run Code Online (Sandbox Code Playgroud)

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

dpkg-deb --fsys-tarfile openssh-client_*.deb | sudo tar x ./etc/ssh/ssh_config -C /
Run Code Online (Sandbox Code Playgroud)

-C /通知tar更改为后提取/,这意味着该目标文件将被替换。如果删除它,tar会解压到当前目录,意思是./etc/ssh/ssh_config会存在于你的当前目录中。

如果由于某种原因sudo不起作用,请pkexec改用。如果pkexec也不起作用,请重新启动到恢复模式,挂载/rw. 如果不起作用...


由包创建

怎么样/etc/ssh/sshd_config?它似乎没有由任何包提供,那么它是如何出现的?

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

要识别此类情况,请尝试通过维护者脚本进行 grep。通常,您只需要查看postinst,但如果您对 没有任何运气postinst,也可以尝试preinst

grep -l /etc/ssh/sshd_config /var/lib/dpkg/info/*.postinst
Run Code Online (Sandbox Code Playgroud)

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

$ grep /etc/ssh/sshd_config /var/lib/dpkg/info/*.postinst -l
/var/lib/dpkg/info/openssh-server.postinst
Run Code Online (Sandbox Code Playgroud)

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

    cat <<EOF > /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
Run Code Online (Sandbox Code Playgroud)

通常,您会看到以下内容(另一个示例,/etc/modules来自kmod):

cat > /path/to/the/file <<EOF
# default contents
EOF
Run Code Online (Sandbox Code Playgroud)

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


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


从长远来看,请将您的配置置于版本控制之下。任何物有所值的 VCS 都可以在这里节省一天,并且etckeeper实用程序大大简化了保存/etc在 VCS 中的任务。


Rav*_*ina 9

  1. 找到拥有配置文件的包:

    dpkg --search /etc/path/to/config
    
    Run Code Online (Sandbox Code Playgroud)

    它将输出类似以下内容:

    unity-greeter: /etc/lightdm/unity-greeter.conf
    
    Run Code Online (Sandbox Code Playgroud)

    所以包名是unity-greeter.

  2. 下载包:

    apt-get download unity-greeter
    
    Run Code Online (Sandbox Code Playgroud)
  3. 然后将其文件系统树数据提取到.tar文件中:

    dpkg-deb --fsys-tarfile unity-greeter_version-0ubuntu1_amd64.deb > pkg.tar
    
    Run Code Online (Sandbox Code Playgroud)
  4. 最后仅在您想要的任何位置提取确切的配置:

    tar -Oxf pkg.tar ./etc/lightdm/unity-greeter.conf | sudo tee /etc/lightdm/unity-greeter.conf 
    
    Run Code Online (Sandbox Code Playgroud)
    • ./etc/lightdm/unity-greeter.conf是我们存档中的文件名。
    • /etc/lightdm/unity-greeter.conf是我发送它来存储的地方。

或者正如 @Muru 建议的那样,我们可以用一行代码完成步骤 3 和 4:

dpkg-deb --fsys-tarfile unity-greeter_version-0ubuntu1_amd64.deb | sudo tar -x -C / ./etc/lightdm/unity-greeter.conf
Run Code Online (Sandbox Code Playgroud)

  • 如果您只是将其恢复回其所属位置,请执行`dpkg-deb --fsys-tarfile unity-greeter_version-0ubuntu1_amd64.deb | sudo tar x -C / ./etc/lightdm/unity-greeter.conf`,因此`tar`在解压之前会cd到`/`。 (3认同)