Monthly Archives: June 2018

The /etc/ directory

In the last post we looked at /etc/passwd.  That file is in the /etc/ directory, which contains all sorts of configuration files.  This time, we’ll talk about that directory and some of the interesting files in it.

Like I said, it’s a configuration directory.  For example, your computer has an IP address.  If that’s a static IP address, then it’s going to be stored in a file in the /etc/ directory.

Every time you start your computer, it starts a lot of programs by default.  For example, it starts init, the mail program, the program that runs DNS, the printing system, cron, and more.  All of this is controlled by the files in /etc/.

There’s also some interesting files there.  For example, /etc/hostname.

Remember cron?  There’s a system file that runs all of the cron jobs for the system.  It’s in /etc/ too, in /etc/crontab.

There’s also our friend NTP, it has a configuration file there too.

The /etc/ directory was designed so that there wouldn’t be configuration files all over the system, instead, they’d be kept in one place.  This makes it much easier to manage the system, you don’t have to go searching in multiple directories to find the files.

Alpha:~ computerlamp$ cat /etc/hostname
Alpha

Yup, there’s the hostname of my computer.  Alpha.

Another file is /etc/motd, it contains the message of the day.   When you first open a terminal, you see the contents of this file. Sometimes it’s empty, sometimes it will have important messages, and sometimes it just has silly things:

Alpha:~ computerlamp$ cat /etc/motd
 _______
< Alpha >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Yes, that is cowsay in my /etc/motd file.  I was feeling whimsical.

The point of the /etc/ directory is to have the configuration files in one place, not scattered all over the computer.  It makes the system a lot easier to manage.

Plus, cowsay is fun!  All motd files should use cowsay!

The password file

A while back I talked about how users have ids and how we can find out what they are using a command.  Remember, Linux loves files, so this information must be kept in a file somewhere.  It’s called the password file.  It should be called the ‘here are the users’ file, but it’s the password file.

You can actually look at this file.

Alpha:~ computerlamp$ more /etc/passwd

Here’s an example of what that can look like:

root:x:0:0:root:/root:/bin/tcsh
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:

There’s 7 different parts to each line of this file and they’re separated by a colon (:).  Each part is:

  1. username
  2. password
  3. user ID
  4. group ID
  5. real name
  6. home directory
  7. login shell

Let’s pull apart each part of this file.  We know what the user name is, mine’s computerlamp.  What’s yours?

The next one is the password.  It looks weird in that file, there’s an x where you’d think we’d see the encrypted password.  That’s because it isn’t in this file, that’s actually what we call a placeholder.  It’s a ‘one used to be here, but isn’t any more’.  That’s because the actual encrypted password is now in a different file, /etc/shadow that only root can read.

Then there’s the user ID and group ID, we know what those are too and I’d assume you know what your real name is, right?

The last two are the home directory and login shell.  That’s the directory you’re dropped in when you first log in and the shell that you’re going to use.  My home directory is /home/computerlamp and my shell is /bin/bash.

There’s something weird in that result though, right?  For example, the user mail has a login password that’s /sbin/nologin.  If someone tries to login to that account, then they’ll get a polite ‘no, you can’t do that’ message and will be logged out.  Another way of doing this is by putting /bin/false as the shell.

Environment Variables

Your environment can affect you.  If it’s raining and you’re sitting outside, well, then, you’re wet.  Sorry about that.  If you’re sitting inside with the air conditioning on, then you’re cool.  Turning on the air conditioning affects your environment, turning it off can make you warm again.  Bash has the same thing, you can turn things on and off again using environment variables.

They’re called variables because they can vary.  How’s that for a spiffy name?  Setting environment variables changes the behavior of your terminal session, they can also contain information about your terminal session as well.

They’re set by using the command export.  It looks like:

Alpha:~ computerlamp$ export VAR=VALUE

If you want to see all the environment variables in your session, just do:

Alpha:~ computerlamp$ export
declare -x G_BROKEN_FILENAMES="1"
declare -x HISTSIZE="1000"
declare -x HOME="/home/computerlamp"
declare -x HOSTNAME="Alpha"
declare -x INPUTRC="/etc/inputrc"
declare -x LANG="en_US.UTF-8"
declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="computerlamp"
declare -x LS_COLORS=""
declare -x MAIL="/var/spool/mail/computerlamp"
declare -x OLDPWD
declare -x PATH="/usr/local/bin:/bin:/usr/bin:/home/computerlamp/bin"
declare -x PWD="/home/computerlamp"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x TERM="xterm-256color"
declare -x USER="computerlamp"

Let’s talk about a few of those from the list.

  1. USER . That’s my username!  Hi, I’m computerlamp.
  2. PWD  My current directory
  3. OLDPWD My previous directory.  There’s nothing set there because I haven’t done anything in this login aside from sit in my current directory
  4. HOME My home directory.  It’s the same as PWD right now, but it could be different.  It’s always set to my home directory.
  5. SHELL The shell I’m using, right now I’m using /bin/bash
  6. HOSTNAME That’s the hostname of this computer, which happens to be Alpha.

Now let’s talk about that PATH variable.  It’s several directories strung together with colons (:).   The path is all the directories that bash looks in when you want to execute a command.  My list is:

  1. /usr/local/bin/
  2. /bin/
  3. /usr/bin/
  4. /home/computerlamp/bin

When I try to run any command, bash starts by looking in /usr/local/bin/ for the command, then /bin/, next is /usr/bin/, and finally it tries /home/computerlamp/bin. If it can’t find it in any of those, it gives up and tells you that command isn’t available.

Now suppose I have two commands, /bin/Avenge and /usr/bin/Avenge.  One of them calls Tony Stark and the next calls Steve Rogers.

Captain America Civil War Clipart

Of course, at the end of Captain America:  Civil War, they are not getting along.

If I just run Avenge, it’s going to call Tony Stark by default.  If I want to call Steve Rogers, I’ll execute /usr/bin/Avenge.

crontab

Last post we talked about cron and the basic format of the crontab file. This time we’re going to talk about more advanced examples of crontab. Remember, the fields are:

field Allowed Values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

I want my computer to beep on Sunday afternoon at 3:15 pm. The first field is 15 and the second field is 15. It isn’t 3, because that would be 3:15AM. I don’t know about you, but I’m asleep then. crontab uses military time where you add 12 to any time after noon. So 6pm becomes 18 for the crontab. I don’t care about the actual day of the month or the month, I want it every Sunday. So the third and fourth entries are * for the wildcard. For the last entry I can use either 0, 7, or Sun. I like Sun because it’s more descriptive. My crontab looks like:

15 15 * * Sun /home/computerlamp/beep.sh

I’ve decided I want my computer to beep on January 1 at midnight. It’s a way to celebrate the New Year! Happy New Year *beep*. The first two entries are 0, because I want 12:00AM, which is 0 minutes and 0 hours. The third entry is 1, for the first day of the month and the fourth is 1 or Jan for the first month of the year. I don’t really care what day of the week the first day of the year is, so I can put a wildcard for the fifth column. That makes my crontab look like:

0 0 1 1 * /home/computerlamp/beep.sh

I could also make it look like:

0 0 1 Jan * /home/computerlamp/beep.sh

We’ve talked about the format of the crontab but I haven’t said anything about how to edit it. That’s a more complex problem and we’ll talk about it in the next post.