LAB 04 - String processing, Process management, Networking, SSH, YUM/RPM, Printing

From JaxHax
Jump to: navigation, search

string processing tools

One of the very powerful tools you have in linux are the string processing tools, they allow you to manipulate information in a efficient manner that is difficult to accomplish otherwise. In this section we will cover grep, wc, sort, uniq, awk, sed, diff and tr. First lets populate a file with some test patterns:

[dan@rhcsa01 ~]$ echo "foo
> bar
> tall
> short
> tall short
> short tall
> random01
> random02
> random with spaces
> RANDOM01
> RANDOM02
> RANDOM WITH SPACES" > test

now we can cat the file we just made to check on how it looks

[dan@rhcsa01 ~]$ cat test
foo
bar
tall
short
tall short
short tall
random01
random02
random with spaces
RANDOM01
RANDOM02
RANDOM WITH SPACES

lets play with some grep and regex now grep for foo

[dan@rhcsa01 ~]$ grep foo test
foo

grep for tall

[dan@rhcsa01 ~]$ grep tall test
tall
tall short
short tall

grep for any line that *begins* with tall (the ^)

[dan@rhcsa01 ~]$ grep ^tall test
tall
tall short

grep for any line that *ends* with short (the $)

[dan@rhcsa01 ~]$ grep short$ test
short
tall short

grep for the word random, notice it does not include the capitals

[dan@rhcsa01 ~]$ grep random test
random01
random02
random with spaces

grep for the word random, but ignore capitalization (-i)

[dan@rhcsa01 ~]$ grep -i random test
random01
random02
random with spaces
RANDOM01
RANDOM02
RANDOM WITH SPACES

grep for the word random, case insensive (-i) but has two characters then the end of the line (each . is a single random character like * means anything)

[dan@rhcsa01 ~]$ grep -i random..$ test
random01
random02
RANDOM01
RANDOM02

wc - by default gives you newline, word and byte counts for given input

[dan@rhcsa01 ~]$ wc test
 12  18 115 test

sort, sorts input, can take arguments to sort in different ways

[dan@rhcsa01 ~]$ sort test
bar
foo
random01
RANDOM01
random02
RANDOM02
random with spaces
RANDOM WITH SPACES
short
short tall
tall
tall short

uniq - this command give you unique entries, generally combined with sort. first lets make a new test file

[dan@rhcsa01 ~]$ echo "foo
> foo
> foo
> bar
> foo
> phil
> bob
> bob
> terry
> phil" > uniqtest

now lets do some uniq-ing, notice that it gets rid of some of the repetitions, however there are still repetitions, that is becuase uniq looks one line to the next

[dan@rhcsa01 ~]$ uniq uniqtest 
foo
bar
foo
phil
bob
terry
phil

so to get the output we want, we should combine sort and uniq

[dan@rhcsa01 ~]$ sort uniqtest | uniq
bar
bob
foo
phil
terry

sweet, now we have our uniq output, and it's even sorted already. What if we wanted to see how many unique entries each item had? What is happening below: the sort command sorts the input file uniqtest, then that stdout is redirected via the | to uniq, which condenses the list, count them, then outputs to the next command sort again, which reverse sorts the number and gives the output below

[dan@rhcsa01 ~]$ sort uniqtest | uniq -c | sort -r
      4 foo
      2 phil
      2 bob
      1 terry
      1 bar

awk / sed: there are entire books on this subject, we will not be going too in depth on this but here is some basic usage. Both utilities are EXTREMELY useful and you would do well to learn them. awk: if you want to tear apart something, and pull out exactly what you want, you can use awk. The command below echos a statement, pipes it to awk, which then pulls out select items. In awk, the default field seperator is a space, the $# is which field you are trying to get, and the things in quotes are direct output.

[dan@rhcsa01 ~]$ echo "bob is a terrible person, phil does not like him" | awk '{print $1 " " $9 "s " $6}'
bob likes phil

sed (stream editor): if you want to change select things inside a stream with the ease you can in vim, you're in for a treat! sed is a great utility to use to do quick in-line changes to streams. The below edit changes a few things about the statement we played with in awk, namely it finds and replaces a few key words. The first modification replaces the word terrible with wonderful, the second modification replaces "not " with nothing (deletes it):

[dan@rhcsa01 ~]$ echo "bob is a terrible person, phil does not like him" | sed 's/terrible/wonderful/' | sed 's/not //'
bob is a wonderful person, phil does like him

diff: this program allows you to see the differences between two files, similarties are not shown by default, and the > and < symbols are used to define which file the difference comes from and it tells you what line as well. This command is commonly used with the sort command.

[dan@rhcsa01 ~]$ echo "item01
> item02
> item03
> item05
> item06
> item08
> item09" > diff01
[dan@rhcsa01 ~]$ echo "item01
> item02
> item04
> item05
> item06
> item07
> item10" > diff02
[dan@rhcsa01 ~]$ diff diff01 diff02
3c3
< item03
---
> item04
6,7c6,7
< item08
< item09
---
> item07
> item10

tr: translate, you can change items fairly easily using this tool, for instance, if you want something converted to all upper or all lower, you can use something like the example below:

[dan@rhcsa01 ~]$ echo "I'm a little teapot" | tr [:lower:] [:upper:]
I'M A LITTLE TEAPOT

