Linux Commands Everyone Should Know
Linux Commands Everyone Should Know
Background
Linux is a kernel
Foundation of the GNU/Linux OS
User commands are from GNU utilities
o Bash (default shell on most distributions) has many commands built in.
o Utilities like sed, awk, grep, etc.
o Everything that lies outside the kernel.
Most of what people call “linux” is actually a collection of GNU utilities and the Linux kernel assembled into a
distribution.
o ForeScout uses a customized version of CentOS, which in turn is Red Hat Enterprise, but with the Red Hat
copyrighted pieces (logos and other artwork) replaced.
You can download packages from the mirror links @ https://www.centos.org/download/mirrors/.
1
Training
done
o Breaking down the script
The first line specifies the shell to run the script.
This way you can specify a different shell than the one you’re in.
Examples in /bin/cshell, /usr/bin/perl, and many others.
The second line creates a variable “list” using the command “ls *.tgz”.
The third line creates another variable “f”, and says for every entry “f” in the list “list”…
Do the following command(s)
tar –zxf ($f the file from the list)
And done, there are no more commands.
Scripts can be as simple or complex as desired.
You can write whole programs in shell script.
For common tasks we do here in support, putting together a collection of scripts do run through common data
collections or to set up debug environments is recommended.
Maybe most interesting; everything is a file.
o Any command that works against a file will work against anything, since it’s all files.
Everything you never wanted to know about the Linux CLI, or 1001* super handy dandy really great
Linux shell commands with examples (*actual number may vary)
ls
o List file command, with a lot of optional switches
o ls –lh – list files in long form (-l) and human readable output (-h)
$ ls -lh
total 21M
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Desktop
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Documents
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Downloads
-rwxr-xr-x 1 rhughes Domain Users 21M Oct 17 14:15 ForeScout-va-10.2.5.7-102057001_unsigned.fpi
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Music
lrwxrwxrwx 1 rhughes Domain Users 38 Jul 18 13:02 mydocs -> '/cygdrive/c/Users/rhughes/My
Documents'
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Pictures
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Public
-rwxr-xr-x 1 rhughes Domain Users 582K Oct 17 14:16 rxvt-2.7.10.tar.gz
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Templates
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Videos
drwxr-xr-x+ 1 rhughes Domain Users 0 Oct 7 17:29 work
2
Training
$ ls -hlS
total 21M
-rwxr-xr-x 1 rhughes Domain Users 21M Oct 17 14:15 ForeScout-va-10.2.5.7-102057001_unsigned.fpi
-rwxr-xr-x 1 rhughes Domain Users 582K Oct 17 14:16 rxvt-2.7.10.tar.gz
lrwxrwxrwx 1 rhughes Domain Users 38 Jul 18 13:02 mydocs -> '/cygdrive/c/Users/rhughes/My
Documents'
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Desktop
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Documents
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Downloads
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Music
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Pictures
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Public
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Templates
drwxr-xr-x+ 1 rhughes Domain Users 0 Apr 28 10:30 Videos
drwxr-xr-x+ 1 rhughes Domain Users 0 Oct 7 17:29 work
$ ls -a
. .profile Music
.. .ssh mydocs
.bash_history .viminfo Pictures
.bash_profile .Xauthority Public
.bashrc .xsession-errors rxvt-2.7.10.tar.gz
.config Desktop Templates
.dbus Documents Videos
.inputrc Downloads work
.minttyrc ForeScout-va-10.2.5.7-102057001_unsigned.fpi
o ls –a will show all files and directories, including hidden files and directories
o ls -t
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
3
Training
.:
Desktop
Documents
Downloads
ForeScout-va-10.2.5.7-102057001_unsigned.fpi
meh.txt
Music
mydocs
Pictures
Public
rxvt-2.7.10.tar.gz
Templates
Videos
work
./Desktop:
…
./work/rxvt-2.7.10/W11/X11:
cursorfont.h
Intrinsic.h
keysym.h
keysymdef.h
Xatom.h
Xlib.h
Xlocale.h
Xmd.h
Xutil.h
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
4
Training
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
5
Training
? pattern Search backward in the file for the N-th line containing the pattern. The search starts at the
line immediately before the top line displayed.
grep – the bacon of shell commands
o Stands for Global Regular Expression Print
You really wanted to know that, didn’t you?
o Variations include egrep and fgrep
grep –e and egrep are the same thing.
fgrep and grep –F are the same thing.
o The grep command is most powerful when combined with regular expressions.
Covered lightly now, more in-depth coverage for regular expressions later.
o The basic usage is simply “grep < expression > file”.
o Or find out which files in a directory contain the search term with “grep –l < expression > [dir | file | wildcard
files]”.
$ grep -l make *
grep: autoconf: Is a directory
ChangeLog
config.log
configure
grep: doc: Is a directory
INSTALL
libtool
Makefile
Makefile.in
grep: rclock: Is a directory
README.configure
rxvt-2.7.10.lsm
grep: src: Is a directory
grep: W11: Is a directory
o You can also use grep to find out the line number your search term is in with “grep –n <expression> [dir | file |
wildcard files]”.
o Highlight matches as in the last example by using the following: grep --color, egrep --color=auto, or fgrep --
color=auto in addition to the other arguments.
o Find everything that doesn’t match the expression by using grep –v <expression>.
o Find everything that matches the expression regardless of case with “grep –i <expression>”.
o Match an expression plus X lines after the match by using “grep –A <NUM> <expression>”
o Match an expression plus X lines before the match by using “grep –B <NUM> <expression>”
o Include subdirectories in the search with “grep –r <expression> <wildcard mask>”.
The wc (word count) command is useful for finding out how many lines are in a file or in the output of another
command.
o Run “wc –l” to get a line count.
The sort command is also very useful.
o sort –u Sort unique
o sort –n Sort numerically
The “find” command is useful for finding files.
The find command has a number of options, so check the man page for complete details.
o Some of the most common are “-type”, “-size”, “-user”, “-group”, “-name”, and many, many others.
o These are most useful when used in the same command, such as “find –type f –user root –name “*filename*”.
You can optionally run a command against the found file.
o For example, if you want to find all regular files and run a grep against them, run “find –type f –exec grep
<something> {} \;”
The “-exec” switch means exec the command that follows.
The “{}” characters stand for the file and path the find command located.
The “\;” terminates the command.
Get and/or manipulate system information with the following commands:
o netstat – very versatile, and can show everything from routes and connections to interface statistics. Add “-c”
to any of the switches to get real-time monitoring similar to top.
Use netstat –rn to show routing
There are several flags that will be shown in the Flags column that help to understand the
routes:
A Receive all multicast at this interface.
B OK broadcast.
D Debugging ON.
M Promiscuous Mode.
O No ARP at this interface.
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
7
Training
Output is:
o Iface – interface name
o MTU – MTU size
o RX-OK – receive OK
o RX-ERR – receive errors
o RX-DRP – receive drops
o RX-OVR – receive overruns
o TX-OK – transmit ok
o TX-ERR – transmit errors
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
8
Training
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
9
Training
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
10
Training
[root@RHughes-CO6-64 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos6x-lv_root
12G 4.0G 7.0G 37% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 485M 60M 401M 13% /bootv
o As you can see, long mount point names sometimes cause odd wrapping.
o This is a limitation of the utility.
Learn what disk slice is mounted where with the “mount” command.
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
11
Training
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
12
Training
o If you run “file <core>” and the result is “data”, then the core is just garbage
dumped from memory and not a dump of the running process, so it’s not useful
for investigation.
Using the kill command
Very straightforward, just run “kill <pid>” to kill a process with the normal termination.
There are a number of other kill options as well when running “kill –SIGNAL_NUMBER
<pid>”
The most common is “kill -9 <pid>”, which is the SIGKILL (brutal kill) signal.
Another useful one is “kill -1 <pid>”, which is SIGHUP. This tells the process to “hangup”,
or re-read it’s config files but continue running.
Running “kill -6 <pid>” will force the process to dump core.
The “pidof” command will return the pid(s) of the named process process.
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
13
Training
This will dump the output to a file and follow processed forked from the main process.
You can also attach to a running process with “strace –f –s 1024 –o <output file> -p <pid of
process>”.
o You can view network traffic with the “tcpdump” command.
Run “tcpdump –s 0 –i <interface> -w <file name>”
This captures the entire packet (default is the first 32 bytes, which is just the headers and a little
data) on the interface specified by –i and writes to the file <file name>.
The files can be opened with wireshark.
If you omit the –w, then basic output is written to the screen.
You can also filter on hosts or ports.
Run “tcpdump –i <interface> host host1 and port port1”
This is also very flexible and you can chain as much as you want together.
Look up a tutorial on Berkley Packet Filter language for in-depth details.
Brief words about sed and awk
o Unlike other utilities, awk supports its own scripting.
o So many things can be done with awk that exploring them in any depth is beyond the scope of this document,
but a session specifically on awk might be done.
o A few examples of very basic usage will be presented later.
o The sed command is for stream editing.
The name “sed” actually stands for “stream editor”.
The most common use is when you need to change the same word in multiple places in one or more
files.
The syntax is “sed –i ‘s/old_word/new_word/g’ file”
The “–i” switch mean “in place”.
The “g” mean global, or all occurrences. Otherwise, only the first occurrence sed finds will
be changed.
I/O redirection in BASH
o Our appliances use the BASH shell (bourne again shell) as the default.
o You will mainly be concerned with standard out (stdout), which is the normal output from a command, and
standard error (stderr) which are things marked as errors.
Sometimes for some unknowable reason developers will choose to send all output to stderr.
They should die. Or at least have their keyboards taken away.
o Occasionally standard in (stdin) will be useful when you’re scripting.
o Redirection is straight forward.
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
14
Training
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
15
Training
o Escape sequence - An escape sequence is a way of indicating that we want to use one of our metacharacters
as a literal. In a regular expression an escape sequence involves placing the metacharacter \ (backslash) in
front of the metacharacter that we want to use as a literal, for example, if we want to find (s) in the target string
window(s) then we use the search expression \(s\) and if we want to find \\file in the target string c:\\file then
we would need to use the search expression \\\\file (each \ we want to search for as a literal (there are 2) is
preceded by an escape sequence \).
This is what the metacharacters do
o Brackets, Ranges and Negation
[ ] Match anything inside the square brackets for ONE character position, once and only once. For
example, [12] means match the target to 1 and if that does not match then match the target to 2
while [0123456789] means match to any character in the range 0 to 9.
- The - (dash) inside square brackets is the “range separator” and allows a range to be defined. In
our example above of [0123456789] we could rewrite it as [0-9].
You can define more than one range inside a list, for example, [0-9A-C] means check for 0
to 9 and A to C (but not a to c).
NOTE: To test for - inside brackets (as a literal) it must come first or last, that is, [-0-9] will
test for - and 0 to 9.
^ The ^ (circumflex or caret) inside square brackets negates the expression. We will see an alternate
use for the circumflex/caret outside square brackets later. Example: [^Ff] means anything except
upper or lower case F and [^a-z] means everything except lower case a to z.
Notes: There are no spaces between the range delimiter values, if there was, depending
on the range, it would be added to the possible range or rejected as invalid. Be very careful
with spaces.
Some regular expression systems, notably VBScript, provide a negation operator (!) for
use with strings. This is a non-standard feature and therefore the resulting expressions are
not portable.
Because of the dual nature of the caret you will frequency see expressions like [^"], [^<] or
[^,] which are typically used as separator triggers (when combined with iterations) for more
complex searches or when parsing HTML, comma delimited text, etc.
o Positioning (or Anchors)
^ The ^ (circumflex or caret) when not used inside square brackets (where it has a diffent meaning)
means look only at the beginning of the target string. For example, ^Win will not find Microsoft
Windows but ^Moz will find Mozilla Firefox.
$ The $ (dollar) means look only at the end of the target string, for example, fox$ will find a match in
'silver fox' since it appears at the end of the string but not in 'the fox jumped over the moon'.
. The . (period) means any character(s) in this position, for example, ton. will find tons, tone and tonn
in tonneau but not wanton because it has no following character. Further, ton. would find “wanton “
because of the trailing space.
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
16
Training
o Iteration 'metacharacters'
? The ? (question mark) matches when the preceding character occurs 0 or 1 times only, for
example, colou?r will find both color (u is found 0 times) and colour (u is found 1 time).
* The * (asterisk or star) matches when the preceding character occurs 0 or more times, for
example, tre* will find tree (e is found 2 times) and tread (e is found 1 time) and trough (e is found 0
times and thus returns a match only on the tr).
+ The + (plus) matches when the preceding character occurs 1 or more times, for example, tre+ will
find tree (e is found 2 times) and tread (e is found 1 time) but NOT trough (0 times).
{n} Matches when the preceding character, or character range, occurs n times exactly, for example,
to find a local phone number we could use [0-9]{3}-[0-9]{4} which would find any number of the form
123-4567. Value is enclosed in braces (curly brackets).
Note: The - (dash) in this case, because it is outside the square brackets, is a literal.
Louise Rains writes to say that it is invalid to commence a NXX code (the 123) with a zero
(which would be permitted in the expression above). In this case the expression [1-9][0-9]
{2}-[0-9]{4} would be necessary to find a valid local phone number.
{n,m} Matches when the preceding character occurs at least n times but not more than m times, for
example, ba{2,3}b will find baab and baaab but NOT bab or baaaab. Values are enclosed in braces
(curly brackets).
{n,} Matches when the preceding character occurs at least n times, for example, ba{2,}b will find
'baab', 'baaab' or 'baaaab' but NOT 'bab'. Values are enclosed in braces (curly brackets).
o More 'metacharacters'
() The ( (open parenthesis) and ) (close parenthesis) may be used to group (or bind) parts of a
search expression together. Officially this is called a subexpression (a submatch or group) and
subexpressions may be nested to any depth. Parentheses (subexpresions) also capture the
matched element into a variable that may be used as a backreference
| The | (vertical bar or pipe) is called alternation in techspeak and means find the left hand OR right
values, for example, gr(a|e)y will find 'gray' or 'grey'. Having found the literal characters 'gr' - if the
first test is not valid (a) the second will be tried (e), if the first is valid the second will not be tried.
Alternation can be nested within each expression, thus gr((a|e)|i)y will find 'gray', 'grey' and 'griy'.
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
17
Training
as a literal (remember that a literal in this context is the actual character without its special meaning).
In this case, you’re escaping two backslashes in order to find a literal “\\”. Normally this expression
would find only a single “\”. We also have a space, so we need to escape that as well. Otherwise,
the grep command would see the part of the expression before the space as the whole expression
and the part after as the file to grep.
o Or another way would be:
egrep –A6 ^subkey
The word “subkey” prefaces registry keys, so this matches only lines that start with this
specific word.
find And remember the ^ metacharacter means that the expression only matches lines that
start with the expression, whereas “word$” only matches lines ending with the expression.
o Find all occurrences of the word “error”, then filter it down to those occurrences which also contain a specific
IP address:
grep error *.log|grep 12.4.5.87
o Find all occurrences of the work “error”, filter that down to those occurrences containing a specific IP, then
further filter the output to that which doesn’t contain the phrase “fingerprint=Windows Machine”
grep error *.log |grep 172.27.43.177 |grep -v fingerprint\=Windows\ Machine
o Tail a logfile while looking for a specific IP or phrase.
tail –f </path/to/file> |grep <ip address>|<phrase>
o Find all instances of a MAC address in a log.
egrep "([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])" <logfile>
o A couple of tcump dump examples:
tcpdump –i eth0 –s0 host 1.2.3.4 and host 4.3.2.1 port 80
This captures all traffic using a source or destination port of 80 between the hosts 1.2.3.4
and 4.3.2.1.
tcpdump –i eth0 not port 22
Captures everything that is not using a source or destination port of 22.
tcpdump –i eth0 src host 1.2.3.4 and dst host 4.3.2.1
Captures traffic leaving 1.2.3.4 for 4.3.2.1
tcpdump –i eth0 ether host <mac address>
Captures traffic to or from a specific MAC address regardless of IP.
tcpdump –i eth0.100
Captures traffic on eth0 vlan 100.
tcpdump –i eth0 arp
Captures all arp traffic on eth0.
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
18
Training
© 2016. ForeScout Technologies, Inc. is a privately held Delaware corporation. ForeScout, the ForeScout logo, ControlFabric,
CounterACT Edge, ActiveResponse and CounterACT are trademarks or registered trademarks of ForeScout. Other names
mentioned may be trademarks of their respective owners.
19