Notes to be organized to Wiki Pages later
Common Commands, nagivating the file system
ls – list dir contents
ls -al – list all dir contents, in long format.
ls -F – list contents, dirs noted with / char. BEAUTIFUL!
mkdir mydir – create dir mydir
cd dir – change directory to dir
pwd – print working dir
mv file1 file2 – move/rename a file from file1 to file2
tail myfile – read last few lines of myfile
head myfile – reas first few lines of myfile
cat myfile – concat files, otherwise displays myfile contents to stout
rm myfile – removes myfile
cd .. – cd's one level up(or back) in file structure.
cd <CR>, cd ~ – quickly cd to home dir
mv mydir1 mydir2 – moves directories also
rmdir – remove empty dirs
rm -r – remove dir recursively (Kills all subdirs)
touch myfile – creates empty file
cp -r mydir mydir2 – copies directory recursively (all subdirs)
services sshd restart – restarted sshd service on X distribution (WHICH ONE?!?!)
less log.file – page through log.file
tee – used in pipes to display stdout as well as output to a file: echo $PATH | tee path.txt
ls | xargs rm – run rm command on each line outputted from ls. xargs is amazzzing!
rm $(find ./ -user Christine – rm all files listed from the find command in parens. Amazzzinging!
expand file – converts tabs to spaces in file file.
unexpand file – converts spaces to tabs in file.
od file.txt – display file in octal format. Can display other formats, consult man.
join file1 file2 – joins lines together based on a key column.
paste file1 file2 – merge lines in file1 and file2
sort -k 3 file.txt – Sort file.txt by field 3 (default is 1).
tr BCJ bc > listing.txt – translate all BCJ chars to bc. -d flag deletes chars entirely in set1.
sort shakespeare.txt | uniq – uniq removes duplicate lines from shakespeare.txt
fmt long.txt – format line length and etc in long.txt
Important Places
ls long output
Files preceeded by a . are hidden/config files and won't show unless -a flag is used in ls cmd.
ls readout: perms, ?, user, group, file, last touched, name.
first perm bit 'd' means it's a dir.
Regular Expressions
Any Single Character
A dot will match any single char except newlines: a.z will match abz aQz a9Z and etc.
Repetition Operators
A regex may be followed by a special symbol to denote how many times a matching item must exist.
Multiple Possible Strings:
Vertical Bar (|) separates two possible matches:
Parentheses
Surrounds subexpressions (no example in chapter 1)
Escaping
If you want to match special chars like a dot (.) you must escape it with a backslask: \. (Not unlike bash and python)
grep
Searches for patterns:
grep [options] regexp [files]
flags:
-c # Instead of displaying lines, it displays the count.
-f # Takes pattern input from a file and not stdin
-i # Ignore Case
-r or rgrep # Search recursively.
-F or –fixed-strings or fgrep # Searches for literal translation, special symbols like $ are now literally printed as $ and not interpreted.
-E or –extended-regexp or egrep # Uses extended regexp as default
sed
sed 's/2012/2013/' file.txt > new.txt # Change first occurrence of 2012 to 2013 and redirect output to new.txt
File Permissions
ls
$ ls -la:
-rw-r–r– 1 abrer abrer 255 Jan 31 18:37 TeamSpeak3CABDO-ID.ini
TYPE OWNER GROUP EVERYONE/WORLD, OWNER, GROUP, SIZE, DATE Moded, TIME moded, NAME
Can change default permissions with umask.
chmod
numeric or symbolic modes.
numeric = chmod 755
4 = read
2 = write
1 = execute
dirs need x to execute
symbolic = chmod g+x myfile
chmod u+rwx,g+rwx,o-rw myfile
u = user/owner
g = group
o = other/world/guest
Set user to group
chown
: or . works as seperator
Cron
Scheduled task system. Stateless, doesn't remember previous jobs.
cron.deny: Users in this list are denied use of cron
cron.allow: If this file exists, everyone is denied unless specified here.
crontab: The schedule file.
Sample Syntax:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/2 1 1,15 * * root echo "Hello world!" > /tmp/hello.txt # 1st and 15th day of month
# /2 = every 2
# 1,15 = 1st and 15th
# 1-5 = 1st through 5th
Otherwise, symlinking scripts/programs to cron.daily,monthly, weekly, hourly, etc folders will work.
user cron
crontab -e to edit user cron, independent from system cron.
Package Management
rpm/yum - Redhat, Centos, Fedora, Suse
apt-get/dpkg - Debian, Ubuntu, etc
CENTOS
rpm
-ivh packagefile - Installs packagefile with a series of hashes as indicators.
-uvh - Same as -ive but upgrades a a package if already installed?
-q pkgname - Queries to see if packagename is installed.
-e pkgname - remove pkgname from system.
yum
yum makecache fast - updates local cache of repo contents. Similar to Pacman -Syy
yum search name - searches repos for pkgs named name
yum install httpd - installs httpd and deps from repos.
yum update – updates all packages on system.
UBUNTU
apt
Config file located in /etc/apt. sources.list important. sources.list.d folder contains official repos.
apt-get update – updates repo list. DOES NOT UPGRADE.
apt-cache search thing – searches packages on local package cache for thing to download from server.
apt-get install apache2 - install apache2 + deps from repos.
apt-get upgrade – upgrades out of date packages.
top
top stuff.
Sys time, sys uptime, users logged in, load avg (cpu utilization .90 = 90%): 1m, 5m, 15m
CPU
Mem
Swap
shift M/N? - sort by mem
shift P - sort by cpu
NI = nice val. nice vals go from -20 to 19. 19 = LOWEST PRI
R = renice value - PID - renice val
K = kill PID [15 = term(inate), kill graceful] [9 = kill, kill forcefully]
Managing Shared Libraries
Code/functions compiled into files called libraries that other programs/code can call on, reducing code and disk space by not having 50 variations of code that do the same thing.
Managing Processes
Managing Hardware
BIOS
Enumerates hardware before OS is loaded. Drivers will determine how hardware acts in Linux OS. May need to drownload drivers from manufacturers for optimal performance.
Drivers
drivers loaded in /sys/bus/pci/drivers.
probs
If problems present, check bios. Check drivers! Check lsmod, lspci, lsusb. use modprobe to load modules. Check with the vendor.
REDIRECT VIDEO TO SERIAL! - Real World examples
Not on exam, but steps demo underlying system that are on exam.
serial --unit 1 --speed=9600
terminal --timeout 300 console serial
LVM - Logical Volume Manager
Logicall manages volumes/partitions, create and resize across multiple disks.
Traditional storage devices look like /dev/sda,b,c,d,etc
LVM sorts volumes into directories by volume group
yum install lvm2
Create the physical volumes
Create Volume Group (To hold partitions)
Check your work.
Create logical volumes (Partitions)
Filesystem Health
/etc/fstab
Mounts devs on boot.
device mount FS Options - Dump - Pass(fschk)
/dev/sda1 / ext4 defaults 0|1 0|1|2
device = device
mount = mount location
FS = filesystem to expect
Options = boot options (defaults is a combo of options). Like, rw, ro, sync, async, etc.
Dump = old unix backup system. If dump not installed or used, it means nothing. 0 no bkup, 1 = bkup.
Pass = FSCHK, 0 no check, 1 check (high pri), 2 check (2ndary pri)
add label to device/part
File Management Commands 1 & 2
whoami - print username
pwd - print present working directory
ls - ls contents of dir
ls -F - puts a slash on directories (handy!)
tar
cpio
gunzip backup.cpio.gz
dd for disk duplication/cloning/imaging
System D
An alternative init system – (usual is upstart, sysV)
Breaks up config files.
Is a program, running all the time. SysV = script, one line/procedural at a time. hangup on one line can cause delays. SystemD, one hung up module won't block all others.
SystemD on boot looks for UNITS and TARGETS.
httpd.service NOTES:
[Unit]
Description- Desc of the file
After=network.target remote-fs.target – don't run httpd.service until AFTER these targets are started.
Wants=samedepends – means if one arg here stopped running, httpd.service will continue to run.
[Service]
Type=notify - lets us know if it worked, didn't work.
Environment=?? no idea
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREFROUND (this is how it's started, AKA systemctm start httpd.service)
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
Xorg Configuration
Find and Locate
Both used to search for files. locate uses a database of information to search and is updated via a syscron or manual updatedb. find will search the disk in specified locations for files and objects.
locate motd
find /etc -name motd