process management

Simply put a process is a program running on the machine. When you start a program, it spawns as a process, and you can interact with that process moving its priority on the resource stack up and down, and send it information such as kill signals or input. The process is represented by a PID or Process ID. Tools we will be going over in this section are top, htop, ps (aux), states, nice/renice, signals, jobs/backgrounding/foregrounding, and cron. ps aux: ps allows you to see a snapshot of the current running processes on the machine, the aux are options to this command. "a" shows all processes, not just your own. "u" shows usernames associated with the processes, and "x" shows processes that have no tty.

[dan@rhcsa01 ~]$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  19228  1504 ?        Ss   11:40   0:01 /sbin/init
root         2  0.0  0.0      0     0 ?        S    11:40   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    11:40   0:00 [migration/0]
root         4  0.0  0.0      0     0 ?        S    11:40   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S    11:40   0:00 [migration/0]
root         6  0.0  0.0      0     0 ?        S    11:40   0:00 [watchdog/0]
root         7  0.0  0.0      0     0 ?        S    11:40   0:00 [migration/1]
root         8  0.0  0.0      0     0 ?        S    11:40   0:00 [migration/1]
root         9  0.0  0.0      0     0 ?        S    11:40   0:00 [ksoftirqd/1]
root        10  0.0  0.0      0     0 ?        S    11:40   0:00 [watchdog/1]
root        11  0.0  0.0      0     0 ?        R    11:40   0:01 [events/0]
root        12  0.0  0.0      0     0 ?        S    11:40   0:03 [events/1]
root        13  0.0  0.0      0     0 ?        S    11:40   0:00 [cgroup]
root        14  0.0  0.0      0     0 ?        S    11:40   0:00 [khelper]
root        15  0.0  0.0      0     0 ?        S    11:40   0:00 [netns]
root        16  0.0  0.0      0     0 ?        S    11:40   0:00 [async/mgr]
root        17  0.0  0.0      0     0 ?        S    11:40   0:00 [pm]
root        18  0.0  0.0      0     0 ?        S    11:40   0:00 [sync_supers]
root        19  0.0  0.0      0     0 ?        S    11:40   0:00 [bdi-default]
root        20  0.0  0.0      0     0 ?        S    11:40   0:00 [kintegrityd/0]
root        21  0.0  0.0      0     0 ?        S    11:40   0:00 [kintegrityd/1]
root        22  0.0  0.0      0     0 ?        S    11:40   0:00 [kblockd/0]
root        23  0.0  0.0      0     0 ?        S    11:40   0:00 [kblockd/1]
root        24  0.0  0.0      0     0 ?        S    11:40   0:00 [kacpid]
root        25  0.0  0.0      0     0 ?        S    11:40   0:00 [kacpi_notify]
root        26  0.0  0.0      0     0 ?        S    11:40   0:00 [kacpi_hotplug]
root        27  0.0  0.0      0     0 ?        S    11:40   0:00 [ata/0]
root        28  0.0  0.0      0     0 ?        S    11:40   0:00 [ata/1]
root        29  0.0  0.0      0     0 ?        S    11:40   0:00 [ata_aux]
root        30  0.0  0.0      0     0 ?        S    11:40   0:00 [ksuspend_usbd]
root        31  0.0  0.0      0     0 ?        S    11:40   0:00 [khubd]
root        32  0.0  0.0      0     0 ?        S    11:40   0:00 [kseriod]
root        33  0.0  0.0      0     0 ?        S    11:40   0:00 [md/0]
root        34  0.0  0.0      0     0 ?        S    11:40   0:00 [md/1]
root        35  0.0  0.0      0     0 ?        S    11:40   0:00 [md_misc/0]
root        36  0.0  0.0      0     0 ?        S    11:40   0:00 [md_misc/1]
root        37  0.0  0.0      0     0 ?        S    11:40   0:00 [khungtaskd]
root        38  0.0  0.0      0     0 ?        S    11:40   0:00 [kswapd0]
root        39  0.0  0.0      0     0 ?        SN   11:40   0:00 [ksmd]
root        40  0.0  0.0      0     0 ?        SN   11:40   0:00 [khugepaged]
root        41  0.0  0.0      0     0 ?        S    11:40   0:00 [aio/0]
root        42  0.0  0.0      0     0 ?        S    11:40   0:00 [aio/1]
root        43  0.0  0.0      0     0 ?        S    11:40   0:00 [crypto/0]
root        44  0.0  0.0      0     0 ?        S    11:40   0:00 [crypto/1]
root        49  0.0  0.0      0     0 ?        S    11:40   0:00 [kthrotld/0]
root        50  0.0  0.0      0     0 ?        S    11:40   0:00 [kthrotld/1]
root        53  0.0  0.0      0     0 ?        S    11:40   0:00 [kpsmoused]
root        54  0.0  0.0      0     0 ?        S    11:40   0:00 [usbhid_resumer]
root        84  0.0  0.0      0     0 ?        S    11:40   0:00 [kstriped]
root       208  0.0  0.0      0     0 ?        S    11:40   0:00 [scsi_eh_0]
root       209  0.0  0.0      0     0 ?        S    11:40   0:00 [scsi_eh_1]
root       215  0.0  0.0      0     0 ?        S    11:40   0:00 [scsi_eh_2]
root       216  0.0  0.0      0     0 ?        S    11:40   0:00 [scsi_eh_3]
root       291  0.0  0.0      0     0 ?        S    11:40   0:00 [kdmflush]
root       293  0.0  0.0      0     0 ?        S    11:40   0:00 [kdmflush]
root       310  0.0  0.0      0     0 ?        S    11:40   0:00 [jbd2/dm-0-8]
root       311  0.0  0.0      0     0 ?        S    11:40   0:00 [ext4-dio-unwrit]
root       312  0.0  0.0      0     0 ?        S    11:40   0:00 [ext4-dio-unwrit]
root       390  0.0  0.0  11512  1548 ?        S<s  11:40   0:00 /sbin/udevd -d
root       880  0.0  0.0  12564  2872 ?        S<   11:40   0:00 /sbin/udevd -d
root       883  0.0  0.0      0     0 ?        S    11:40   0:00 [kdmflush]
root       919  0.0  0.0      0     0 ?        S    11:40   0:00 [jbd2/sda1-8]
root       920  0.0  0.0      0     0 ?        S    11:40   0:00 [ext4-dio-unwrit]
root       921  0.0  0.0      0     0 ?        S    11:40   0:00 [ext4-dio-unwrit]
root       922  0.0  0.0      0     0 ?        S    11:40   0:00 [jbd2/dm-2-8]
root       923  0.0  0.0      0     0 ?        S    11:40   0:00 [ext4-dio-unwrit]
root       924  0.0  0.0      0     0 ?        S    11:40   0:00 [ext4-dio-unwrit]
root       965  0.0  0.0      0     0 ?        S    11:40   0:00 [kauditd]
root       997  0.0  0.0  12300  2604 ?        S<   11:40   0:00 /sbin/udevd -d
root      1125  0.0  0.0      0     0 ?        S    11:40   0:00 [flush-253:0]
root      1166  0.0  0.0  27636  2836 ?        S<sl 11:40   0:00 auditd
root      1191  0.0  0.0 255416  1612 ?        Sl   11:40   0:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
dbus      1208  0.0  0.0  31908  1368 ?        Ssl  11:40   0:00 dbus-daemon --system
root      1219  0.0  0.1  97612  5528 ?        Ssl  11:40   0:00 NetworkManager --pid-file=/var/run/NetworkManager/NetworkM
root      1226  0.0  0.0  55816  2412 ?        S    11:40   0:00 /usr/sbin/modem-manager
root      1238  0.0  0.0 189200  3308 ?        Ss   11:40   0:00 cupsd -C /etc/cups/cupsd.conf
root      1241  0.0  0.0   9116  1532 ?        S    11:40   0:00 /sbin/dhclient -d -4 -sf /usr/libexec/nm-dhcp-client.actio
root      1244  0.0  0.0  44608  1004 ?        Ss   11:40   0:00 /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_suppli
68        1269  0.0  0.1  25380  4248 ?        Ss   11:40   0:00 hald
root      1270  0.0  0.0  18100  1292 ?        S    11:40   0:00 hald-runner
root      1311  0.0  0.0  20216  1244 ?        S    11:40   0:00 hald-addon-input: Listening on /dev/input/event3 /dev/inpu
68        1318  0.0  0.0  17800  1132 ?        S    11:40   0:00 hald-addon-acpi: listening on acpi kernel interface /proc/
root      1335  0.0  0.0  64116  1184 ?        Ss   11:40   0:00 /usr/sbin/sshd
root      1411  0.0  0.0  78720  3272 ?        Ss   11:40   0:00 /usr/libexec/postfix/master
postfix   1418  0.0  0.0  78972  3284 ?        S    11:40   0:00 qmgr -l -t fifo -u
root      1421  0.0  0.0 117208  1376 ?        Ss   11:40   0:00 crond
root      1432  0.0  0.0  21452   480 ?        Ss   11:40   0:00 /usr/sbin/atd
root      1445  0.0  0.0  67428  2016 ?        Ss   11:40   0:00 login -- root     
root      1447  0.0  0.0   4060   572 tty2     Ss+  11:40   0:00 /sbin/mingetty /dev/tty2
root      1449  0.0  0.0   4060   576 tty3     Ss+  11:40   0:00 /sbin/mingetty /dev/tty3
root      1451  0.0  0.0   4060   572 tty4     Ss+  11:40   0:00 /sbin/mingetty /dev/tty4
root      1453  0.0  0.0   4060   576 tty5     Ss+  11:40   0:00 /sbin/mingetty /dev/tty5
root      1455  0.0  0.0   4060   576 tty6     Ss+  11:40   0:00 /sbin/mingetty /dev/tty6
root      1475  0.0  0.0 1033628 3468 ?        Sl   11:40   0:00 /usr/sbin/console-kit-daemon --no-daemon
root      1542  0.0  0.0 108296  1896 tty1     Ss+  11:40   0:00 -bash
root      1560  0.0  0.0  97864  3804 ?        Ss   11:40   0:00 sshd: dan [priv] 
dan       1566  0.0  0.0  97864  1772 ?        S    11:41   0:01 sshd: dan@pts/0  
dan       1567  0.0  0.0 108300  1936 pts/0    Ss   11:41   0:00 -bash
postfix   1805  0.0  0.0  78800  3236 ?        S    13:20   0:00 pickup -l -t fifo -u
dan       1872  0.0  0.0 110224  1168 pts/0    R+   14:04   0:00 ps aux

