Author Archives: computerlamp

About computerlamp

The author of this blog has over 20 years of experience with Linux and loves the command line and the flexibility it gives. The author's nephew is invaluable for his help with pop culture references and explaining concepts.

Index of ComputerLamp Posts

You will find 83 posts in the category Linux on this blog.


Jump to B, C, D, E, F, H, I, K, L, M, N, P, R, S, T, U, V, W

B (4)

C (4)

D (8)

E (3)

F (6)

H (3)

I (5)

K (3)

L (9)

M (9)

N (2)

P (7)

R (2)

S (8)

T (6)

U (1)

V (1)

W (2)

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.

 

History

Remember that teacher who never forgot a single thing in history? Who could not only tell you what General Custer was wearing at his last stand but the grade you got on your last test?

Well, bash, the shell you’re using in your command line has the same thing. It’s called history. How original! You use the command history to see your history. Okay, maybe not original. Maybe it should be called thingsIdid or whatIdid. Anyway, it looks like:

Alpha:~ computerlamp$ history
    1  ls
    2  pwd
    3  cd
    4  history

See?  Those are the last four commands I happened to type.  Bash is like that teacher, it doesn’t forget.

It’s also useful to combine history with a grep:

Alpha:~ computerlamp$ history | grep ls
    1  ls
    5  history | grep ls

So if you forget ‘exactly how did I do that find command?’ you can, well, find it!

Another way in bash to see your history is to hit the up arrow at the command line.  You’ll see previous commands that you executed and can hit enter and run them again.  If I hit the up arrow ↑ I get:

Alpha:~ computerlamp$ history | grep ls

Which, guess what? It’s the command I just typed.

Your history can contain 500 (or more) commands… and that’s a lot to see. To see the last 10 commands you typed, try this:

Alpha:~ computerlamp$ history 10
    4  ls
    5  echo "Stop Hydra" >plans
    6  ls
    7  history
    8  fortune
    9  yes
   10  find . -type f
   11  ps auxw
   12  ps
   13  history 10

See those numbers before each command? They’re useful too. You can either hit that up-arrow until you find the command you’re looking for (boy, that’s boring) or you can do:

Alpha:~ computerlamp$ !8
fortune
You learn to write as if to someone else because NEXT YEAR YOU WILL BE
"SOMEONE ELSE."

Now you don’t have to remember every command flag you’ve ever used, bash does it for you.

 

Finding Processes

Now that we know that processes run, how can we go about finding processes?  There must be some way to find a particular process… like if I want to find every process that has the letter a in the name. Or if I want to find every process that has a name like ‘insight’ because I’m sure Hydra is running something on my system and I want to know what it is.  Or if I want to know every process that’s being run by a user with the name hydra.

You get the idea.  I want to find processes!

To do this, we’re going to combine two commands.  The first is ps, that useful command that lists the processes.  The second is that useful command grep that we used to find things in files.

ps auxw shows you every single process that’s currently running on a system.  On a system that’s very very busy, this can be a lot.  There’s 65536 processes allowed at any time, so combing through those looking for hydra (or Hydra or hYdRa) would take too long.  I’m lazy!  I want the system to do it for me!

From this post we know that grep has an option where it ignores case.  This means it can find hydra, Hydra, HYdra, HyDrA, or… I’m stopping that now.  It means you can find any combination of the 5 letters hydra no matter what capitals you use.

Now that we have those two commands, we’re going to combine them.  Remember this post on pipes?  We can take the output from one command, ps and pipe it through to the other command, grep.

Alpha:~ computerlamp$ ps auwx | grep -i hydra

Nothing was found from that command.  Hurray!  No Hydra user on the system!

…unless they’re hiding themselves.  Alexander Pierce didn’t have a username of Hydra.  He was hiding and probably had a user name of apierce or alexander or alexander.pierce or even supersecretary.  But not something we could actually look for in ps.

Remember ps a from this post? It showed what processes you were running in a single terminal.  If you want to see every single process you were running, no matter what terminal, then this combination of ps and grep  would work for you!

Alpha:~ computerlamp$ ps auwx | grep -i computerlamp

Finding processes is easy with that combination of ps and grep!

Silly Commands

In honor of New Year’s Day, today we’re going to talk about more silly commands.

The first one is called yes

Alpha:~ computerlamp$ yes
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y

 

And it keeps going and going and going! It’s the Energizer Bunny of commands!

To stop it, hit control-C.  That’ll stop the Bunny, er, Y from streaming on your terminal.

So what’s this command for? Other than to fill your screen up with more y’s than should be legal. Well, remember from this post the xargs command? Some commands insist you sit there and type y over and over and over before they’ll finish. This command lets you do:

Alpha:~ computerlamp$ yes | xargs annoying_command

So you can be lazy and not type it over and over and… did I mention over? Then when that command finishes, your yes command will exit.

Linux® has lots of silly commands.  Another one has no redeeming feature at all. Aside from pure amusement. It’s called fortune.

Alpha:~ computerlamp$ fortune
Long life is in store for you.
Alpha:~ computerlamp$ fortune
Q:    What do you say to a New Yorker with a job?
A:    Big Mac, fries and a Coke, please!
Alpha:~ computerlamp$ fortune
So she went into the garden to cut a cabbage leaf to make an apple pie;
and at the same time a great she-bear, coming up the street pops its head
into the shop. "What! no soap?" So he died, and she very imprudently
married the barber; and there were present the Picninnies, and the Grand
Panjandrum himself, with the little round button at top, and they all
fell to playing the game of catch as catch can, till the gunpowder ran
out at the heels of their boots.
        -- Samuel Foote
Alpha:~ computerlamp$ fortune
You may worry about your hair-do today, but tomorrow much peanut butter will
be sold.

It even has a man page. You can type man fortune and see the flags it has too.

For example, if you only want short fortunes, then you could do:

Alpha:~ computerlamp$  fortune -s
How apt the poor are to be proud.
		-- William Shakespeare, "Twelfth-Night"

If you’re only interested in long fortunes, then you would do:

Alpha:~ computerlamp$  fortune -l
At once it struck me what quality went to form a man of achievement,
especially in literature, and which Shakespeare possessed so enormously
-- I mean negative capability, that is, when a man is capable of being
in uncertainties, mysteries, doubts, without any irritable reaching
after fact and reason.
		-- John Keats

The fortune command pulls it’s sayings from many different files. If you want to know what file the fortune came from, you would do:

Alpha:~ computerlamp$  fortune -c
(riddles)
%
Q:	How do you play religious roulette?
A:	You stand around in a circle and blaspheme and see who gets
	struck by lightning first.

As you can see, not all of the fortunes are polite.

So there you have it. Two silly commands found in Linux®.

 

Parent and Child Processes

Processes have strange terminology, we can talk about a parent and child process and isn’t that weird? We’re talking about processes, not cats!

A process can start another process. Your bash shell starts another process anytime you run a command. Your command was started by that bash process. That bash shell is the parent process to any process you create. And any process you create is the child process of that bash shell.

I keep typing process over and over. So I just have to say…

…process.

Anyway, processes start processes and are started by processes. The great granddaddy of all processes is called the init process. Your init process has the processes id of 1 since it’s the first thing started when a system boots up and all processes are the child (or grandchild or great grand child or… you get the idea) of that process.

ps, that very useful command, has a combination of flags that lets us see parent and child relationships.

Alpha:~ computerlamp$ ps fa
  PID TTY      STAT   TIME COMMAND
 9767 pts/2    Ss     0:00 -bash
 9775 pts/2    R+     0:00  \_ ps fa

You can see where bash started the command ps fa. For another layer, I typed bash then I did ps fa again.

Alpha:~ computerlamp$ bash
Alpha:~ computerlamp$ ps fa
 PID TTY      STAT   TIME COMMAND
 9767 pts/2    Ss     0:00 -bash
11442 pts/2    S      0:00  \_ bash
11511 pts/2    R+     0:00      \_ ps fa

The bash shell is the parent of the bash shell which is the parent of the ps fa command.

Another way to do this is with ps -ejH. This time, the results look different.

Alpha:~ computerlamp$ ps -ejH
  PID  PGID   SID TTY          TIME CMD
 9767  9767  9767 pts/2    00:00:00 bash
12398 12398  9767 pts/2    00:00:00   ps

It does the same thing, but this version doesn’t have the lines that the first version does.

And in conclusion, process!

 

ps and flags

Last post we introduced ps, this post we’ll talk about ps and flags. So, brief recap:

Alpha:~ computerlamp$ ps
  PID TTY          TIME CMD
17245 pts/2    00:00:00 bash
17248 pts/2    00:00:00 ps

Tada! ps with no flags.

Let’s start with a simple flag, x. In the first example, we saw every command that has a tty, or a direct connection to a terminal. That’s the default action of ps. What if we want to see every thing even if it doesn’t have a terminal?

Alpha:~ computerlamp$ ps x
12779 pts/2    R+     0:00 ps x
17245 pts/2    Ss     0:00 -bash

Well, that isn’t any different from the first time around, except now I see that I ran ps with a flag.

But wait, something is weird here. Since we started talking about flags, they’ve always started with a aka a dash or a hyphen. That time, I didn’t use one. ps is an interesting command, it has three kinds of flags you can use. From the man page:

      This version of ps accepts several kinds of options:
       1   UNIX options, which may be grouped and must be preceded by a dash.
       2   BSD options, which may be grouped and must not be used with a dash.
       3   GNU long options, which are preceded by two dashes.

This means that there are flags that begin with a dash, flag that don’t begin with a dash and flags that have two dashes. If you use the kind without a dash then you can’t use the kind with a dash. It also means that ps -a and ps a are two different commands. See?

Alpha:~ computerlamp$ ps -a
  PID TTY          TIME CMD
