Running out of time

In this post, I said that Linux keeps time by counting seconds. So, here’s a good question: Is there a number that Linux can’t count past? I mean, if you sat there and counted, you could keep going and going and going until you fell asleep. Linux doesn’t actually fall asleep, it keeps counting… until it can’t. We’ll call it the ‘Running out of Time’ problem.

It’s not like when the good guy runs out of time trying to stop the bomb and everything explodes.

Time is Running Out

Running out of Time

Your system isn’t going to explode! I promise. It just runs out of ways to keep counting.

Most systems today are 32 bit systems or 64 bit systems. That 32-bit (or 64-bit) refers to several different internal operations of your system. We’re going to talk about what it means in terms of numbers. If your system is a 32 bit system, that means the largest number it can store is 2^32. That’s 4294967296. That looks like a big number, doesn’t it? It seems like it’d take a while to count that high. If your system is a 64 bit system, the largest number it can store is 2^64. That’s 2^32 squared or 4294967296 squared. That’s.. a REALLY big number.

The running out of time problem has to do with 32 bit systems.  On January 19, 2038, the number of seconds from January 1, 1970 is going to be larger than 4294967296.  Your 32 bit system isn’t going to be able to count any higher than that and, well, interesting things will happen, or won’t happen.

Running out of Time

Running out of Time

Your 64-bit system doesn’t have that problem, since it can count so much higher than your 32-bit system.

Is there a solution for this problem? Well, no. There’s a lot of smart people working on it though. Those smart people don’t include Tony Stark or Dr. Strange or even Batman, but they’re going to figure it out. Hopefully before January 19, 2038.

Linux® Time

So let’s talk about Linux® and time again. We’ve talked about the calendar, we’ve talked about the Network Time Protocol, but let’s talk about how Linux® stores it.

Everyone seems to have their own way they want to write the date. In Europe, they like to lis t the date first but in America, they like to list the month first. Some people even like to list the year first. And that’s just the date. Now there’s the time! There is the standard of hour, minute, second, but what if you want to write year, hour, month, minute, day, and then seconds? There’s no reason you shouldn’t, right?

Well, keeping track of all of those choices isn’t something Linux® should have to do. Instead, it keeps time by counting seconds, and you can take those seconds and make them look however you want.

Linux® had to start counting seconds at some point, and they chose to start at January 1, 1970.  That’s weird, right?  Linux® wasn’t released until 1991, why did they choose 1970?

Well, I could spin an awesome story about how that’s the instant the first real computer was turned on, or that Iron Man first turned his suit on, or Batman decreed it to be the start of the new era (or new new era), but that would be wrong. The real story isn’t that it’s the birthday of Unix (the Operating System that Linux® mimics), the real story is that it looked pretty.

Computers used to keep time on the sixteenth of a second. This caused problems, because that meant the computer could only tell time for a couple of years before it couldn’t store it anymore. The decision was made that ‘gee, we should use seconds, that’ll work better’. (There was more discussion about this. Imagine the Avengers and the Justice League trying to agree on something.) Once that decision was made, they said ‘huh. Well, let’s start counting from January 1, 1970. That’s a nice round time. Even though it’s 1972 now, that’ll work great’.

And so that’s what they did, they started counting from January 1, 1970.  All Linux® time is stored like that, it just prints out the pretty time (in the format you want).    Check out the man page for date, it will tell you how.

Network Time Protocol

Ever noticed how a clock may not have the exact right time? Like it’s close, but not there? The same thing can happen with a computer. It relies upon its internal clock, but that can be almost, but not quite, right. Luckily, there’s a protocol to fix that (the unofficial motto of the Internet) called Network Time Protocol.

This lets you automatically make sure the clock on your computer is set to the right time. This is really important if you have many computers working together. Kind of like Tony Stark’s drones in the Age of Ultron movie. Suppose he sent them after Ultron but the clocks are all just a little bit off. Instead of blasting Ultron all at once, you’d get one shot, a few minutes later another, and then another, and.. see? that’s not a good thing. You want one BLAST to take Ultron (and HIS drones) out.

So, how does it work? To begin with, you have a really accurate clock, something like an Atomic Clock. That has an awesome name, right? Sounds like something used in a Doomsday weapon to make sure that no one can stop it, except maybe Super Man. It’s one of the most accurate clocks known, so that’s a good thing to ask.

There’s computers that synchronize directly to the clocks, so those are a good thing to ask too. Generally, there’s a list of servers on your system that your system will synchronize to. Network Time Protocol is designed to use the list and say ‘yo, other computer, tell me what time it is’ and then adjust your computer if it’s off.

The list of servers is kept in the file ntp.conf which is generally located at /etc/ntp.conf. Let’s look at it:

# /etc/ntp.conf, configuration for ntpd

driftfile /var/lib/ntp/ntp.drift
statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to more than 300 low-stratum NTP servers.
# Your server will pick a different set every time it starts up.
#  *** Please consider joining the pool! ***
#  *** <http://www.pool.ntp.org/join.html> ***
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst

There’s lots of information in there. The important part is the names after that word server. This computer uses servers by the name of 0.debian.pool.ntp.org 1.debian.pool.ntp.org, 2.debian.pool.ntp.org and 3.debian.pool.ntp.org to keep the time straight. This means that if one of those isn’t available, the Network Time Protocol will ask one of the other ones.
One of the interesting facts about the Network Time Protocol is that it’s one of the oldest protocols on the Internet. People have been trying to keep the times synchronized on their computers as long as they’ve been able to talk to other computers.

How about a Date?

I bet your cell phone knows what today’s date is.  Your computer should know too, right?  Depending on how your system is set up, you might even see the current date on your screen.  Well, your command line also knows what date it is and can tell you if you ask it.  The command is called (drum roll here please):  date

Alpha:~ computerlamp$ date
Fri Apr 28 15:21:15 EDT 2017

That’s the date that I wrote this and it even include the actual time.  That’s rather useful, isn’t it?

What else can we do?  Well, let’s find out what today’s month looks like.  I had an appointment on the 15th but I forgot what day of the week that was.  Well, there’s a command for that!

Alpha:~ computerlamp$ cal
     April 2017
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

That’s one useful command, isn’t it?  I can see that the 15th was on a Saturday.  But hey, it’s almost the end of the month, I want to know what day of the week May 14th is.  Well, I can ask that too:

Alpha:~ computerlamp$ cal 5 2017
      May 2017
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

I just asked it for the calendar for the fifth month of the year 2017.  Huh.  I wonder what happens if I ask it for the calendar for May 2020.

Alpha:~ computerlamp$ cal 5 2020
      May 2020
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

What do you know, that works too!  I can ask for the month and year for almost any time!  I bet that would come in use when Flash starts playing with the timeline again

The Flash Changing Time Again!

The Flash Changing Time Again!

I bet if he goes way back into the past I can figure out what the calendar looks like.

According to this Captain America disappeared around April 18, 1945.  That was a Wednesday!

Alpha:~ computerlamp$ cal April 1945
     April 1945
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

So The Flash could go back in time and rescue him, right?  Except that Captain America is Marvel and The Flash is DC.  Oh well, details, right?

sudo

So one day, Tony Stark noticed a wasp in his lab.  He could put on the whole suit to kill it, or he could just put on the glove.  The whole suit is overkill, right?  Just one glove would take care of the whole thing.  There’s a similar idea in Linux®.  Using su to get root powers is overkill, the ‘just enough’ command is sudo.

Glove vs Wasp

Iron Man’s Glove vs Wasp

sudo lets you execute a command as another user.  You can be root, you can be tonystark, batman, spock… you can be anyone you want, but you have to be given the power.  You can’t just walk up to the computer terminal and say ‘today, I’ll be batman, Batman’s user name.  I’ll do whatever batman can do on this Linux® system because sudo will let me’.

That’s kind of like putting on Batman’s suit and declaring yourself Batman.  It doesn’t work like that… though I wouldn’t mind being Batman for a day and driving around in the Batmobile.  That thing looks awesome.

Once you’re given permissions, you execute the commands like this:

Alpha:~ computerlamp$ sudo -u batman vi plans

If I have the permissions to do it, I can edit the plans file as batman.  I’m hoping it contains the plans for how to stop the Riddler.  Riddles aren’t my thing.

Anyway, how do you know what commands you have?  Well, there’s a flag for that!  (The other Linux® motto.)

Alpha:~ computerlamp$ sudo -l
Matching Defaults entries for computerlamp on Alpha:
    env_reset, env_keep+=BLOCKSIZE, env_keep+="COLORFGBG COLORTERM",
    env_keep+=__CF_USER_TEXT_ENCODING, env_keep+="CHARSET LANG LANGUAGE LC_ALL LC_COLLATE
    LC_CTYPE", env_keep+="LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME", env_keep+="LINES
    COLUMNS", env_keep+=LSCOLORS, env_keep+=SSH_AUTH_SOCK, env_keep+=TZ, env_keep+="DISPLAY
    XAUTHORIZATION XAUTHORITY", env_keep+="EDITOR VISUAL", env_keep+="HOME MAIL",
    lecture_file=/etc/sudo_lecture

User computerlamp may run the following commands on Alpha:
    (ALL) ALL

So the first part of that response we’ll talk about another time.  It’s the last line that’s the most interesting.  It says that I can run all commands on Alpha.  Every single one!

So why do I use sudo rather than su?  Well, it’s to keep myself from making mistakes. If I have to think about each command before I use it, then I’ll be more careful.

Remember, for both sudo and su, it’s like Spiderman’s Uncle Ben said.

Spiderman!

With great power comes great responsibility!

Swap on/Swap off/Swap!

In the last post we talked about computer memory and there was that weird line in the free command results called swap.  I also promised to talk about it this week.

So there you are in Math class one day and your teacher writes the most complex equation on the board you’ve ever seen and says ‘SOLVE IT!’.  What do you do?  Well, you pull out a piece of paper and get to work.

swap is kind of like that piece of paper.  The computer wants to do all its thinking in the memory it has installed for that, but sometimes that doesn’t work.  So instead of pulling out a piece of paper and doing work on that, it uses swap.  It would be more fun if your computer could stop what its doing and pull out paper, but it wouldn’t work as well.

So what exactly is swap anyway?  Well, when you set up your system, you designate a part of your hard drive to be the swap space.  This means that the computer is using its remembering memory for thinking memory.  Sounds weird, but it works.  You remember when you think sometimes, right?  You remember how to do the math problem while you’re thinking about the math problem.  Or you’re thinking about how Iron Man would solve it rather than actually solving it, but hey, whatever keeps the math teacher happy!

So let’s look at the result of that free command again:

Alpha:~ computerlamp$ free -g
             total       used       free     shared    buffers     cached
Mem:            31         29          1          0          0         12
-/+ buffers/cache:         17         13
Swap:            2          2          0

According to this, I have 2 gigabytes of swap to use and I’ve used both of them.  In other words, I’ve used up all the paper I had free to do my work on and there’s no more available.  Not exactly the best spot to be in, but I’m sure I can borrow some from the guy next to me… except that doesn’t really work with computers.    But your administrator can add swap if she decides she needs it.

Computer Memory and Linux®

You have memory where you sit and remember, well, computers have a similar thing.  And it’s also called memory.  Sounds kind of silly to also call it memory, but it actually kind of works.

Computers have two kinds of memory.  There’s hard drives, where it stores programs.  You can think of it like the memories you have.  You can sit there and remember watching Star Wars, right?  A computer can save all of Star Wars to its hard drive so you (or it, if it wants) can watch it again and again.

The other kind of memory is the thinking kind.  If I sit here and think about what Luke Skywalker and Rey have to say each other, that’s not the remembering kind of memory, that’s the thinking kind.  When your computer is running a program, it’s thinking.  When you look at a directory, it’s remembering.

So, how much thinking memory does my computer have?  Of course, there’s a command for that!  It’s called free.

Alpha:~ computerlamp$ free -g
             total       used       free     shared    buffers     cached
Mem:            31         29          1          0          0         12
-/+ buffers/cache:         17         13
Swap:            2          2          0

So when I used the -g flag, I wanted to know the results in gigabytes.  If I don’t use that flag, I get the results in bytes.  That doesn’t really mean much to me, so I’ll go with gigabytes.

The man page for free also tells me to ignore the shared column, that’s obsolete.

But back to the results.  The first column tells me I have 31 Gigabytes of memory on this system and 29 are used but 1 is free.  That’s weird, right?  Its because the system actually counts them in bytes and then divides things out (and rounds it off) for the results.  It wants to give you nice pretty numbers to look at.

The bottom row, that’s called swap and we’ll talk about it in the next post.

The Kernel Version

Over the years, Iron Man has had a lot of suits.  The suit he had when he started out looks nothing like the suit he wore in the Civil War movie.  Linux® kernels are something like that, the first kernels don’t look anything like the kernels of today.  There are different kernel versions that depend on what the person who put it together wanted and what hardware you’re running.

In other words, the kernel version of your Linux® system probably doesn’t look anything like the kernel version of the system I’m running.  But you can’t tell just by looking at the system what kernel version you’re running, so it’s not like Iron Man’s suit.  It would be neat if it was, imagine if your system put on a new suit every time you put a new kernel on it.  I think it would be awesome for it to change color at least every time I put on a new kernel, but it doesn’t work like that.