Process states: this is what the process is currently doing, in the ps above you can see the state codes. The descriptions below are from the ps manual `man ps`, you can look it up yourself. PROCESS STATE CODES

      Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will
      display to describe the state of a process.
      D    Uninterruptible sleep (usually IO)
      R    Running or runnable (on run queue)
      S    Interruptible sleep (waiting for an event to complete)
      T    Stopped, either by a job control signal or because it is being traced.
      W    paging (not valid since the 2.6.xx kernel)
      X    dead (should never be seen)
      Z    Defunct ("zombie") process, terminated but not reaped by its parent.
      For BSD formats and when the stat keyword is used, additional characters may be displayed:
      <    high-priority (not nice to other users)
      N    low-priority (nice to other users)
      L    has pages locked into memory (for real-time and custom IO)
      s    is a session leader
      l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
      +    is in the foreground process group

nice/renice: the niceness of a process is it's willingness to either give up resources to other processes or take them from other processes. The nicer the process is the higher its niceness number (19 is the nicest) and the less nice something is it has a lower number (-20 is the least nice). For instance, if you want a very cpu intensive program you're running to give up cpu if other processes need it, you would make it very nice, however if you have something that is say very time sensitive to run, but does not take a machine-crippling load amount, you may want to make it not nice so it gets the resources it needs when it needs them at the expense of other processes. The default niceness level is 0 and you can change it on start of the program or using a command called renice. The nice command increases the niceness by 10 unless specified otherwise, and only privilaged users (root) can set a negative niceness level:

[dan@rhcsa01 ~]$ renice -n 10 1897 
1897: old priority 0, new priority 10
[dan@rhcsa01 ~]$ renice -n -10 1897
renice: 1897: setpriority: Permission denied

signals: these are a crude form of interprocess communication and are initated by process management and a command called kill. Common usage is kill PID or kill -9 PID. There is a list of all the signals if you run kill -l (listed below). kill is a nasty way of stopping a process and should be avoided unless you really need to use it, doing a kill -9 is like pressing and holding the power button on your computer.

[dan@rhcsa01 ~]$ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	
[dan@rhcsa01 ~]$ kill 1897
[1]+  Terminated              while true; do
    sleep 1;
done

jobs, backgrounding and foregrounding: job control allows you to run multiple processes in the shell that you are running in. For instance, if you want to wipe 50 disks at once, you can use job control to spawn 50 instances of dd without having to either wait on each one to finish or run them one at a time which would be terribly painful. below is an example of jobs and their control backgrounding: you can background a job by either appending a & at the end of the job or by hitting <ctrl>+z while the job is running in the foreground stops (pauses) the process, then typing `bg` when you get your shell back puts the job in the background:

[dan@rhcsa01 ~]$ while true; do sleep 1; done &
[1] 3067
[dan@rhcsa01 ~]$ while true; do sleep 1; done
^Z
[2]+  Stopped                 sleep 1
[dan@rhcsa01 ~]$ bg
[2]+ sleep 1 &

jobs: this command allows you to see what jobs you are currently running and what their state is as well as their PID:

[dan@rhcsa01 ~]$ jobs
[1]   Running                 while true; do
    sleep 1;
done &
[2]   Running                 while true; do
    sleep 1;
done &
[3]   Running                 while true; do
    sleep 1;
done &
[4]   Running                 while true; do
    sleep 1;
done &
[5]   Running                 while true; do
    sleep 1;
done &
[6]-  Running                 while true; do
    sleep 1;
done &
[7]+  Running                 while true; do
    sleep 1;
done &

foregrounding: you can foreground jobs as well if you type `fg` it will foreground the latest job, and if you do `fg #` # being the job number, you will foreground the job with the selected number:

[dan@rhcsa01 ~]$ jobs
[1]   Running                 while true; do
    sleep 1;
done &
[2]   Running                 while true; do
    sleep 1;
done &
[3]   Running                 while true; do
    sleep 1;
done &
[4]   Running                 while true; do
    sleep 1;
done &
[5]   Running                 while true; do
    sleep 1;
done &
[6]-  Running                 while true; do
    sleep 1;
done &
[7]+  Running                 while true; do
    sleep 1;
done &
[dan@rhcsa01 ~]$ fg
while true; do
    sleep 1;
done
^C
[dan@rhcsa01 ~]$ jobs
[1]   Running                 while true; do
    sleep 1;
done &
[2]   Running                 while true; do
    sleep 1;
done &
[3]   Running                 while true; do
    sleep 1;
done &
[4]   Running                 while true; do
    sleep 1;
done &
[5]-  Running                 while true; do
    sleep 1;
done &
[6]+  Running                 while true; do
    sleep 1;
done &
[dan@rhcsa01 ~]$ fg 3
while true; do
    sleep 1;
done
^C
[dan@rhcsa01 ~]$ jobs
[1]   Running                 while true; do
    sleep 1;
done &
[2]   Running                 while true; do
    sleep 1;
done &
[4]   Running                 while true; do
    sleep 1;
