本文所列内容在Linode和Vultr的VPS上面均测试通过,Debian 7系统。
Windows有自动更新,其实Debian也可以的,小伙伴们跟我来:
这个教程告诉你怎样设置Deban 7进行每天自动更新。
1 配置系统为无人值守更新
为了知晓自动更新的情况,需要配置一下系统邮件,所以需要同时安装bsd-mailx,命令:
apt-get install unattended-upgrades bsd-mailx
你可以通过修改/etc/apt/apt.conf.d/50unattended-upgrades文件来配置自动更新
nano /etc/apt/apt.conf.d/50unattended-upgrades
一般情况下,默认配置是ok的,不过你也可以手工配置一下,比如更新源(默认是stable和security),也可以设置黑名单,让某些软件不被更新,如果更新遇到问题,可以让系统向你发邮件,也可以让系统更新后自动重启,双斜杠是注释语句。内容如下:
// Automatically upgrade packages from these origin patterns
Unattended-Upgrade::Origins-Pattern {
// Archive or Suite based matching:
// Note that this will silently match a different release after
// migration to the specified archive (e.g. testing becomes the
// new stable).
// "o=Debian,a=stable";
// "o=Debian,a=stable-updates";
// "o=Debian,a=proposed-updates";
"origin=Debian,archive=stable,label=Debian-Security";
};
// List of packages to not update
Unattended-Upgrade::Package-Blacklist {
// "vim";
// "libc6";
// "libc6-dev";
// "libc6-i686";
};
// 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 SIGUSR1. 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 "true";
// Install all unattended-upgrades when the machine is shuting 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. "user@example.com"
//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";
// 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 a
// the file /var/run/reboot-required is found after the upgrade
//Unattended-Upgrade::Automatic-Reboot "false";
// Use apt bandwidth limit feature, this example limits the download
// speed to 70kb/sec
//Acquire::http::Dl-Limit "70";
然后创建文件/etc/apt/apt.conf.d/02periodic来启用自动更新
nano /etc/apt/apt.conf.d/02periodic
内容:
// Enable the update/upgrade script (0=disable) APT::Periodic::Enable "1"; // Do "apt-get update" automatically every n-days (0=disable) APT::Periodic::Update-Package-Lists "1"; // Do "apt-get upgrade --download-only" every n-days (0=disable) APT::Periodic::Download-Upgradeable-Packages "1"; // Run the "unattended-upgrade" security upgrade script // every n-days (0=disabled) // Requires the package "unattended-upgrades" and will write // a log in /var/log/unattended-upgrades APT::Periodic::Unattended-Upgrade "1"; // Do "apt-get autoclean" every n-days (0=disable) APT::Periodic::AutocleanInterval "7";
ok了,自动更新将以cron job脚本的形式从/etc/cron.daily/apt运行。
这个脚本会将日志记录在/var/log/unattended-upgrades/unattended-upgrades.log里面,像下面的一样,有详细信息:
2014-07-07 17:49:30,878 INFO Initial blacklisted packages:
2014-07-07 17:49:30,880 INFO Starting unattended upgrades script
2014-07-07 17:49:30,880 INFO Allowed origins are: ["('Debian', 'stable')", "('Debian', 'wheezy-security')"]
2014-07-07 17:50:05,222 INFO Packages that are upgraded: apache2 apache2-doc apache2-mpm-prefork apache2-utils apache2.2-bin
apache2.2-common apt apt-utils base-files dpkg dselect file gnupg gpgv
libapt-inst1.5 libapt-pkg4.12 libc-bin libc6 libc6:i386 libcurl3:i386
libdbus-1-3 libdbus-1-3:i386 libgnutls26 libgnutls26:i386 libjbig0:i386
libmagic1 libnspr4:i386 libnspr4-0d:i386 libsnmp-base libsnmp15 libssl1.0.0
libssl1.0.0:i386 libwbclient0 libxml2 libxml2:i386 locales locales-all
multiarch-support openssh-client openssh-server openssl samba samba-common
snmp tzdata wget whois
2014-07-07 17:50:05,225 INFO Writing dpkg log to '/var/log/unattended-upgrades/unattended-upgrades-dpkg_2014-07-07_17:50:05.224836.log'
2014-07-07 17:51:51,290 INFO All upgrades installed2 让系统发送可用更新的通知
这个,需要另个软件包来完成,安装apticron,命令:
apt-get install apticron
然后配置的话,修改/etc/apticron/apticron.conf:
nano /etc/apticron/apticron.conf
需要修改的是接收通知的邮箱,可以用多个,空格分割:
EMAIL="root@localhost root@tingtao.org"
然后,就没有然后了,像Windows一样每天更新了。