12825 pts/2    00:00:00 ps
Alpha:~ computerlamp$ ps a
  PID TTY      STAT   TIME COMMAND
12832 pts/2    R+     0:00 ps a
13451 pts/2    Ss     0:00 -bash

And isn’t that confusing? It feels like Dr. Strange wandered in and the world went weird.

Next post, we’ll talk about processes again.  Understanding processes is key to understanding ps.

Processes and Running Things

In one of my first posts I talked about this thing called a process. It happens when you run any program on a Linux® system, you create a process. If these things are running, how do I see them? Well, Linux&reg keeps them a table called a process table. We can see them with the command ps. You run it like:

Alpha:~ computerlamp$ ps

The output is kind of boring. It looks like:

  PID TTY          TIME CMD
17245 pts/2    00:00:00 bash
17248 pts/2    00:00:00 ps

It is just the processes that I’m currently running. bash is a shell and that is a program that gives me my command lines. We’ve been using it all along. There’s many kinds of shells, bash is one of the most common. It stands for Bourne Again Shell and was called that because it was a written as a replacement for a Bourne Shell. Wikipedia says it was first released in 1989. It’s been around a long time and it’s the standard command line interface on most Linux® systems.

The second command is the ps that we ran to get that command. If we had used any flags with ps, we would see those too.

The first column is the process id. Remember, Linux® understands numbers better than strings, so every process gets a number. It uses that id to refer to the process.

The second column is the tty. That stands for teletypewriter, a very very old device. According to Wikipedia it’s a typewriter that can be used to send messages to another typewriter. How’s that for very old school? Linux® took that history and made a tty mean terminal or output device. It’s the output device that that command is hooked up to.

ps is a command with many flags. Many MANY flags. We’ll talk about those in more posts.

Permissions or How to Hide Things

I promised in the last post that I’d tell you how to fix that little problem where Hydra can see in the Insight directory, also known as a permissions problem. We call that a big ‘oops’ because we don’t want them to see anything in there.

The Insight directory looked like:

drwxr-xr-x  2 computerlamp  shield  68 Nov 13 09:56 Insight/

Our problem has two parts that we need to fix. First, the group permissions on this directory don’t let the shield group write to it. Secondly, the world (that is, anyone!) can see in the directory. Let’s start with the first problem. We want shield to be able to write in that directory.

The command we’re going to use is called chmod. So I want the group permissions to look like rwx. I can do this:

Alpha:~ computerlamp$ chmod g=rwx Insight

That means change the permissions on Insight for the group to read, write and execute. Check out the results in ls -l:

drwxrwxr-x  2 computerlamp  shield  68 Nov 13 09:56 Insight/

I’m halfway there. I want the last three letters, those things that are currently r-x to be . The chmod command for that looks like:

Alpha:~ computerlamp$ chmod o=--- Insight

We’ve done it! Hydra is prevented from seeing in the Insight directory.  We fixed our permissions problem.

drwxrwx---  2 computerlamp  shield  68 Nov 13 09:56 Insight/

Then we have the horrible moment when S.H.I.E.L.D. fell and we discovered that Hydra was part of S.H.I.E.L.D. all along. This means we need to remove that group access from Project Insight, though it’s probably a little late. But we do what we can, which includes blowing up helicarriers. The command to remove all access from the group is:

Alpha:~ computerlamp$ chmod g=--- Insight

Checking things out with ls -l again, we see:

drwx------  2 computerlamp  shield  68 Nov 13 09:56 Insight/

Which means that only I can read what’s in that directory, write to it, or make anything run.

Users and Groups

You’re using the computer, so you are a user. Computers understand numbers better than names, so you have a number associated to you called a user id.

If you want to see your user id, the command id with the flag -u will show you.

Alpha:~ computerlamp$ id -u

Let’s pretend that you have a friend named Nick who also uses your computer sometimes. The two of you are working on a project together on the computer so you want to share your files so that bot of you can read them or write to them. You and Nick want your project to be secret too, so that only the two of you can access the files. For fun, we’ll call this project insight. It’s a super secret project to design helicarriers to be used by S.H.I.E.L.D. and you don’t want Hydra to see them.

But I digress. Back to the original problem. How do you and Nick make it so only the two of you can access your project?

Linux® has groups to solve this problem. This means you create a group of you and Nick (let’s pretend his username is nickfury) and this will let you and Nick make it so only the two of you can access the project insight files.

You actually already belong to groups that are added by default when your account was added to the system. The command groups returns a list.

Alpha:~ computerlamp$ groups
computerlamp staff admin netusers

That’s just an example. It isn’t every group I’m a member of and your results are certainly different from mine.

To add the new group, let’s call it shield, you need to talk to the system administrator for the computer system you’re using. She’s the one that set the whole thing up to begin with and can add the group. Once you do that, if you run the groups command you get a new list.

Alpha:~ computerlamp$ groups
computerlamp staff admin netusers shield

Now that we have the new group, in the next post we’ll talk about how to share your files with Nick and only Nick.