done &
[5]-  Running                 while true; do
    sleep 1;
done &
[6]+  Running                 while true; do
    sleep 1;
done &
[dan@rhcsa01 ~]$

cron: cron allows you to run jobs based on a timetable, you can configure it to run at most once a minute. You access cron via a crontab file, crontab -l lists it, crontab -e edits it. Crontab editor is the default system editor and it is modifiable via the EDITOR environment variable. the fields are <min> <hour> <day of month> <month> <day of week (0/7 are sunday)> <command>

30 5 * * * echo "this runs 5:30 am every day"
*/5 * * * * echo "this runs every 5 min"
00 0 5 6 7 echo "this runs at midnight (00:00) on month 6 day 5 as long as that day is a sunday

networking

/etc/sysconfig/network-scripts/ifcfg-eth#: this file contains the configuration for the ethernet device eth# (starting at 0) and the system uses this file to configure the system at boot. By default, unless you have installed via the network, ONBOOT will be no, to enable this interface on boot you need to change ONBOOT to yes, I also get rid of the hardware and uuid lines on a vm so I can change the hardware addresses and only deal with udev. You would add lines like NETWORK, IPADDR and NETMASK if you wanted to have a static assignment. I also turn off NM_CONTROLLED (Network Manager) because it commonly has a lot of issues and generally does not play well with manual configuration. You should not even have network manager on your server because it is a gui tool part of the desktop but I shut it off just in case. Contents of the modified file for dhcp setup are below:

[dan@rhcsa01 network-scripts]$ cat ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="dhcp"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"

After changing the network file above, you can do a `service network restart` and the changes will take effect. ifconfig: this utility allows you to manupulate the network interfaces directly as well as view the current status of them. In the sample below, the eth0 is the physical NIC (Network Interface Card) and lo is Local lOopback. ifconfig is marked for replacement and is being replaced, along with other tools, by the `ip` utility, which is also shown below.

[dan@rhcsa01 ~]$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:2B:70:88  
          inet addr:192.168.56.101  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe2b:7088/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5301 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2983 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:482428 (471.1 KiB)  TX bytes:677315 (661.4 KiB)
 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
 
