返回资讯中心
外部精选
软件工程
#开发者实时流

Rabbit Holes: The Secret to Technical Expertise (2019)

Sometimes, the simplest questions take you on exciting journies. This was, in fact, the most powerful and motivating force that got me into doing computery things from a very young age. I would ask a question, how do I …

Hacker News 最新13172026/10/20 · 27 天前30 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

Sometimes, the simplest questions take you on exciting journies. This was, in fact, the most powerful and motivating force that got me into doing computery things from a very young age. I would ask a question, how do I X? And after some poking around I discover that I can't do X without learning about Y and Some Authoritative Resource says you definitely can't do Y without also knowing the arcane black magic of Z. And so on and so forth until I get myself so buried in tangents that at a certain point, I have no choice but to stop and come up for air. Or a potty break and snack.

In the glamorous tech sector, we call these things rabbit holes. Unless you got into tech solely for the money (you monster), it's stuff like this that we nerds live for. It's how we got our start and crucially, it's how we continue to learn and hone our skillset.

But what, you ask, does a rabbit hole look like? And anyway, don't rabbits live in dens or burrows? First of all, nobody asked you to critique the metaphor. Second, I'll show you. This isn't the deepest or most complex rabbit hole that I've stumbled down but it is recent and that counts for something when I'm itching to write something. Please feel free to follow along on your own instance of Ubuntu 18.04 if you have one handy. When you log into such a host, you are greeted with 27 lines of this here nonsense:

[jayne:~]$ ssh [email protected] Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1044-aws x86_64)

System information as of Sat Aug 17 01:05:15 UTC 2019

System load: 0.0 Processes: 87 Usage of /: 13.7% of 7.69GB Users logged in: 0 Memory usage: 14% IP address for eth0: 127.0.0.1 Swap usage: 0%

  • Keen to learn Istio? It's included in the single-package MicroK8s.

https://snapcraft.io/microk8s

  • Canonical Livepatch is available for installation.

0 packages can be updated. 0 updates are security updates.

Last login: Fri Aug 16 23:40:01 2019 from 127.0.0.1

The first time you log into an Ubuntu host, all of this is very flashy and impressive. You think, wow, this looks like Serious Business. But right around the 427th time, it's just noise. Then at some inconvenient time in the future--when something on that host breaks badly and you have to endure that crap before you get to your shell prompt--you really hope that one day you can meet the guy who programmed it on the outside chance you might be able to covertly spike his coffee with a large dose of laxative.

In these 27 lines (which is three longer than the default terminal height, mind you), we have:

Somebody somewhere obviously thought that every piece here would be vital information to somebody else. But let's see about how we can pare this down to something managable. Or in the worst case, wipe it out altogether.

I happen to know, from my decades of previous BSD/Linux rabbit holes, that messages which are printed after you log in usually come from the file /etc/motd. "motd" by the way, stands for Message of the Day. In the Olden Days it was a way for administrators to tell users about important things about the system or communicate news, like, "Hey everyone, the print server is down again. We think Phil broke it." On Debian and Ubuntu, most every configuration file has a man page so let's check out that lead with man motd:

DESCRIPTION The contents of /etc/motd are displayed by pam_motd(8) after a success‐ ful login but just before it executes the login shell.

The abbreviation "motd" stands for "message of the day", and this file has been traditionally used for exactly that (it requires much less disk space than mail to all users).

On Debian GNU/Linux, dynamic content configured at /etc/pam.d/login is also displayed by pam_exec.

FILES /etc/motd /etc/pam.d/login

Great, so if the man page is right, all that crap should be festering in /etc/motd, let's take a look:

ubuntu@ip-127-0-0-1:~$ cat /etc/motd cat: /etc/motd: No such file or directory

Well, bugger. That certainly didn't pan out. Moreover, we've learned an important lesson: man pages can lie. Or at the very least, often don't tell the whole truth. Here is where we can try an experiment. What happens if we write something to /etc/motd? Will that replace the default Ubuntu MOTD? Could we be so lucky? Let's make ourselves root and see:

echo "Goodbye, Cruel World" > /etc/motd

And upon logging in again:

[jayne:~]$ ssh [email protected] Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1044-aws x86_64)

System information as of Sat Aug 17 01:09:15 UTC 2019

System load: 0.0 Processes: 87 Usage of /: 13.7% of 7.69GB Users logged in: 0 Memory usage: 14% IP address for eth0: 127.0.0.1 Swap usage: 0%

  • Keen to learn Istio? It's included in the single-package MicroK8s.

https://snapcraft.io/microk8s

  • Canonical Livepatch is available for installation.

0 packages can be updated. 0 updates are security updates.

Goodbye, Cruel World Last login: Fri Aug 16 23:45:01 2019 from 127.0.0.1

Stellar. Now there are 28 lines of crap! But at least we know /etc/motd does something. Sigh. And we also know that /etc/ is where a bunch of system configuration lives, so maybe there's something else in there that has to do with the MOTD. Let's use grep:

It turns out there's quite a lot! There are some promising leads here, but skimming over the list of possibilities, the last few lines catch my attention the most since those files are in a directory called /etc/update-motd.d. These are the files in it:

root@ip-127-0-0-1:/etc/update-motd.d# ls -1 /etc/update-motd.d/ 00-header 10-help-text 50-landscape-sysinfo 50-motd-news 80-esm 80-livepatch 90-updates-available 91-release-upgrade 95-hwe-eol 97-overlayroot 98-fsck-at-reboot 98-reboot-required

That's interesting: ignore a few of these and these files start to look a lot like our MOTD outline from earlier. Let's see what's in the first one. Omitting the comments and a bit of variable-setting code for brevity, we get:

printf "Welcome to %s (%s %s %s)\n" "$DISTRIB_DESCRIPTION" "$(uname -o)" "$(uname -r)" "$(uname -m)"

And the next one, 10-help-text contains mostly this:

printf " * Documentation: https://help.ubuntu.com\n" printf " * Management: https://landscape.canonical.com\n" printf " * Support: https://ubuntu.com/advantage\n"

It's as close to a smoking gun as we're likely to see: These definitely appear to match the first few lines of the MOTD. Given that the file names contain an integer prefix, we can guess that probably all the scripts in this directory are executed sequentially and their output blurted out to us when we log in.

But by what? A normal, perfectly sane person would stop here and say, "Well now the rest is easy, you just delete or modify those scripts and Roberta's-yer-auntie!" And you would be right. But who said I was a normal, perfectly sane person? Certainly not Roberta. Let's look at disabling the most obnoxious thing in the MOTD, the Ubuntu news, announcements, and astrology section. I recall that this part changes from time to time so it must be phoning home to Canonical. As a refresher, here's what it says today:

  • Keen to learn Istio? It's included in the single-package MicroK8s.

https://snapcraft.io/microk8s

Today, I'm mostly keen to learn how the f to get rid of this message. By perusing a few of the files, I suspect that 50-motd-news is likely our guy. Not least of all because there's a URL in it: https://motd.ubuntu.com And if we go to that URL:

root@ip-127-0-0-1:~# curl https://motd.ubuntu.com

  • Keen to learn Istio? It's included in the single-package MicroK8s.

https://snapcraft.io/microk8s

So the /etc/update-motd.d/50-motd-news script is what's printing ads into our young impressionable eyeballs. And again we dive deeper into the rabbit hole because we want to know how. I'll spare you the boring details of the full code listing but basically it's doing this:

This tells us that 1) there is a way to disable this part of the MOTD easily by editing /etc/default/motd-news and 2) the output of this script is fetched and then cached somewhere. If we run the script, we get the same output as when we fetched the contents of the URL a moment ago. And if we print the contents of /var/cache/motd-news, we get the same thing as well:

root@ip-127-0-0-1:~# /etc/update-motd.d/50-motd-news

  • Keen to learn Istio? It's included in the single-package MicroK8s.

https://snapcraft.io/microk8s root@ip-127-0-0-1:~# cat /var/cache/motd-news

  • Keen to learn Istio? It's included in the single-package MicroK8s.

https://snapcraft.io/microk8s

So this pretty much confirms our broad understanding of how this is all working. Pop the cork off the champagne, we figured it out! Woohoo!

But wait... what's that --force flag all about? What calls this script with it? (Not the login process, as far as we know.) And how does /var/cache/motd-news ever get updated with new content? And we still don't know what part of the login process is running the scripts in /etc/update-motd.d anyway? These are questions that we could certainly live without knowing but deep down we know that it will be a hollow, meaningless existence unless we find out. It's not champagne time yet, I'm afraid.

Taking another close look at the /etc/update-motd.d/50-motd-news file, this comment catches the eye:

If we've made it here, we've been given the --force argument,

probably from the systemd motd-news.service. Let's update...

"The systemd motd-news.service," you say? As in, something else besides the login process calls this script? Weirder things I have seen. Let's look for that service:

root@ip-127-0-0-1:~# systemctl list-unit-files | grep motd motd-news.service static motd.service masked motd-news.timer enabled

What we have here is a motd-news service and timer. There's also an motd.service but it's masked which means it's quite definitely not doing anything. I'm curious to know what that is for but the other two are what I'm interested in at the moment.

The Old Way of running jobs on a Unix system at specific intervals is through the cron daemon. You specify the interval as a sequence of fields and the job to run, and then crond takes care of the rest. It's a simple, elegant system and has been serving us well for decades. Systemd, which has good parts and bad parts and insane parts, has come up with a replacement for cron called timers. Long story short, timers are like cron jobs but with a lot more options for scheduling and management. Their configuration is also quite a bit more complex as a result. Another difference is that timers don't execute commands directly, instead you have to define a systemd service which is then triggered by a timer.

Anyway, on Ubuntu the unit files live in /lib/systemd/system so that's where we find the motd-news.* units. Let's look at the timer first.

[Unit] Description=Message of the Day

[Timer] OnCalendar=00,12:00:00 RandomizedDelaySec=12h Persistent=true OnStartupSec=1min

[Install] WantedBy=timers.target

The Description field confirms that motd doesn't actually stand for "Melon of the Deep" and the OnCalender and RandomizedDelaySec fields are what tells systemd to fire the timer once at some random time once within every 12-hour period. We don't see the service unit listed here because according to the man page for timers, a timer will by default trigger a service file of the same name. So let's crack open motd-news.service, then:

[Unit] Description=Message of the Day After=network-online.target Documentation=man:update-motd(8)

[Service] Type=oneshot ExecStart=/etc

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →