Monthly Archives: May 2017

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.