[dan@rhcsa01 ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:2b:70:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global eth0
    inet6 fe80::a00:27ff:fe2b:7088/64 scope link 
       valid_lft forever preferred_lft forever

netstat -an: netstat is a network statistic program that allows you to see what the active connections of the network stack as well as UNIX domain sockets. netstat will be replaced and is marked as obsolete, from the man page: Replacement for netstat is ss. Replacement for netstat -r is ip route. Replacement for netstat -i is ip -s link. Replacement for netstat -g is ip maddr.

[dan@rhcsa01 ~]$ netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 192.168.56.101:22           192.168.56.1:39785          ESTABLISHED 
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN      
udp        0      0 0.0.0.0:631                 0.0.0.0:*                               
udp        0      0 0.0.0.0:68                  0.0.0.0:*                               
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  10     [ ]         DGRAM                    11281  /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     8442   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     11337  /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     12254  public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     12261  private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     12265  private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     12269  private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     12273  private/defer
unix  2      [ ACC ]     STREAM     LISTENING     12277  private/trace
unix  2      [ ACC ]     STREAM     LISTENING     12281  private/verify
unix  2      [ ACC ]     STREAM     LISTENING     12285  public/flush
unix  2      [ ACC ]     STREAM     LISTENING     12289  private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     12293  private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     12298  private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     12302  private/relay
unix  2      [ ACC ]     STREAM     LISTENING     12306  public/showq
unix  2      [ ACC ]     STREAM     LISTENING     12310  private/error
unix  2      [ ACC ]     STREAM     LISTENING     12314  private/retry
unix  2      [ ACC ]     STREAM     LISTENING     12318  private/discard
unix  2      [ ACC ]     STREAM     LISTENING     12322  private/local
unix  2      [ ACC ]     STREAM     LISTENING     12326  private/virtual
unix  2      [ ACC ]     STREAM     LISTENING     12330  private/lmtp
unix  2      [ ]         DGRAM                    8621   @/org/kernel/udev/udevd
unix  2      [ ACC ]     STREAM     LISTENING     12334  private/anvil
unix  2      [ ACC ]     STREAM     LISTENING     12338  private/scache
unix  2      [ ACC ]     STREAM     LISTENING     11555  /var/run/cups/cups.sock
unix  2      [ ]         DGRAM                    11665  @/org/freedesktop/hal/udev_event
unix  2      [ ACC ]     STREAM     LISTENING     11636  @/var/run/hald/dbus-myMrldGQ82
unix  2      [ ACC ]     STREAM     LISTENING     11643  @/var/run/hald/dbus-4zJPgZE6Es
unix  2      [ ]         DGRAM                    14982  
unix  3      [ ]         STREAM     CONNECTED     13228  
unix  3      [ ]         STREAM     CONNECTED     13227  
unix  2      [ ]         DGRAM                    13224  
unix  3      [ ]         STREAM     CONNECTED     13064  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     13063  
unix  3      [ ]         STREAM     CONNECTED     13038  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     13037  
unix  3      [ ]         STREAM     CONNECTED     13023  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     13022  
unix  2      [ ]         DGRAM                    12385  
unix  2      [ ]         DGRAM                    12369  
unix  3      [ ]         STREAM     CONNECTED     12341  
unix  3      [ ]         STREAM     CONNECTED     12340  
unix  3      [ ]         STREAM     CONNECTED     12337  
unix  3      [ ]         STREAM     CONNECTED     12336  
unix  3      [ ]         STREAM     CONNECTED     12333  
unix  3      [ ]         STREAM     CONNECTED     12332  
unix  3      [ ]         STREAM     CONNECTED     12329  
unix  3      [ ]         STREAM     CONNECTED     12328  
unix  3      [ ]         STREAM     CONNECTED     12325  
unix  3      [ ]         STREAM     CONNECTED     12324  
unix  3      [ ]         STREAM     CONNECTED     12321  
unix  3      [ ]         STREAM     CONNECTED     12320  
unix  3      [ ]         STREAM     CONNECTED     12317  
unix  3      [ ]         STREAM     CONNECTED     12316  
unix  3      [ ]         STREAM     CONNECTED     12313  
unix  3      [ ]         STREAM     CONNECTED     12312  
unix  3      [ ]         STREAM     CONNECTED     12309  
unix  3      [ ]         STREAM     CONNECTED     12308  
unix  3      [ ]         STREAM     CONNECTED     12305  
unix  3      [ ]         STREAM     CONNECTED     12304  
unix  3      [ ]         STREAM     CONNECTED     12301  
unix  3      [ ]         STREAM     CONNECTED     12300  
unix  3      [ ]         STREAM     CONNECTED     12297  
unix  3      [ ]         STREAM     CONNECTED     12296  
unix  3      [ ]         STREAM     CONNECTED     12292  
unix  3      [ ]         STREAM     CONNECTED     12291  
unix  3      [ ]         STREAM     CONNECTED     12288  
unix  3      [ ]         STREAM     CONNECTED     12287  
unix  3      [ ]         STREAM     CONNECTED     12284  
unix  3      [ ]         STREAM     CONNECTED     12283  
unix  3      [ ]         STREAM     CONNECTED     12280  
unix  3      [ ]         STREAM     CONNECTED     12279  
unix  3      [ ]         STREAM     CONNECTED     12276  
unix  3      [ ]         STREAM     CONNECTED     12275  
unix  3      [ ]         STREAM     CONNECTED     12272  
unix  3      [ ]         STREAM     CONNECTED     12271  
unix  3      [ ]         STREAM     CONNECTED     12268  
unix  3      [ ]         STREAM     CONNECTED     12267  
unix  3      [ ]         STREAM     CONNECTED     12264  
unix  3      [ ]         STREAM     CONNECTED     12263  
unix  3      [ ]         STREAM     CONNECTED     12260  
unix  3      [ ]         STREAM     CONNECTED     12259  
unix  3      [ ]         STREAM     CONNECTED     12257  
unix  3      [ ]         STREAM     CONNECTED     12256  
unix  3      [ ]         STREAM     CONNECTED     12252  
unix  3      [ ]         STREAM     CONNECTED     12251  
unix  3      [ ]         STREAM     CONNECTED     12248  
unix  3      [ ]         STREAM     CONNECTED     12247  
unix  2      [ ]         DGRAM                    12215  
unix  3      [ ]         STREAM     CONNECTED     11996  @/var/run/hald/dbus-myMrldGQ82
unix  3      [ ]         STREAM     CONNECTED     11995  
unix  3      [ ]         STREAM     CONNECTED     11907  @/var/run/hald/dbus-myMrldGQ82
unix  3      [ ]         STREAM     CONNECTED     11906  
unix  3      [ ]         STREAM     CONNECTED     11660  @/var/run/hald/dbus-4zJPgZE6Es
unix  3      [ ]         STREAM     CONNECTED     11659  
unix  3      [ ]         STREAM     CONNECTED     11638  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     11637  
unix  3      [ ]         STREAM     CONNECTED     11546  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     11545  
unix  2      [ ]         DGRAM                    11528  
unix  3      [ ]         STREAM     CONNECTED     11416  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     11415  
unix  2      [ ]         DGRAM                    11414  
unix  3      [ ]         STREAM     CONNECTED     11407  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     11406  
unix  3      [ ]         STREAM     CONNECTED     11391  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     11389  
unix  2      [ ]         DGRAM                    11383  
unix  3      [ ]         STREAM     CONNECTED     11357  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     11356  
unix  3      [ ]         STREAM     CONNECTED     11350  
unix  3      [ ]         STREAM     CONNECTED     11349  
unix  3      [ ]         DGRAM                    8643   
unix  3      [ ]         DGRAM                    8642 
[dan@rhcsa01 ~]$ ss
State       Recv-Q Send-Q                           Local Address:Port                               Peer Address:Port   
ESTAB       0      0                               192.168.56.101:ssh                                192.168.56.1:39785   
[dan@rhcsa01 ~]$ ip route
192.168.56.0/24 dev eth0  proto kernel  scope link  src 192.168.56.101  metric 1 
[dan@rhcsa01 ~]$ ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX: bytes  packets  errors  dropped overrun mcast   
    0          0        0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    0          0        0       0       0       0      
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:2b:70:88 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    519802     5727     0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    725489     3226     0       0       0       0      
[dan@rhcsa01 ~]$ ip maddr
1:	lo
	inet  224.0.0.1
	inet6 ff02::1
2:	eth0
	link  33:33:ff:2b:70:88
	link  01:00:5e:00:00:01
	link  33:33:00:00:00:01
	inet  224.0.0.1
	inet6 ff02::1:ff2b:7088
	inet6 ff02::1

email: electronic mail is one of the keystone technologies in computers and in multi-user systems you can send mail to one another without it ever leaving the system. We will not be discussing setting up a mail server in this beginner level section. mailx is one of many clients you can use, if you wish to send mail to someone else on the system, simply run `mailx username` then tell the system what your subject is, then a body and to send the email hit <ctrl>+d. To read the email, you can just invoke mailx and give it a command, the ? allows you to see a menu of options.

[dan@rhcsa01 ~]$ mailx
No mail for dan
[dan@rhcsa01 ~]$ mailx dan
Subject: hello from dan
hello to dan
EOT
[dan@rhcsa01 ~]$ mailx
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/dan": 1 message 1 new
>N  1 dan@rhcsa01.localdom  Thu Dec  5 17:39  18/595   "hello from dan"
& 1
Message  1:
From dan@rhcsa01.localdomain  Thu Dec  5 17:39:46 2013
Return-Path: <dan@rhcsa01.localdomain>
X-Original-To: dan
Delivered-To: dan@rhcsa01.localdomain
Date: Thu, 05 Dec 2013 17:39:46 -0500
To: dan@rhcsa01.localdomain
Subject: hello from dan
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: dan@rhcsa01.localdomain
Status: R
 
hello to dan
 
& d
& ?
               mailx commands
type <message list>             type messages
next                            goto and type next message
from <message list>             give head lines of messages
headers                         print out active message headers
delete <message list>           delete messages
undelete <message list>         undelete messages
save <message list> folder      append messages to folder and mark as saved
copy <message list> folder      append messages to folder without marking them
write <message list> file       append message texts to file, save attachments
preserve <message list>         keep incoming messages in mailbox even if saved
Reply <message list>            reply to message senders
reply <message list>            reply to message senders and all recipients
mail addresses                  mail to specific recipients
file folder                     change to another folder
quit                            quit and apply changes to folder
xit                             quit and discard changes made to folder
!                               shell escape
cd <directory>                  chdir to directory or home if none given
list                            list names of all available commands
 
A <message list> consists of integers, ranges of same, or other criteria
separated by spaces.  If omitted, mailx uses the last message typed.
& q

command line web browsers: these things are very useful, once in a while, and are a nice thing to have just in case. You can look things up, download stuff from websites, and check your own sites with them. There are two main ones, links and lynx. They both have their plusses and minuses, but I usually just use links if I need to. To invoke links, just run `links http://whatever.com` to open that site, then you can navigate with the arrow keys and q to quit.

[dan@rhcsa01 ~]$ links http://www.google.com

wget: wget is a utility that allows you to download things off the internet including files and entire sites. Usage is pretty simple, just `wget http/ftp://filelocation/filename

[dan@rhcsa01 ~]$ wget http://www.kernel.org/path/to/kernel/file

curl: curl allows you to do things like wget, but in its basic usage, it just dumps the output of whatever you point it at. So if you need to see if you're getting the correct site from http://www.jaxhax.org you would just do `curl http://www.jaxhax.org` and the site source would pop up on your terminal.

[dan@rhcsa01 ~]$ curl http://www.jaxhax.org

telnet: this utility is somewhat dated and was replaced by ssh a long time ago, however for troubleshooting it is a nice program still to use. If you want to see that a port is open for instance, you can telnet to it to see if you can connect to the port. Syntax is `telnet address port`.

[dan@rhcsa01 ~]$ telnet www.jaxhax.org 80

ftp: file transfer protocol is still highly used today, however you have to understand that EVERYTHING about it is unencrypted, your credentials, the files you transfer, the commands, EVERYTHING. ftp in interactive mode is pretty simple, the man page will give you more, but you start it by `ftp somehost` then log in with some credentials (sometimes anonymous) then you can do things like `ls` `cd` `put` and `get` all of which are pretty self explanitory.

[dan@rhcsa01 ~]$ ftp ftp.jaxhax.org

scp: one of the most used file transfer programs in linux for system to system communication, it means secure copy and is both encrypted and uses the ssh keys if you have them set up (this is discussed later). The example below would copy a file from your current directory to user's home on remotehost server and rename it fileto.ext.

[dan@rhcsa01 ~]$ scp filefrom.ext user@remotehost:~/fileto.ext

SSH

SSH or Secure SHell was a replacement for RSH or Remote SHell which had no encryption and for telnet which also has no encyrption. SSH is an extremely robust and useful progrem, not only can you use it to open a interactive shell, you can do non-interactive commands, open socks proxy tunnels, open children ssh shells from a master shell, copy files over the network via ssh (using it as a stream carrier) or scp along with many other things. SSH also keeps track of the other machine's RAS key fingerprints to try to prevent man in the middle attacks. Simple usage to get from one machine to another for the first time:

dan@icarus:~$ ssh 192.168.56.101
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.
RSA key fingerprint is ad:bf:42:d2:fd:dd:08:74:ea:da:09:3a:93:54:80:32.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.101' (RSA) to the list of known hosts.
dan@192.168.56.101's password: 
Last login: Thu Dec  5 11:41:15 2013 from 192.168.56.1
[dan@rhcsa01 ~]$

SSH Server is the server portion of SSH. It allows clients to connect to it and is very customizable. We will not go over the customization, suffice to say the configuration file is in /etc/ssh/sshd_config. To check the status of sshd or SSH Daemon, which should be running by default in CentOS, run the following command:

[root@rhcsa01 ~]# service sshd status
openssh-daemon (pid  1335) is running...

SSH Keygen: You can make keys for ssh to enable either logging in via no password, or if your server on the other side requires keys you can put a password in the key to make the login even more secure. SSH is extremely particular on its permissions for its keys directories, you need to have 700 access on .ssh and 600 access on your private key or the key will not work. The id_rsa in this case is your private key and the id_rsa.pub is your public key you give to other people and or servers. In most linux distributions you can use the `ssh-copy-id` program that will automatically build the directory and place the public key on the server that you send it to. If you build keys that have no password like below, then you will be able to, after pushing the public key out, log into machines without using a password.

[dan@rhcsa01 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dan/.ssh/id_rsa): 
Created directory '/home/dan/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/dan/.ssh/id_rsa.
Your public key has been saved in /home/dan/.ssh/id_rsa.pub.
The key fingerprint is:
e2:68:2a:fb:7d:c0:f0:7b:3d:f7:09:43:d9:e6:14:fa dan@rhcsa01
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|            .    |
|  .        + .   |
|   +  . S + +    |
|    +o . . =     |
|    oo..  o E    |
|.  +. o o .o .   |
|.+o .o   o .o    |
+-----------------+
[dan@rhcsa01 ~]$ ls -lad .ssh
drwx------. 2 dan dan 4096 Dec  5 18:49 .ssh
[dan@rhcsa01 ~]$ ls -la .ssh/
total 20
drwx------.  2 dan dan 4096 Dec  5 18:49 .
drwxrwx---+ 14 dan dan 4096 Dec  5 18:49 ..
-rw-------.  1 dan dan 1675 Dec  5 18:49 id_rsa
-rw-r--r--.  1 dan dan  393 Dec  5 18:49 id_rsa.pub
[dan@rhcsa01 ~]$ ssh-copy-id 192.168.56.1
The authenticity of host '192.168.56.1 (192.168.56.1)' can't be established.
RSA key fingerprint is 76:f4:e4:b6:7f:27:4e:ff:ad:c4:1d:f2:77:25:ff:0e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.1' (RSA) to the list of known hosts.
dan@192.168.56.1's password: 
Now try logging into the machine, with "ssh '192.168.56.1'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
[dan@rhcsa01 ~]$ ssh 192.168.56.1 uname -a
Linux icarus 3.11-2-amd64 #1 SMP Debian 3.11.8-1 (2013-11-13) x86_64 GNU/Linux

Interactive vs non-interactive login via ssh: Interactive login via SSH is simple, you ssh to a server, put in your password or automatically log in via a key and you are now in interactive mode. If you choose to run a single or set of commands via ssh but do not want to have an interactive shell, you can string those commands into the ssh command and you can execute them on the other system without going into interactive mode. This is useful when you have a lot of servers you want some kind of information on or where you want to do a particular activity on a lot of machines.

[dan@rhcsa01 ~]$ ssh 192.168.56.1
dan@icarus:~$ echo this is interactive mode
this is interactive mode
dan@icarus:~$ uname -a
Linux icarus 3.11-2-amd64 #1 SMP Debian 3.11.8-1 (2013-11-13) x86_64 GNU/Linux
dan@icarus:~$ logout
Connection to 192.168.56.1 closed.
[dan@rhcsa01 ~]$ ssh 192.168.56.1 "echo 'this is non-interactive mode'; uname -a"
this is non-interactive mode
Linux icarus 3.11-2-amd64 #1 SMP Debian 3.11.8-1 (2013-11-13) x86_64 GNU/Linux

YUM/RPM

YUM stands for Yellowdog Update Manager, Yellowdog was a derivitave of Red Hat Linux, they made a package manager that was better then Up2Date so RedHat adopted it and it is now the default package manager for RHEL/Fedora/CentOS. YUM usage is pretty easy, `yum command action`. If you wanted to install for instance links, you would do `yum install links`.

Yum configuration lies in files contained in /etc/yum.repos.d/*.conf If you want to add a repo, or change something about a repo, the files in that directory are what you'll need to interact with

RPM stands for Redhat Package Manager, it is a good system that YUM relies on as a backend but is very old. files that end in .rpm are files that this utility uses to install. These files contain not only the package, but where the files go, install scripts, and dependencies of the package that need to be installed prior to the package being installed. Usage for install is `rpm -U foo.rpm` and that 'updates' the package which is safer then 'installing it' in case the package already exists, it will just update it instead of leaving the system in a strange state. `rpm -qa` will show all of the packages that are installed on the system.

Printing

Linux printing is handled by the CUPS system or Common Unix Printing System. When you install it you can access it via http://localhost:631 You can also access it using system-config-printer which is a redhat utility that can also configure the printers, it works pretty well.