为什么要更新系统

定期更新系统是系统安全性很重要的方面之一。特别是遇到一些突发的安全漏洞,如果不及时更新会导致服务器很容易受到攻击


Ubuntu可以使用unattended-upgrades这个软件进行自动更新

软件安装

sudo apt install unattended-upgrades

启动自动更新

sudo dpkg-reconfigure --priority=low

启动后会自动创建/etc/apt/apt.conf.d/20auto-upgrades文件,内容如下

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

配置自动更新内容

/etc/apt/apt.conf.d/50unattended-upgrades这个配置文件对要更新的范围进行了定义,如果出于对软件兼容性或稳定性的担心,可以配置只进行安全更新 该配置文件内容如下:

// Automatically upgrade packages from these (origin:archive) pairs
//
// Note that in Ubuntu security updates may pull in new dependencies
// from non-security sources (e.g. chromium). By allowing the release
// pocket these get automatically pulled in.
Unattended-Upgrade::Allowed-Origins {
        "${distro_id}:${distro_codename}";
	"${distro_id}:${distro_codename}-security";
	// Extended Security Maintenance; doesn't necessarily exist for
	// every release and this system may not have it installed, but if
	// available, the policy for updates is such that unattended-upgrades
	// should also install from here by default.
	"${distro_id}ESMApps:${distro_codename}-apps-security";
	"${distro_id}ESM:${distro_codename}-infra-security";
//	"${distro_id}:${distro_codename}-updates";
//	"${distro_id}:${distro_codename}-proposed";
//	"${distro_id}:${distro_codename}-backports";
};

// List of packages to not update (regexp are supported)
Unattended-Upgrade::Package-Blacklist {
//	"vim";
//	"libc6";
//	"libc6-dev";
//	"libc6-i686";
};

// This option will controls whether the development release of Ubuntu will be
// upgraded automatically.
Unattended-Upgrade::DevRelease "false";

// This option allows you to control if on a unclean dpkg exit
// unattended-upgrades will automatically run 
//   dpkg --force-confold --configure -a
// The default is true, to ensure updates keep getting installed
//Unattended-Upgrade::AutoFixInterruptedDpkg "false";

// Split the upgrade into the smallest possible chunks so that
// they can be interrupted with SIGTERM. This makes the upgrade
// a bit slower but it has the benefit that shutdown while a upgrade
// is running is possible (with a small delay)
//Unattended-Upgrade::MinimalSteps "false";

// Install all unattended-upgrades when the machine is shutting down
// instead of doing it in the background while the machine is running
// This will (obviously) make shutdown slower
//Unattended-Upgrade::InstallOnShutdown "true";

// Send email to this address for problems or packages upgrades
// If empty or unset then no email is sent, make sure that you
// have a working mail setup on your system. A package that provides
// 'mailx' must be installed. E.g. "[email protected]"
//Unattended-Upgrade::Mail "root";

// Set this value to "true" to get emails only on errors. Default
// is to always send a mail if Unattended-Upgrade::Mail is set
//Unattended-Upgrade::MailOnlyOnError "true";

// Remove unused automatically installed kernel-related packages
// (kernel images, kernel headers and kernel version locked tools).
//Unattended-Upgrade::Remove-Unused-Kernel-Packages "false";

// Do automatic removal of new unused dependencies after the upgrade
// (equivalent to apt-get autoremove)
//Unattended-Upgrade::Remove-Unused-Dependencies "false";

// Automatically reboot *WITHOUT CONFIRMATION*
//  if the file /var/run/reboot-required is found after the upgrade 
//Unattended-Upgrade::Automatic-Reboot "false";

// If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately
//  Default: "now"
//Unattended-Upgrade::Automatic-Reboot-Time "02:00";

// Use apt bandwidth limit feature, this example limits the download
// speed to 70kb/sec
//Acquire::http::Dl-Limit "70";

// Enable logging to syslog. Default is False
// Unattended-Upgrade::SyslogEnable "false";

// Specify syslog facility. Default is daemon
// Unattended-Upgrade::SyslogFacility "daemon";

// Download and install upgrades only on AC power
// (i.e. skip or gracefully stop updates on battery)
// Unattended-Upgrade::OnlyOnACPower "true";

// Download and install upgrades only on non-metered connection
// (i.e. skip or gracefully stop updates on a metered connection)
// Unattended-Upgrade::Skip-Updates-On-Metered-Connections "true";

Unattended-Upgrade::Allowed-Origins { };块中定义的是要更新的内容 Unattended-Upgrade::Package-Blacklist { };块中定义的是不想更新的内容

邮件通知

如果由于某种原因自动更新失败或出现问题,希望可以收到邮件通知,可以启用配置中的以下两行。前提是有正确配置[[Linux发送邮件|mail]]

Unattended-Upgrade::Mail "[email protected]";
Unattended-Upgrade::MailOnlyOnError "true";

自动重启

如果想更新后自动重启,需要安装update-notifier-common。在最小安装中默认不会安装它,如果没有安装,自动更新将不会重启,甚至不会进行邮件通知。同时需要启动如下配置;

Unattended-Upgrade::Automatic-Reboot "true";

检查配置

查看自动更新是否被启用

apt-config dump APT::Periodic::Unattended-Upgrade

如果上面的命令执行后,APT::Periodic::Unattended-Upgrade的值显示为0则表示未启动,如果是其它数值,则表示是对应的天数更新

最后更新于