Luckily, there’s a command to find out what kernel version you’re running and it’s called uname.  The man page for it

uname man page

uname man page

Tells you exactly what it’s designed for,  it’s to print system information.

So, what information do I want?  I want the kernel version!

That’s:

Alpha:~ computerlamp$ uname -v
#1 SMP Tue Aug 12 09:58:26 UTC 2014

What else can I find out?

Well, I can find out what machine name I’m running, that’s uname -m

I can find out the name of my computer, that’s uname -b.  I can also find out the name of my computer using a different command.  That one is

Alpha:~ computerlamp$ hostname
Alpha
Alpha:~ computerlamp$ uname -b 
Alpha

Either way I run the command, I get the same result out, which means as usual there’s more than one way to do it!

I can also find out my processor, or my hardware platform, or the operating system.

But wait, you say, of course I know my operating system, I’m using Linux®.  Well, it’s always good to double check that using our friendly comman.

Alpha:~ computerlamp$ uname -o 
GNU/Linux

If you have multiple systems, it’s always useful to find out what kernel version you’re running on each one.  It’s a good idea to keep it consistent, but that’s up to the administrator.

Kernel Modules or Putting it all Together

The Linux® kernel is not one huge thing that can run every hardware ever made and talk to every application ever created.  Can you imagine that? It would be huge and every time something new was added, you’d have to add it on.  Even if it wasn’t anything you needed.  Instead, the kernel is a modular kernel, which means you can add pieces on as needed.  These pieces are called modules.

It’s like Lego® blocks.  You add the blocks on that you need but you don’t have to use every single one.

Lego Blocks

Lego Blocks

Just like that picture.  If I’m making a wall, I don’t need to add this head:

Lego Head

Lego Head

It doesn’t make any sense to add that to a wall, does it?  But if the rule is ‘use every single Lego® block you own no matter what’, then even if you’re making a wall, you have to add that on .  Maybe to the top, it would be good at scaring intruders, or at least making them laugh.

With the Linux® kernel, you don’t have to do that.  You can use modules to build your kernel so it does exactly what you need and nothing more.

It’s hard to find computing hardware that Linux® won’t support.  It runs motherboards, networking connections, webcams, security systems, CDs, DVDs, mice, keyboards, card readers, I could go on and on.  In fact, it might be easier to list things it doesn’t support, but… I really can’t think of anything.  If someone really wants to use Linux® to run a piece of hardware, they’ll either write the kernel module or find someone to write it for them.

You may not use all of the modules in Linux® but when you download the source code for it from https://kernel.org (that’s where the most recent source code is) you do get almost all of the modules along with it.  You can pick and choose which parts you want when you build it, which is something we’ll talk about in the future.  That’s because it’s about how to compile a program from source code, a more advanced topic than this.

I said ‘almost all’ because sometimes hardware manufacturers offer the modules separately.  It depends on the manufacturer and the people that maintain the kernel source code.

Kernel of the Matter

We’ve talked about how your computer gets places on the Internet.  Now let’s talk about how your computer, well, works!  How does Linux® do its thing, aside from  typing commands at the command line?  It’s called the kernel and it’s the core (or kernel) of the operation of your system.

So it’s not like the kernel of your popcorn, it’s more like the thing in the middle.  Like Nick Fury.  He’s always in the middle of the Avengers keeping things going.  Or making things go.  He’s always up to something.

Nick Fury

Nick Fury

He showed up with that helicarrier at the end of The Age of Ultron, didn’t he? He made that happen and got the Avengers (and the towns people) from the flying in the sky town (something I never want to see) and on to safety.

That’s what the kernel does.  It’s in between the hardware and the software.  Like this picture:

What the Kernel Does

What the Kernel Does

Linus Torvalds made the first Linux® kernel in 1991 for his own system. These days it runs on all sorts of things, but back then, he just wanted something for his own computer.

So how does it control your hardware?  It uses things called device drivers.  It lets you type on your keyboard and have it show up on a screen, or print to a printer, or read a CD, or read a USB, or… you get the idea.  It’s really flexible.

The kernel is also  modular.  That means that it isn’t one huge thing that keeps things going, but it’s made up of lots of little systems that put it all together and make it go.

It’s kind of like a Death Star built from Legos.  It needs all the parts together to make it a Death Star, but you can leave pieces off and still mostly have a Death Star.  You can add pieces to it and it’ll be a Death Star with more abilities.  Like if someone added an ice cream maker to the Death Star.  Or a spa for the emperor.  It’s still the Death Star and still acts like it, it’s just more!