Linux System Fundamentals

Today's Agenda

images/Tux.png

Operating Systems

The Terminal

images/crashcart.jpg
images/televideo_terminal.jpg
images/teletype_terminal.jpg

Basic Shell Commands

Note

Explain architecture:
 built in commands vs. external binaries
Demo commands:Directory movement and file manipulation: Cd, pwd, ls, rm, mv, touch
User info:id, whoami, w
Pipes:redirection (pipe.txt, redirect.txt)
Special variables:
 $?, $$ (pid.sh), !!, !*, !$
images/bash.png
test@x230 ~ $ tree
.
├── Documents
│   ├── Code
│   │   └── scripts
│   │       └── test.sh
│   ├── School
│   └── Work
└── Pictures
    ├── manatee.gif
    └── turtle.png

6 directories, 5 files

Invoking a script

Note

Permissions discussed later.

$ ls -l
$ chmod +x $filename
$ ./$filename

Arguments (or flags) are extra information that you pass to a script or program when you call it. They tell it in more detail what you want to do.

$ ls -a -l
$ ls -al
$ ls -si
$ ls --si

File Paths

test@x230 ~ $ ls
Documents  Pictures
test@x230 ~ $ cd Documents/
test@x230 ~/Documents $ ls
Code  School  Work
test@x230 ~/Documents $ pwd
/home/test/Documents

Note

root directory is not to be confused with a home directory for the root account

Special Characters

images/xkcd_regex.png

Type less

images/space_cadet_keyboard.gif

Automation > Typing > Mouse

Help, get me out of here!

images/exit.jpg

Knowledge Check

test@x230 ~ $ tree
.
├── Documents
│   ├── Code
│   │   └── scripts
│   │       └── test.sh
│   ├── School
│   └── Work
└── Pictures
    ├── manatee.gif
    └── turtle.png
6 directories, 5 files

More about Man Pages

Documentation

Man pages, blogs you find by Googling, StackOverflow

images/google.gif

Asking for help

It's okay to ask.

  1. What should be happening?
  2. What's actually happening?
  3. Google it
  4. Skim the manuals of each component
  5. Identify a friend, mentor, or IRC channel who could help
  6. When they're not busy, give them a quick synopsis of points 1 and 2, mentioning what possibilities you've ruled out by searching.

Contributions = expertise + time

Don't waste experts' time, but do build your expertise.

Review

What are users?

$ whoami    # your username
$ who       # who is logged in?
$ w         # who is here and what are they doing?
$ id        # user ID, group ID, and groups you're in

Users have

Managing users

$ cat /etc/passwd
# username:x:UID:GID:GECOS:homedir:shell
$ useradd $USER # vs adduser, the friendly Ubuntu version
$ userdel $USER
$ passwd
images/xkcd215.png
# GECOS: full name, office number and building, office phone extension,
# home phone number (General Electric Comprehensive Operating System)
$ chfn # change GECOS information; only works sometimes
$ finger # tells you someone's GECOS info

Passwords

test@x230 ~ $ ls -l /etc/ | grep shadow
-rw-r-----  1 root shadow   1503 Nov 12 17:37 shadow

$ sudo su -
$ cat /etc/shadow
daemon:*:15630:0:99999:7:::
bin:*:15630:0:99999:7:::
sys:*:15630:0:99999:7:::
mail:*:15630:0:99999:7:::

# name:hash:time last changed: min days between changes: max days
#    between changes:days to wait before expiry or disabling:day of
#    account expiry

$ chage # change when a user's password expires

Root/Superuser

images/xkcd149.png

Acting as another user

$ su $USER          # become user, with THEIR password
$ su                # become root, with root's password
$ sudo su -         # use user password instead of root's
$ sudo su $USER     # become $USER with your password
images/xkcd_838.png
If someone has permissions errors:
  • Check that they or their group owns the files
  • Check that they have the flag +x to execute

What are groups?

$ groupadd
$ usermod
$ groupmod
$ cat /etc/group
    root:x:0:
    daemon:x:1:
    bin:x:2:
    sys:x:3:
    adm:x:4:
    tty:x:5:
# group name:password or placeholder:GID:member,member,member

Hands-On: Users and Groups

Note

To give yourself sudo powers do the following:

  1. Add your user to the wheel group using gpasswd.

  2. As the root user, use visudo and uncomment this line:

    pcwheel  ALL=(ALL)   ALL
    
  3. Save the file and now you should have sudo!

We'll cover sudo in more depth at a later time.

What are files?

test@x230 ~ $ ls -il
total 8
2884381 drwxrwxr-x 5 test test 4096 Nov  6 11:46 Documents
2629156 -rw-rw-r-- 1 test test    0 Nov 13 14:09 file.txt
2884382 drwxrwxr-x 2 test test 4096 Nov  6 13:22 Pictures

File extensions

$ file $FILENAME # tells you about the filetype

test@x230 ~ $ file file.txt
file.txt: ASCII text

