Category Archives: Commands

last

Every time you log in to your Linux® system, a log entry is made.  It is important for security, what if you logged in while you were asleep?  Either you were sleep typing or someone got your password.  Either way, that’s important information.  Lucky for us, Linux® logs this and there’s an easy way to see when you logged in.  That command is last.

If someone does log in and pretend to be you, then that person who isn’t you wants you not to know that you logged in when you didn’t actually log in.

Wait, let me try that again.

If the bad guy logs into the system, he wants to hide from you that he logged in.

Ultron went traipsing around the Internet and he didn’t care if anyone knew what he was up to.

Ultron up to no good

Ultron up to no good

Other bad guys do care so their first attempt is to remove these messages.

Linux® tries to make this hard.  Most log files are straight boring plain text.  That means you can look at them and even edit them and no one will know the difference.

The file that last reads from isn’t like that. It’s a binary file, which means that you an’t edit it at all without mucking things up.

The output of the command looks like this:

computerlamp    pts/2        console Thu Mar 15 20:53   still logged in
computerlamp    pts/2        console Wed Mar 14 20:52 - 20:53  (00:00)
computerlamp    pts/2        console Fri Mar  9 21:24 - 21:34  (00:10)
computerlamp    pts/1        console Sat Feb 24 15:56 - 17:13  (01:17)
computerlamp    pts/0        console Fri Feb 23 14:30 - 15:47  (01:17)
reboot   system boot         console Fri Feb 23 13:59         (20+05:53)
computerlamp    pts/2        console Wed Feb 21 20:06 - 20:16  (00:09)

It shows you when I logged into the system, when I logged out, it shows you that there was a reboot of the system as well.  If I looked over this and said ‘now wait a minute, I was out of town February 23, what happened there?’ then I would be investigating a possible break in of my system.

Fun Linux® Commands

Let’s have some fun with Linux® commands!  There’s lots of interesting and weird commands on most Linux® systems, so lets’ have some fun and try out some fun Linux® commands.

We start with one called figlet.  I know what you’re thinking, who names these things?  At least with Dr. Strange you know exactly what’ you’re getting, you’re getting a strange doctor.  figlet just sounds weird.

 

Dr Strange Clipart

Image from clipartfest.com

 

Let’s see what we get when we try it:

Alpha:~ computerlamp$ figlet test
  m                    m
 mm#mm   mmm    mmm   mm#mm
   #    #"  #  #   "    #
   #    #""""   """m    #
   "mm  "#mm"  "mmm"    "mm

Well isn’t that neat?  It took our word and turned into a banner made of characters.  Now I’m not saying this isn’t a useful command at some point, because clearly someone made it.  I’m saying it is a rather silly command.  It’s also one of many fun Linux® commands!

There isn’t a man page for figlet, but if you look at the output of figlet -h you’ll see all sorts of flags you can use with it.

Let’s do one more.

Alpha:~ computerlamp$ figlet Hi There

 m    m   "          mmmmmmm #
 #    # mmm             #    # mm    mmm    m mm   mmm
 #mmmm#   #             #    #"  #  #"  #   #"  " #"  #
 #    #   #             #    #   #  #""""   #     #""""
 #    # mm#mm           #    #   #  "#mm"   #     "#mm"

That was fun!

There’s another way to do it, of course.  That’s called banner and it’s another of the fun Linux® commands.  It makes similar messages, but they look a little different:

Alpha:~ computerlamp$ banner -w 40
Message: Hi There
          #                         #
          #                         #
          ###########################
          ###########################
          #              #          #
          #              #          #
                         #
                         #
                         #
          #              #          #
          ###########################
          ###########################
          ###########################
          #                         #

          #              #
          ################    ####
          ################    ####
          ################     ##











                                #####
                                #####
                                   ##
                                    #
          #                         #
          #                         #
          ###########################
          ###########################
          #                         #
          #                         #
                                    #
                                   ##
                                #####
                                #####
          #                         #
          ###########################
          ###########################
          ###########################
                        #
                        ##
                        ##
          ################
          ###############
          ############
               #####
            ###########
           ##############
          ##     #     ##
          #      #       #
          #      #       #
          #      #      ##
           #     ########
            ##   ######
                 ###
          #              #
          ################
          ################
          ################
                      ##
                       ##
                     #####
                    ######
                    #####

               #####
            ###########
           ##############
          ##     #     ##
          #      #       #
          #      #       #
          #      #      ##
           #     ########
            ##   ######
                 ###

When I ran the banner command, I gave it that -w flag so that it wouldn’t fill the entire string.  Try it without any flag and see what you get.  If you don’t pass in the string for the message, it asks you what you want to use.

More than one way to do it

There’s more than one way to do it!  I think that should be one of the mottos of Linux®.  There’s always more than one way to do it.  That’s what we’re going to talk about in this post, some of the more than one way to do the commands we’ve already learned.

We learned about ls and how it has flags.  One of the most useful flags is -l which lets us see all the group ownerships and users and everything we could need.

For example:

Alpha:~ computerlamp$ ls -l
total 164
drwxr-xr-x   3 root root 69632 Aug  9 13:15 bin
drwxr-xr-x   2 root root  4096 Jan  5  2016 games
drwxr-xr-x  82 root root 20480 Jul 17 16:18 include
drwxr-xr-x 111 root root 24576 Jul 17 16:18 lib
drwxr-xr-x   3 root root 12288 Aug  3 06:43 libexec
drwxr-xr-x  19 root root  4096 Dec 27  2016 local
drwxr-xr-x   2 root root 12288 Jul 28 18:58 sbin
drwxr-xr-x 199 root root  4096 Jul 28 18:58 share
drwxr-xr-x   2 root root  4096 Apr 19  2012 src
drwxr-x---   3 root root  4096 Jan  5  2016 var

I listed all the files and directories in /usr using ls -l.  I can do the same thing with the command vdir.

Alpha:~ computerlamp$ vdir /usr
total 164
drwxr-xr-x   3 root root 69632 Aug  9 13:15 bin
drwxr-xr-x   2 root root  4096 Jan  5  2016 games
drwxr-xr-x  82 root root 20480 Jul 17 16:18 include
drwxr-xr-x 111 root root 24576 Jul 17 16:18 lib
drwxr-xr-x   3 root root 12288 Aug  3 06:43 libexec
drwxr-xr-x  19 root root  4096 Dec 27  2016 local
drwxr-xr-x   2 root root 12288 Jul 28 18:58 sbin
drwxr-xr-x 199 root root  4096 Jul 28 18:58 share
drwxr-xr-x   2 root root  4096 Apr 19  2012 src
drwxr-x---   3 root root  4096 Jan  5  2016 var

There’s more than one way to list the files.  I can even do it with find though it’s much more work than just doing vdir or ls.

If I leave off the -l flag, I get:

Alpha:~ computerlamp$ ls /usr
bin  games  include  lib  libexec  local  sbin	share  src  var

I get the same output if I do the following two commands, just with fewer spaces:

Alpha:~ computerlamp$ cd /usr
Alpha:~ computerlamp$ echo *

In other words, there’s more than one way to do it.  I can ls -l or vdir, or I can ls or use the echo * trick.

This is certainly not the only case of more than one command to do the same thing.  It keeps things interesting when you’re learning Linux®.

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!

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.

SuperUser aka Root

Before we go back into the world of Linux (having spent time in the world of networking), let’s talk about users again.  In particular, the SuperUser also known as Root.  Root is his every day name, SuperUser is his power!  And with great power comes great responsibility.

Groot not Root

Groot not Root

That’s Groot, not root, even though they rhyme.  Groot is a superhero, root is a superuser, and root doesn’t go around saying ‘I am Groot’ all the time.  They’re both super though.

SuperUsers’s great power is that he can do anything on a system and  I do mean, anything.  You change the permissions on your file so that you’re the only one that can see it?  Well, the superuser can see it too. He’s all powerful like Superman, but without that annoying green rock allergy.

His username is root and because he’s so powerful, he shouldn’t be logged in unless he’s absolutely needed.  The administrator of your system shouldn’t keep him logged in, but there’s a command to see who is logged in.  It’s called who.

Alpha:~ computerlamp$ who
computerlamp  console  Mar 17 08:01

There I am, logged into the system and I don’t have root logged into my system right now and that’s a good thing.

Since root is a user, he (or she!) has a password.  If you know the password, you can become root.  Like IronMan putting on his suit or BatMan suiting up, you can put on the root armor and be… SuperUser!  And like I always say, there’s a command for that.  This one is called su.

Alpha:~ computerlamp$ su
Password:
Alpha:~ :~ root#

You have to know the password in order for it to work.  If you don’t know the password, don’t try to do this.  It means you don’t have the permission to do it and if you try, you’re attempting to go around the system’s security and that’s a bad thing.  It’s like trying to lift Thor’s hammer and you’re not worthy.

If your root password gets out, then that’s a security problem.  When we made the shield group to hide the helicarrier plans, we thought we were safe from prying eyes.  But if Hydra got the root password, it wouldn’t matter.  That’s why it’s so important to keep that root password safe. The administrator of the system shouldn’t share it with anyone who doesn’t absolutely need it.

Ping — Are You There Computer?

We talked about traceroute, that’s a way to see how your traffic gets to its destination.  Like putting a GPS tracker on someone and saying ‘now how did you get to the comic book store?’.  What if you want to know ‘is the other computer on the network’?  Not how do I get there, but just are you there?  There’s a command for that!  It’s called ping.

ping is a way of saying ‘are you alive?’ to another computer.  It doesn’t mean the computer is actually doing anything worthwhile, but it does mean that the computer is talking on the network.

It’s like playing ping pong.

One Person Ping Pong

One Person Ping Pong

Pretend that there’s a field that blocks your view of the other guy.   You hit the ball over to him and the only way you’re sure he’s over there is if he hits it back to you. He could be there and just not in the mood to hit, but you don’t know because you can’t see him.

Alpha:~ computerlamp$ ping www.google.com
PING www.google.com (216.58.217.132): 56 data bytes
64 bytes from 216.58.217.132: icmp_seq=0 ttl=54 time=16.306 ms
64 bytes from 216.58.217.132: icmp_seq=1 ttl=54 time=15.747 ms
64 bytes from 216.58.217.132: icmp_seq=2 ttl=54 time=15.769 ms
64 bytes from 216.58.217.132: icmp_seq=3 ttl=54 time=15.342 ms
64 bytes from 216.58.217.132: icmp_seq=4 ttl=54 time=16.163 ms

If you try it, you’ll see that it keeps pinging along and it’ll keep going until you hit control-C to stop it.  When you do that, you’ll see:

^C
--- www.google.com ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 15.342/15.865/16.306/0.341 ms

It’s nice how it keeps up with the statistics, doesn’t it?  And it tells you how long the ping took in milliseconds.

Suppose we know we only want to ping a computer a couple of times.  There’s a flag for that!

Alpha:~ computerlamp$ ping -c 2 www.marvel.com PING www.marvel.com (72.32.138.96): 56 data bytes 64 bytes from 72.32.138.96: icmp_seq=0 ttl=242 time=51.122 ms 64 bytes from 72.32.138.96: icmp_seq=1 ttl=242 time=50.637 ms — www.marvel.com ping statistics — 2 packets transmitted, 2 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 50.637/50.880/51.122/0.243 ms

If your favorite website is down, or not responding to your polite web request, then a good first step is to ping it.  Maybe the entire computer is down and if it is, well, maybe you could try a traceroute next.  A traceroute might tell you if the computer is down or if there’s something down between you and the computer.

In other words, ping is a useful networking tool to keep in your Linux® bag of tricks.

 

Nano! Creating, Changing and Saving Files

In this post we created files using the echo command, now we want something more complex.  The answer is to use an editor.  Now this isn’t an editor like the guy who works for a newspaper, this is a program that lets us create, save, change, and otherwise fiddle with a file.  We’re going to use one called nano.

In the Linux© community, a good way to start an argument is to ask a group of people what the best editor is.  It’s kind of like asking who the best superhero is (I’m told Deadpool. I personally like Iron Man… or even Hawkeye… Black Widow is cool… see what I mean?) or which is better, Marvel or DC?  So we’re not going to talk about best editor, we’re going to start with an easy one.

Alpha:~ computerlamp$ nano

This is what it looks like:

The nano editor

The nano editor

I can type whatever I want into this screen, like this:

Editing with nano

Editing with nano

I can save it with the key combination control-O:

Saving with nano

Saving with nano

I just have to tell it what filename I want.  I’m going to save it as nano.txt.

Then, when I’m done, I’ll exit nano:

Exiting nano

Exiting nano

And now I can create, edit and save a file.  What if I want to edit the file again?

Alpha:~ computerlamp$ nano nano.txt

That brings up that file I just created.

nano has a man page, and it even explains why it’s called nano:

NANO(1)                                                                                NANO(1)

NAME
       nano - Nano's ANOther editor, an enhanced free Pico clone

SYNOPSIS
       nano [OPTIONS] [[+LINE,COLUMN] FILE]...

There’s also help available within nano, if you use the key combination control-G you’ll get a help screen that looks like:

Help within nano

Help within nano

nano is one of the easiest editors to use, which is awesome because we no longer have to rely on echo!  It also creates what are called text files, which means that what  you type in is what you get.

 

 

Alias … Not the Television Show

Alias was a television show where Sydney Bristow took on various aliases to do spy things and to kick butt while she was at it. But the aliases were important. She had names like Julia Thorne, Kate Jones, Sharleen…all kinds of names she hid behind.
bash has aliases too, but it’s not names to hide behind because you’re spying, it’s more names to hide that long command behind so you don’t have to remember it. Remember in the post about bash history where I said it should be named thingsIdid or whatIdid? Now we can make that happen!

Alpha:~ computerlamp$ alias whatIdid="history"

 

Now when you type whatIdid, you get the results of the history command.

There’s the more command and the  less command. I don’t like the more command but I love the less command. So what do I do to keep from typing more by accident? Well, I don’t stop myself from typing it, I just make it run the less command:

Alpha:~ computerlamp$ alias more="less"

The bash shell has history, so there’s got to be a way for me to see what commands I’ve aliased aside from using that history command (which I could, if I combined it with grep!). And there is… just type:

Alpha:~ computerlamp$ alias
alias whatIdid="history"
alias more="less"

Since I can add them and I can list them, there’s got to be a way to remove them… and there is! It’s the imaginatively named unalias command.

Alpha:~ computerlamp$ unalias more

That removes the alias for more from my list of aliases.

Alpha:~ computerlamp$ alias
alias whatIdid="history"

What happens if I type the wrong thing?

Alpha:~ computerlamp$ unalias jedi
-bash: unalias: jedi: not found

I guess the alias command doesn’t know where Luke Skywalker is hiding at either.  Which is good for him, right?  He can hide from Snoke and Kylo Ren a little longer.