test@x230 ~ $ file squirrel.jpg
squirrel.jpg: JPEG image data, JFIF standard 1.01

ls -l

$ ls -l
drwxrwxr-x 5 test test 4096 Nov  6 11:46 Documents
-rw-rw-r-- 1 test test    0 Nov 13 14:09 file.txt
drwxrwxr-x 2 test test 4096 Nov  6 13:22 Pictures

chmod and octal permissions

+-----+--------+-------+
| rwx | Binary | Octal |
+-----+--------+-------+
| --- | 000    | 0     |
| --x | 001    | 1     |
| -w- | 010    | 2     |
| -wx | 011    | 3     |
| r-- | 100    | 4     |
| r-x | 101    | 5     |
| rw- | 110    | 6     |
| rwx | 111    | 7     |
+-----+--------+-------+

chown, chgrp

user & group

# Change the owner of myfile to "root".
$ chown root myfile

# Likewise, but also change its group to "staff".
$ chown root:staff myfile

# Change the owner of /mydir and subfiles to "root".
$ chown -hR root /mydir

# Make the group devops own the bootcamp dir
$ chgrp -R devops /home/$yourusername/bootcamp

Types of files

drwxrwxr-x      5 test    test      4096    Nov  6 11:46 Documents
-rw-rw-r--      1 test    test         0    Nov 13 14:09 file.txt
drwxrwxr-x      2 test    test      4096    Nov  6 13:22 Pictures
----------     -------  -------  -------- ------------ -------------
    |             |        |         |         |             |
    |             |        |         |         |         File Name
    |             |        |         |         +---  Modification Time
    |             |        |         +-------------   Size (in bytes)
    |             |        +-----------------------        Group
    |             +--------------------------------        Owner
    +----------------------------------------------   File Permissions

- is a normal file

d is a directory

b is a block device

Hands-On: Files and Permissions

$ touch foo # create empty file called foo

Package Management

Take care of installation and removal of software

Core Functionality: * Install, Upgrade & uninstall packages easily * Resolve package dependencies * Install packages from a central repository * Search for information on installed packages and files * Pre-built binaries (usually) * Find out which package provides a required library or file

Popular Linux Package Managers * .deb / APT + dpkg (used by Debian, Ubuntu, Linux Mint) * .rpm / YUM + rpm (used by RedHat, CentOS, Fedora)

Language-specific Package Managers

Other Package Managers

They each fill a specific niche and have their own pros and cons.

Installing from source

$ wget http://mirrors.kernel.org/gnu/grep/grep-2.15.tar.xz
$ tar -Jxvf grep-2.15.tar.xz
$ cd grep-2.15
$ ./configure --prefix=$HOME/programs/
$ make
$ make install

Hands-on: Package Management

Review

IRC

images/multiple_networks.gif

A Client

Note

Switch to a terminal and show example

Use irssi or weechat in screen

# This step is optional, but persistent IRC is cool
$ ssh <username>@<preferred shell host>

# start screen with the name 'irc'
$ screen -S irc

# start your client in the 0th window of the screen session
$ irssi
# or
$ weechat-curses

# exit irc screen with CTRL+a, CTRL+d
# exit ssh session with CTRL+d or 'exit'
# to get back to irc:
$ ssh <username>@<preferred shell host>
$ screen -dr IRC

Networks

images/multiple_networks.gif
/connect irc.freenode.net

/nick <myawesomenickname>
/msg nickserv register <password> <email>

/nick <myawesomenickname>
/msg nickserv identify <password>

Channels

/join #osu-lug
/join #devopsbootcamp
/list:
  • tells all channels on network
  • Don't do this on Freenode!
/topic:

tells you the current channel's topic

/names:

tells you who's here

Commands

/say $thing
/join, /part, /whois <nick>, /msg, /help <command>

Note that nothing shows up in the channel when you run a /whois command; it shows up either in your status buffer or your conversation with the person.

12:04 -!- _test_ [~test@c-50-137-46-63.hsd1.or.comcast.net]
12:04 -!-  ircname  : Example User
12:04 -!-  channels : #ExampleChannel
12:04 -!-  server   : moorcock.freenode.net [TX, USA]
12:04 -!-  hostname : c-50-137-46-63.hsd1.or.comcast.net 50.137.46.63
12:04 -!-  idle     : 0 days 0 hours 2 mins 38 secs [signon: Wed Nov  6
12:00:30
                      2013]
12:04 -!- End of WHOIS

Useful tricks

Screen & Irssi Hints

SCREEN(1)                                                               SCREEN(1)

NAME
       screen - screen manager with VT100/ANSI terminal emulation

SYNOPSIS
       screen [ -options ] [ cmd [ args ] ]
       screen -r [[pid.]tty[.host]]
       screen -r sessionowner/[[pid.]tty[.host]]
Manual page screen(1) line 1 (press h for help or q to quit)

Etiquette

Terminology

images/jargon.jpg

Review