#Summary
These are notes for the Linux OS.
There’s a lot of Linux distributions. This image shows the various distributions:
Each system has a package management program. They include:
apt-get
interface tool (e.g. search, update)yum
interface toolsudo apt-get update
- run this after changing /etc/apt/sources.list
or /etc/apt/prefernces
sudo apt-get upgrade
- upgrades all installed packages
sudo apt-get dist-upgrade
- same as upgrade, except does a ‘smart upgrade’ (smart conflict resolution)
sudo apt-get check
- diagnostic tool, does an update of the package lists and checks for broken dependencies
sudo apt-get -f install
- Fixes broken packages (if you get “unmet dependencies”)
sudo apt-get autoclean
- removes .deb fiels for packages that are no longer installed on your system
sudo apt-get clean
- removes all packages from the package cache
sudo apt-get remove <package_name>
- removes an installed package, leaving configuration files
sudo apt-get purge <package_name>
- completely removes a package and any associated configuration files
sudo apt-get autoremove <package_name>
- removes packages that were installed by other packages that are no longer needed
sudo apt-cache search <search_term>
- searches package name and description contains <search_term>
##Overview
The Linux Operating System has three main levels:
##Kernel
The Kernel is the interface between hardware and the user software. In order to managem the CPU and memory, the Kernel has unrestricted access to hardware. Its main jobs are:
###Kernel’s MMU (Memory Management Unit)
The Kernel manages memory with the help of a memory management unit (MMU), that uses a memory management scheme (to enable virtual memory). Some details about memory:
So what exactly is the relationship between the Kernel and the CPU? The Kernel helps with allocating CPU time for processes with these steps:
Rinse and Repeat.
###System Calls (syscall) with fork() and exec(program)
A system call (aka syscall) is when a User Process can’t do a process (either can’t access or have ability), so the process requests from the Kernel. There are two types of syscalls:
So what does this mean? Other than init, all User Processes start as a result of fork(). Most of the time, fork() then calls exec(program) to start a new program instead of running a copy of an existing program.
Note, system alls are written in C, which is why the syntax has parenthesis.
There are two different types of processes in Linux:
Daemon processes are special types of background processes that start at system startup and keep running forever as a service.
The init process is the mother (parent) of all processes on the system. You can see this with the following (see process id of 1)
ps aux | grep init
root 1 0.0 0.1 225680 9332 ? Ss Jan24 1:50 /sbin/init splash
A new process is normally created when an existing process makes an exact copy of itself in memory. The child process will have the same environment as its parent, but only the process ID number is different. You’ll see the Process ID (PID) and the Parent Process ID (PPID).
When creating a new process, you can use fork or exec.
##Users
Linux defines a User as ‘someone that can run processes and own files’. As a User:
passwd
to change the passworduseradd <name>
to create a standard user (and you can specify their home directory, otherwise will be /home/username/
Passwords are stored in /etc/shadow
The username root is a superuser, meaning it has access to escalated permissions to allow terminating other Users’ processes. You don’t want to normally run as root since it can be easy to make mistakes. Even though root is powerful, root still runs in ‘user space’ (and not in Kernel mode)
###Groups
Linux uses groups to organize users. Groups organize collections of accounts, mainly for handling permissions nicer.
Control of each group is administered through the /etc/group
file.
Every user has a default or primary group. You can change groups with the chgrp <newgroup>
command.
##Shells
A shell is a program that runs commands. Shell Scripts are files that contain many shell commands. There are many types of shells, but they mainly originate from the bourne shell and the most common is the bourne again shell (aka bash).
You can usually find what shell is being used by going to /bin/sh
and peaking in there to see the link to the shell.
chsh to change a shell.
###Bourne Shell
The original shell is the bourne shell. To see a modern shell, see below for Bourne Again Shell (aka bash)
###bash
bash is an enhanced version of the bourne shell and is the standard shell on most Linux distributions. Most of the below notes are for bash.
Linux processes use input and output steams to read and write data. For example, an input stream might be a device and the output stream might be a file or terminal. So what do you mean?
cat
in bashThere’s three types of Standard Streams:
Basic Input.
Where output goes. By default, the Stream ID is 1.
Where errors go. By default, the Stream ID is 2.
Unix is a flavor of Linux. You can use unix in many other systems so they will be your staple commands.
cat
outputs the contents of one or more filesls
lists the contents of a directorycp
copies files, e.g. cp file1 file2
copies file1 to file2mv
moves a file (really just renaming it), e.g. mv filea fileb
renames filea to filebtouch
creates a file touch file1
rm
removes a file (or directory by specifying the correct options), e.g. rm file1
or rm -rf dir1
to remove a directoryecho
prints its arguments to standard output, a useful way to find out values, variables, or results of pattern matchinghead
gets the first few lines of a filetail
gets the last few lines of a filesort
sorts a file (default alphabetical), use -n
for numerical order, -r
for reverse orderless
to return large file contents in a paginated format (one page at a time), e.g. less largefile1
. Usually used with other commands like grep stuff /usr/share | less
diff
to return the differences between two files, often used with -u
to return in a friendlier format for tools, e.g. diff -u file1 file2
file
to get a guess on what the filetype is, e.g. file file1
xargs
build and execute command lines from standard input (i.e. construct argument lists and invoke another utility)###xargs
xargs is a good command to help chain other commands.
Say you have a set of directories that you want to copy over to another location. You can use xargs to help:
/tmp/yay/ls
dir1 dir2 dir3 dir4
/tmp/yay/ls | xargs -I % mkdir -p /tmp/another_dir/%
/tmp/another_dir/ls
dir1 dir2 dir3 dir
The -I
option lets you take a string that will be replaced with the supplied input before the command is executed.
Usually you use %
as a common choice.
Another trick is to use a shell script inside your command:
mkdir ~/backups
$ find /path -type f -name '*~' -print0 | xargs -0 bash -c 'for filename; do cp -a "$filename" ~/backups; done' bash
##Navigating Directories and Files
In order to navigate around Linux in the command line, you will need to know how directories and files are referenced and how to move across these paths.
/
is the root directory;, we never use a \
in Linux/
, e.g. /etc/blah/something.txt
/
, e.g. static/js/something.txt
..
using two dots means referencing the parent directory, e.g. if you’re in /usr/bin
, ../bin
to refer to bin
.
means the current directory; not really usedThese commands and terms are useful to navigate around paths:
cd
changes directories, e.g. cd dir1
will change to dir1
(or if None, returns to home dir)~
references the home directorymkdir
creates a directory, e.g. mkdir dir1
creates dir1
rmdir
to remove a directory, e.g. rmdir dir1
to delete the dir1
. If dir has files, use rm -rf dir1
to recursively (-r
) delete a dir -d
and all its contentspwd
to print the current working directory; this will be useful when working with symbolic links, usually pwd -P
to find the true path##Searching Directories in Shell using Globbing (Wildcards)
Globbing (aka Wildcards) is the process of matching files and directories to a pattern in the shell.
?
to match one arbitrary character, where regex uses .
So what happens after a match is made? The shell does a process called expansion, which means:
Some Notes:
*
simply prints out *
*
is used in shell globbing to match any arbitrary set of characters. Here’s sample ways of using *
:
*at
matches anything that ends with at
at*
matches anything that starts with at
*at*
matches anything with at
in the string?
is used in shell globbing to match one arbitrary character.
b?at
would match boat
and brat
, but not brrat
##Grep
Grep prints the lines that matches an expression/a pattern. Grep understands patterns called regular expressions. Example uses include:
grep root /etc/passwd
to find the text root
in the directory /etc/passwrd
grep -i stuff etc/*
to find the word stuff
(-i
for case insensitive) in the directory /etc/
grep -v stuff etc/*
to find the inverse of stuff (does not match) in /etc/
To search inside a file (not in the shell), use /
and ?
.
/
to search forward from the cursor?
to search backwards from the cursorn
to go to next match once search results are foundLet’s take a look at how our shell is configured and how to move around using shortcuts.
Shell keyboard shortcuts can be helpful:
^C
is often used to reference Ctrl + C
^P
to get previous command^B
to move back a step^F
to move forward a step^A
to move to beginning of the line^E
to move to end of the line^W
to erase preceding word^U
to erase from cursor to beginning of the line^K
to erase from cursor to end of the line^Y
to paste erased text (e.g. from ^U
)Dot files are files that start with a .
. These configuration files and directories are normally hidden by default and usually hold config settings.
ls
normally doesn’t show configuration files and directories by default (otherwise things print messy)ls -a
to see all the directory files, including the configuration files.*.
pattern to make shell globs search through configuration files, otherwise will ignore these files by default.bashrc
and .login
are common configuration files, .ssh
is a common dot directoryShell variables are temporary variables being stored by the shell. These can contain simple strings or some variables to control the way the shell behaves.
STUFF=blah
to assign the value blah
to the shell variable STUFF
echo $STUFF
to use the shell variable (referencing with $
)Environment variables are slightly different than shell variables in that they get passed to the programs that the shell runs. These are used in configuration and option settings.
STUFF=blah
to assign the value blah
to the variable STUFF
(at this point, it’s not an environment variable yet)export STUFF
to make environment variable STUFF (key is to use export
to make environment variable)The PATH variable is a special environment variable that contains the command path; its purpose is to list the directories that the shell searches to locate a command.
echo $PATH
to show PATH, with $
to reference the PATH variable:
PATH=somedir:$PATH
to add a directory to the beginning of the PATHPATH=$PATH:somedir
to add a directory to the end of the PATH/usr/local/bin:/usr/bin:/bin
man
stands for Manual Pages, which are built in docts that are split into these 8 sections:
Type in man <number> <search>
where number is one of the sections above (1-8) and a search term
man 5 passwd
to get the file description of /etc/password
(as opposed to system command)<command> --help
to get help on a specific command, e.g. ls --help
man -k <keyword>
to search a manual page by keyword, e.g. man -k keyword
To get additional information, try out info <command>
, e.g. info grep
/usr/share/doc
is where some packages dump their available documentation##Redirecting Standard Streams
You can redirect standard input, standard output, and standard error streams. Normally we have:
stdin 0
- standard input typically carries data from a user to a program (e.g. receive input from a device like
a keyboard). Normally stdin is terminated by EOF (end-of-file), meaning no more data to be read.
stdout 1
- standard output writes the data that is generated by a program; when this is not redirected, it normally
outputs text back to the terminal.
stderr 2
- standard error writes the errors generated by a program that has failed at some point in its execution.
the default destination for this stream is the terminal display
With redirection, we can:
>
to redirect Standard Output to another location by creating or replacing a file, e.g. command > file1
command >> file1
|
(aka piping) to redirect Standard Output to Standard Input of another command, e.g. command1 | command2
, head /proc/cpuinfo | tr a-z A-z
2>
to redirect Standard Error to a separate stream, e.g. command > file_a 2> file_b
>&
to redirect Standard Output and Standard Error to the same file, e.g. `command > file_a 2>&1<
is an uncommon scenario to take file and redirect to Standard Input, e.g. command < file_a
You can combine some of the stdin, stdout, stderr together with redirection:
Overwrite with:
>
standard output
<
standard input
2>
standard error
You can append with:
>>
appends standard output
<<
appends standard input
2>>
appends standard error
Use pipes |
to move output from one program to the input of another program
##List and Manipulate Processes
A process is a running program. Here’s how to read process details:
ps
to list processesS
for sleeping, R
for runningCommon process commands are:
ps x
to show the running (executing) processesps ax
to show all running processes (not just yours)ps u
to show detailed info on processesps w
to show full command nmaes (not just what fits in a line)ps aux
to show all running (executing) processes in detailkill
to signal to kill a process
kill <pid>
to kill a specific process (based on PID)kill -STOP <pid>
to STOP a specific process (based on PID)kill -CONT <pid>
to continue a process (based on PID)kill <pid1> <pid2> <pid3>
to kill multiple processes^C
to kill the running jobAs you saw in Kill, you can manage shell jobs by sending TSTP signals (similar to STOP) and CONT signals. You can also send a job to the background or foreground.
jobs
command to see what jobs are currently running^Z
to send a TSTP signal to SUSPEND a processfg
to start the process again (bring to foreground)bg
to move the process to the background&
to detach a process from the shell and put it in the background
so you have the prompt back, e.g. gunzip file.gz &
to unzip a large file in bg^L
to redraw the entire screen^R
to redraw the current line, but be careful, can put you in reverse isearch mode if in bash prompt already (Esc to quit)The details of the Linux Directory Hierarchy can be found in the Filesystem Hierarchy Standard (FHS) at http://www.pathname.com/fhs/
The overview looks like this for the main directories:
ls
and cp
etc/X11
has graphics cards configsstatic
and shared
); this only has shared libraries/var/log/
), user tracking, caches, and other files that system programs create and manageusr/bin
, usr/lib
). This is where most of the user-space programs and data sit###usr
The /usr
directory is where most of the user-space programs and data sites.
- `/usr/bin/`
- `/usr/man/` has man pages
- `/usr/info/` has info pages
- `/usr/include` holds header files used by the C compiler
- `/usr/lib/`
- `/usr/local/` where administrators can install their own software; should look like that of `/` and `/usr`
- `/usr/sbin/`
- `/usr/share/` has files that should work on other kinds of Unix machines; sometimes `/man` or `/info` are here
Every Unix file has a set of permissions that say whether you can read, write, or execute the file.
ls -l
to display the permissions-rw-r--r-- 1 juser somegroup 7041 Mar 26 19:34 endnotes.html
-
) determines the file type. -
for regular file, d
for directoryrw-
, etc)###chmod to change file permissions
Use chmod
to change file permissions. There’s also umask
to set a predefined set of permissions for any new files you create.
chmod g+r file1
to add file permissionschmod g-r file2
to remove file permissionschmod 664 file3
to change permissions with numbers (an absolute change) since it changes all permission bits at onceumask
shell command to set a predefined set of permissions to any new file you createSome of the most common modes:
644
- user: read/write; group, other: read, e.g. used for files600
- user: read/write; group; other: none755
- user: read/write/execute; group, other: read/execute. e.g. used for directories, programs700
- user: read/write/execute; group, other: none. e.g. used for directories, programs711
- user: read/write/execute; group, other: execute. e.g. used for directoriesYou can see all the group permissions under /etc/group
.
If you want to check the members of a group, you can run getent group my_group
.
To see your own groups, run groups
.
A symbolic link is a file that points to another file or directory.
lrwxrwxrwx 1 ruser users 11 Feb 27 13:52 somedir -> /home/origdir
In this example, when you try to access somedir
, you get /home/origdir
, i.e. a shortcut.
Links can get pretty confusing because:
/home/origdir
) doesn’t even need to exist-s
option when creating a symbolic link, which means you create a hard linkln -s target linkname
so that the linkname argument is the name of the symbolic link.
The target arg is the path of the file or directory that the link points to. The -s
flag specifies a symbolic link.
To update a link, you can run ln -sf /path/to/dir/that/exists/already/ /path/to/symlink/akamyshortcut/
Remember that symlink is like cp, where you have the source and the target.
To delete a symbolic link, just type in unlink
followed by the link
You can see a symlink with something like ls -alh .
lrwxrwxrwx 1 will will 42 Apr 2 2016 my_stuff -> my_folder/my_stuff/
##Archiving and Compressing Files
We want to be able to zip/unzip files (i.e. do compression / uncompress files) and to handle archives (pack / unpack multiple files and directories into one file).
###Gzip
To compress and uncompress files, we’ll use the program gzip
. Files end with .gz
gzip my_file
to compress the filegunzip my_file.gz
to uncompress the file###tar
To pack multiple files and directories into one file, we want to create an archive with tar
. We can also unarchive (take one file and put back into multiple files and directories). Files end with .tar
A lot of this depends on the modes:
c
flag activates create modex
flag means to put tar into extract/unpack modet
flag to check contents with table-of-contents
modeThen we can add additional flags:
tar cvf my_archive.tar file_1 file_2 file_3
v
flag prints details such as file size and permissionsf
flag means the file optionExample Commands:
tar xvf my_archive.tar
to unpack a .tar
file###Compressed Archives with .tar.gz
Archives are normally found compressed, so you’ll see a filename ending in .tar.gz
. Work from the right side to the left.
Uncompress, then Unarchive.
gunzip my_file.tar.gz
tar xvf my_file.tar
###zcat
A faster and more efficient way (in terms of kernel I/O time, disk space), is to use zcat
.
zcat my_file.tar.gz | tar xvf -
This zcat
command is the same as gunzip -dc
. The -d
option decompresses and the -c
option sends to standard output (in this case, to the tar
commmand)
Note: a .tgz
file is the same as a .tar.gz
file
###bzip2
A .bz2
file is the result of another compression program called bzip2
. The advantage of this format is that it comrpesses text files a little more.
bunzip2
to decompress.
zip
and unzip
are still available to help with zip archives that come from Windows systems.
You can run commands as the superuser; one method is to use su
. Issues include:
visudo
to edit /etc/sudoers
, this command checks for file syntax errors after you save the fileInstead, use sudo
, e.g. sudo vipw
Note, you can configure list of users that can run commands as supervisor under /etc/sudoers
file. You can set which Users can use sudo
##Devices
The kernel provides a way to interface with devices.
/dev
directory is the directory for devices###Using /dev to identify devices
Let’s see how device ouptut might look like:
$ls -l
#Sample output
brw-rw---- 1 root disk 8, 1 Sep 6 08:37 sda1
crw-cw-rw- 1 root disk 1, 3 Sep 6 08:37 null
Here’s how to read the output:
b
(block), c
(character), p
(pipe), s
(socket), then the file is a device/dev
dir.There are also two numbers before the date, indicating the major and minor device numbers that help the kernel identify the device.
###Using /sys to identify devices
/dev
are so user processes can use the device, while the same devices are viewable in /sys/devices
to view info and manage the device./sys/devices
./sys
directory, you can see symbolic link shortcuts (e.g. /sys/block
contains all the block devices on the systemls -l /sys/block
to see the true sysfs paths###dd program for block and character devices
dd
is useful for working with block and character devices because it allows you to read from an input file or stream and write to an output file or stream.
dd
copies data in blocks of a fixed sizedd if=/dev/zero of=new_file bs=1024 count=1
copies a single 1024 byte block from ‘/dev/zero’ (a continuous stream of zero bytes) to ‘new_file’###Finding the name of a device
You can search for the name of a device a few ways:
udevd
using udevadm
/sys
directorydmesg
command (prints the last few kernel messages) or the kernel system log filemount
command on the disk device and check outputcat /proc/devices
to see the block and character devicesfindmnt
helps you find what mounts are available###Hard Disks
Most hard disks are connected using the sd
prefix, standing for SCSI disk (Small Computer System Interface). You might see:
/dev/sda
, /dev/sdb
means entire disks/dev/sda1
, /dev/sda2
means partitions on a disklsscsi
tool is used to list the SCSI devices on your system####Terminals
Terminals are devices for moving character between a user process and an I/O device, usually for text output to a terminal screen. Pseudoterminal devices are emulated terminals that understand the I/O features of real terminals, but instead of talking to actual hardware, the kernel presents the I/O interface to software, like the shell terminal window.
/dev/tty1
is the first virtual console/dev/pts/0
is the first pseudoterminal device/dev/pts
directory is a dedicated filesystem/dev/tty
device is the controlling terminal of the current processLinux has two primary display modes: text mode and an X Window System server (graphics mode)
####Other Devices
/dev/sr
(e.g. /dev/sr0
, /dev/sr1
)/dev/hda
, /devhdb
); if you find one, its probably an unrecognized SATA drive (which can hinder performance)/dev/ttyS
(e.g. /dev/ttyS0
, `/dev/ttyACM1)), but you can’t do much with these here/dev/lp0
and /dev/lp1
), but these are old and have mostly been replaced by USB/dev/parport0
and /dev/parport1
/dev/snd/
, /dev/dsp
, and /dev/audio
. Advanced Linux Sound Architecture (ALSA) is newer interface in /dev/snd
####Creating Device Files with udevd
Use devtmpfs
and udev
to create device files
udevd
is a user-space process that gets sent notifications from the Linux Kernel when a new device is detected on the system (e.g. new usb drive)devtmpfs
filesystem was created in response to this problem of device availability during bootWhen working with disks and filesystems, you should be able to partition disks, create and maintain filesystems inside of the partitions, and work with swap space.
/dev/sda
, but these disks are often partitioned into subdivisions of the whole disk at say /dev/sda1
, /dev/sda2
There are a few kinds of partition tables.
When we break up the partitions, we see that there are primary, extended, and logical partitions:
When initially reading a table, the Linux kernel produces a debug output (can be found through dmesg
) that shows these partitions, e.g.
sda: sda1 sda2 < sda5 >
# the `sda2 < sda5 >` output means that `/dev/sda2` is an extended partition containing a logical partition (`/dev/sda5`)
* We normally ignore extended partitions because we only want the logical partitions inside
Here are some different partitioning tools:
parted
is a text-based tool (MBR and GPT supported)gparted
is a graphical version of parted
fdisk
is the traditional text-based Linux disk partitioning tool (does not support GPT)gdisk
is a version of fdisk that supports GPT, but not MBRYou can view and change partition tables with parted
and fdisk
.
We will use parted
for system partitioning, but it’s not recommended to use parted
for creating filesystems.
parted -l
to view a system’s partition tableparted
and fdisk
can also change partition tables (but fundamentally different approaches; fdisk
designs a new partition table before making changes to the disk, only doing so when you exit the program while parted
takes into effect as you issue the commands)dmesg
, e.g. creating two partitions on /dev/sdf
outputs: sdf: sdf1 sdf2
udevadm
to watch the kernel event changes, e.g. udevadm monitor --kernel
shows the old partition devices being removed and new ones being added/proc/partitions
for full partition information/sys/block/device/
for altered partition system interfaces or /dev
for altered partition devicesThere can be big performance consequences if you aren’t careful with laying out data based on the hardware.
Hard drives have a spinning platter on a spindle, with the head attached to a moving arm to read/write data on a cylinder; you can see sectors and cylinders, but the reported values are fiction so there’s not much you can do here; trust the Logical Block Addressing (LBA) to assign blocks correctly
Solid-State Disks (SSDs) don’t have moving parts, but one performance factor is partition alignment, which means read/writes are 4096 bytes at a time; this means if your partition data is outside the 4096 byte boundary, you have to do two reads instead of one
####Filesystems
The file-system are the files that you’re used to interacting with in user space. You can run ls
and cd
to these files and directories.
Remember that the filesystem is a form of database that structures these simple block devices into a hierarchy of files and subdirectories that users can understand
There are different filesystem types, some optimized for Linux, and other foreign ones like Windows FAT family. Here are some common types:
ext4
is the Fourth Extended filesystem, the second iteration of a line of filesystems native to Linux; has backward compatibility with older versions like ext3
, ext2
ext3
is the most common Linux fs type from a few years backntfs
is the a common Windows fs type for larger external hard drivesiso9660
is ISO 9660, a CD-ROM standardmsdos, vfat, umsdos
are FAT filesystems, which are specific to Microsoft systems (common in smaller external hard drives)exfat
is file system commonly found on USB flash drives and other external driveshfsplus
(HFS+) is an Apple standard used on most Mac systemsauto
a special case; it will try to guess the fs type when you use thissysfs
is a pseudo file system by the Linux kernel that exports information about various kernel subsystemsnfs
stands for Network File System (NFS), which is a way of mounting Linux discs/directories over a network
An NFS server can export one or more directories that can then be mounted on a remote Linux machineYou can also create filesystems with mkfs
, e.g. mkfs -t ext4 /dev/sdf2
to automatically determine the number of blocks in a device and set reasonable defaults
mkfs
, it prints out diagnostic output as it worksmkfs
is just a frontend for a series of filesystem creation programs (e.g. mkfs.fs
where fs
is a filesystem type like ext4
)Mounting is the process of attaching a filesystem. When the system boots, the kernel reads some configuration data, then mounts root /
based on the config data.
You need to know the following in order to mount a filesystem:
####Mount Commands
Run mount
to get the filesystem status on your system. Each line corresponds to one currently mounted filesystem and follows this format:
proc
), but are stand-ins for real device names because these filesystems do not need deviceson
/sys
)type
sysfs
, devpts
)blkid
(Block ID) which uses.To mount and umount:
mount -t type device mountpoint - e.g.
mount -t ext4 /dev/sdf2 /home/extra`umount
to unmount - e.g. umount mountpoint
Finding mounts:
$ findmnt
TARGET SOURCE FSTYPE OPTIONS
/ /dev/sda1 ext4 rw,relatime,errors=remount-ro,data=ordered
├─/sys sysfs sysfs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/kernel/security securityfs securityfs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/fs/cgroup tmpfs tmpfs ro,nosuid,nodev,noexec,mode=755
│ │ ├─/sys/fs/cgroup/systemd cgroup cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd
│ │ ├─/sys/fs/cgroup/rdma cgroup cgroup rw,nosuid,nodev,noexec,relatime,rdma
│ │ ├─/sys/fs/cgroup/cpu,cpuacct cgroup cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct
│ │ ├─/sys/fs/cgroup/memory cgroup cgroup rw,nosuid,nodev,noexec,relatime,memory
│ │ ├─/sys/fs/cgroup/net_cls,net_prio cgroup cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio
│ │ ├─/sys/fs/cgroup/cpuset cgroup cgroup rw,nosuid,nodev,noexec,relatime,cpuset
│ │ ├─/sys/fs/cgroup/pids cgroup cgroup rw,nosuid,nodev,noexec,relatime,pids
│ │ ├─/sys/fs/cgroup/blkio cgroup cgroup rw,nosuid,nodev,noexec,relatime,blkio
│ │ ├─/sys/fs/cgroup/devices cgroup cgroup rw,nosuid,nodev,noexec,relatime,devices
│ │ ├─/sys/fs/cgroup/perf_event cgroup cgroup rw,nosuid,nodev,noexec,relatime,perf_event
│ │ ├─/sys/fs/cgroup/freezer cgroup cgroup rw,nosuid,nodev,noexec,relatime,freezer
│ │ └─/sys/fs/cgroup/hugetlb cgroup cgroup rw,nosuid,nodev,noexec,relatime,hugetlb
│ ├─/sys/fs/pstore pstore pstore rw,nosuid,nodev,noexec,relatime
│ ├─/sys/kernel/debug debugfs debugfs rw,relatime
│ ├─/sys/kernel/config configfs configfs rw,relatime
│ └─/sys/fs/fuse/connections fusectl fusectl rw,relatime
├─/proc proc proc rw,nosuid,nodev,noexec,relatime
│ └─/proc/sys/fs/binfmt_misc systemd-1 autofs rw,relatime,fd=34,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=480
├─/dev udev devtmpfs rw,nosuid,relatime,size=12283432k,nr_inodes=3070858,mode=755
│ ├─/dev/pts devpts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
│ ├─/dev/shm tmpfs tmpfs rw,nosuid,nodev
│ ├─/dev/hugepages hugetlbfs hugetlbfs rw,relatime,pagesize=2M
│ └─/dev/mqueue mqueue mqueue rw,relatime
└─/run tmpfs tmpfs rw,nosuid,noexec,relatime,size=2462716k,mode=755
├─/run/lock tmpfs tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k
└─/run/user/1000 tmpfs tmpfs rw,nosuid,nodev,relatime,size=2462716k,mode=700,uid=1000,gid=1000
└─/run/user/1000/gvfs gvfsd-fuse fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000
####Mount NFS File Systems with fstab
Another way you can mount an NFS share from another machine is to add a line to /etc/fstab
file.
The line needs to start the hostname of the NFS server, the directory on the server being exported,
and the directory on the local machine where the NFS share is to be mounted.
####Mount NFS File Systems with autofs
You can mount an NFS share using the autofs
service. Autofs uses the automount daemon to manage your mount
points by only mounting them dynamically when they are accessed. Autofs uses the master map configuration file
over in /etc/auto.master
to determine which mount points are defined. It then starts an automount process
with the appropriate parameters for each mount point. Each line in the master map defines a mount point
and a separate map file that defines the file systems to be mounted under this mount point. You might see:
/etc/auto.misc
file that defines the mount points in the /misc
directory
/etc/auto.master
the master file might define the relationship for this with a line like:
/misc and /etc/auto.misc --timeout 60
#####Autofs Commands
You can start the autofs service with:
/sbin/service autofs restart
/sbin/service autofs status
/sbin/service autofs reload
- you must reload autofs when you modify /etc/auto.master
Load Averages are a good indication on how your server is being used. You can
see this when you type uptime
or from htop
. You see three numbers, like:
$ uptime
Tasks: 137, 554 thr, 1 running
Load average: 0.03 0.54 1.24
Uptime: 9 days
There are three numbers for load averages, and they represent the 1 min, 5 min, 15 min marks. With these, you can tell if the system load is idle, increasing, decreasing, or if its higher than your CPU count, you probably have a performance problem. Note that this is system load, not necessarily just CPU so it might include CPU, disk, uninterruptible locks.
You can look up bcc-tools (https://github.com/iovisor/bcc) for analysis of what the CPU is doing (e.g. create a CPU Flame Graph).
Some better metrics (especially for CPUs) might be:
$ mpstat -P ALL 1
09:45:30 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
09:45:31 PM all 1.27 0.00 0.76 0.00 0.00 0.00 0.00 0.00 0.00 97.97
09:45:31 PM 0 1.04 0.00 1.04 0.00 0.00 0.00 0.00 0.00 0.00 97.92
09:45:31 PM 1 2.02 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 97.98
09:45:31 PM 2 1.02 0.00 1.02 0.00 0.00 0.00 0.00 0.00 0.00 97.96
09:45:31 PM 3 1.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 98.00
$ top, pidstat 1
09:52:14 PM UID PID %usr %system %guest %CPU CPU Command
09:52:15 PM 123 1123 1.00 0.00 0.00 1.00 2 beam.smp
09:52:15 PM 0 1271 1.00 0.00 0.00 1.00 1 docker-containe
09:52:15 PM 0 1332 1.00 0.00 0.00 1.00 3 Xorg
09:52:15 PM 1001 2288 0.00 1.00 0.00 1.00 0 i3status
09:52:15 PM 1001 10390 1.00 0.00 0.00 1.00 2 pidstat
09:52:15 PM 1001 16999 2.00 0.00 0.00 2.00 1 chrome
E.g. in /proc/PID/schedstats, delaystats, perf sched
$ perf sched
E.g. in /proc/schedstat, perf sched
E.g. vmstat 1
and the ‘r’ column
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 48348 224424 385352 3696108 0 0 28 149 205 156 36 8 57 0 0
0 0 48348 224268 385352 3696148 0 0 0 0 275 674 1 1 98 0 0
1 0 48348 277900 385352 3642484 0 0 0 0 3792 1269 1 1 98 0 0
0 0 48348 277912 385352 3642432 0 0 0 0 5856 1545 1 1 98 0 0
0 0 48348 277820 385356 3642428 0 0 0 60 7907 1987 2 1 97 0 0
##IO Stat
You can get IO stats from iostat
.
$iostat
Linux 4.10.0-20-generic (xps) 08/08/2017 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
36.21 0.09 7.78 0.08 0.00 55.84
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
nvme0n1 1.10 5.03 254.27 4160554 210132300
dm-0 0.02 0.01 0.06 9600 52532
Run iostat -m 5
to show in megabytes per second and 5 to recalculate every 5 seconds. Another good option is -x
to show
extended statistics.
$iostat -mx 5
avg-cpu: %user %nice %system %iowait %steal %idle
16.43 13.11 3.13 0.96 0.72 65.65
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
vda 0.00 246.45 279.18 235.57 2.04 2.77 19.15 0.04 0.07 0.28 0.21 0.08 3.90
dm-0 0.00 0.00 1.49 5.55 0.01 0.04 14.11 0.02 2.73 4.83 2.17 0.43 0.31
dm-1 0.00 0.00 2.28 119.85 0.04 0.53 9.68 0.02 0.19 7.62 0.05 0.19 2.26
dm-2 0.00 0.00 48.27 33.16 0.33 0.18 12.83 0.06 0.69 1.34 2.47 0.47 3.80
dm-3 0.00 0.00 225.28 278.88 1.65 2.01 14.87 0.04 0.09 0.33 0.21 0.01 0.31
You can check out the man pages to see what each of these mean, but in summary:
%user
- the percentage of CPU utilization that occurs while executing at the user level (i.e. appplication)%nice
- the percentage of CPU utilization that occurs while executing at the user level with nice priority (-20 is highest priority, 19 is lowest priority)%system
- the percentage of CPU utilization that occurs while executing at the system level (i.e. kernel)%iowait
- the percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request%steal%
- the percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor%idle
- the percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O requestOverall IOPS are with:
iostat -d <your disk name> | grep <your disk name> | awk '{ print $2; }'
e.g. iostat -dm sda | grep sda | awk '{ print $2; }'
16.41
Below this is the Disk Utilization Report, which shows stats per physical device or partition basis.
##sysstat
sysstat
is good for checking out disk I/O. On Ubuntu, you can install with:
sudo apt-get install sysstat
sudo vim /etc/default/sysstat
ENABLED="false"
to ENABLED="true"
/etc/cron.d/sysstat
to something like:/etc/cron.d/sysstat
# The first element of the path is a directory where the debian-sa1
# script is located
PATH=/usr/lib/sysstat:/usr/sbin:/usr/sbin:/usr/bin:/sbin:/bin
# Activity reports every 10 minutes everyday
*/2 * * * * root command -v debian-sa1 > /dev/null && debian-sa1 1 1
# Additional run at 23:59 to rotate the statistics file
59 23 * * * root command -v debian-sa1 > /dev/null && debian-sa1 60 2
sudo service sysstat restart
sar -A
For additional metrics / troubleshooting tips, see more from Brendan Gregg (like his USE Method)[http://www.brendangregg.com/usemethod.html].
USE Method - For every resource, check utilization, saturation, and errors.
Terminology:
resource - all physical server functional components (CPUs, disks, buses,
etc)
utilization - average time that the resource was busy servicing work
(expressed as a percent over time, e.g. one disk is running at 90%
utilization)
saturation - degree to which the resource has extra work which it can't
service, often queued (as a queue length, e.g. the CPU has an average run
queue length of four)
errors - count of error events (e.g. this network interface has had fifty
late collisoins)
Things to look for:
##Systemd
Systemd is an init system and system manager that is managed through systemctl
(command line
utility for starting and stopping system-level services and checking their status)
We have these files living in /etc/systemd/system/
directory and in directories like
/run/systemd/system/
(for run-time unit definitions) and
/lib/systemd/system/
(where the system has a copy of unit files).
# List the services that can be started or stopped
ls /etc/systemd/system/*.service
ls /run/systemd/system/*.service
ls /lib/systemd/system/*.service
Where to store files:
/etc/systemd/system
have precedence over any of the other locations on the filesystem.
If you want to modify the way a unit functions, this is the best location to modify it./run/systemd/system
have inbetween precedence between /etc/systemd/system
and /lib/systemd/system
This is used for run-time unit definitions, basically systemd uses this area for dynamically changing the system’s
unit behavior. All changes made in this diretory will be lost when the server is booted. Don’t make changes here./lib/systemd/system
directory. When
software installs unit files on the system, this is the default location where files are placed.If you were modifying a unit in say /etc/systemd/system
, you want to do the following so you
don’t overwrite the original file:
.d
appended on the end
For example, a unit called example.service
will have a subdirectory named example.service.d
example.service.d
, create a file ending with .conf
You can run commands like:
systemctl status my_name.service
systemctl status ntpd.service
# list unit files
systemctl list-unit-files --type=service
systemctl list-units --type [unit-name]
systemctl start my_name.service # activate/start a service immediately
systemctl stop my_name.service # deactivate/stop a service immediately
systemctl restart my_name.service # restart a service (stop then start)
systemctl enable my_name.service # Enable a service to start on bootup
systemctl disable my_name.service # Disable a service to NOT start on bootup
systemctl reload my_name.service # When supported, reloads a config file without interrupting pending operations
systemctl is-enabled my_name.service; echo $? # Check if service is enabled (0 is enabled, 1 is disabled)
systemctl daemon-reload # if you've modified a .service and need to reload it
In systemd, a unit refers to any resource that the system knows how to operate on and manage. These resources are defined using configuration files called unit files. Basically, units are the objects that systemd knows how to manage. These units can be abstract services like network resources, devices, and filesystem mounts.
Systemd categories units according to the type of resource they describe. You can tell the type of a unit with its
suffix (e.g. .service
, .socket
).
.service
- a service unit descries how to manage a service or application on the server (e.g. how to start and
stop a service, the dependency and ordering information for related software).socket
- a socket.device
.mount
.automount
.swap
.target
.path
.timer
.snapshot
.slice
.scope
Logrotate is a program on Linux systems that rotates, compresses, and mails system logs. Logrotate is used with ease of administration of systems that generate a large number of log files. Each log file can be handled daily, weekly, monthly, or when it grows too large.
Normally logrotate is setup as a daily cron job and is setup through a series of configuration files.
On Ubuntu, logrotate files are usually in:
/etc/logrotate.conf
- default settings and sets up rotation for a few logs that are not owned
by an system packages. This has an include
statement to get configuration from /etc/logrotate.d
/etc/logrotate.d/
- where any packages that you install that might need help with log rotation.For example, apt
’s logrotate would be in cat /etc/logrotate.d/apt
:
/var/log/apt/term.log {
rotate 12
monthly
compress
missingok
notifempty
}
/var/log/apt/history.log {
rotate 12
monthly
compress
missingok
notifempty
}
Another logrotate configuration would be:
# sample logrotate configuration file
compress
/var/log/messages {
rotate 5
weekly
postrotate
/usr/bin/killall -HUP syslogd
endscript
}
"/var/log/httpd/access.log" /var/log/httpd/error.log {
rotate 5
mail www@my.org
size 100k
sharedscripts
postrotate
/usr/bin/killall -HUP httpd
endscript
}
/var/log/news/* {
monthly
rotate 2
olddir /var/log/news/old
missingok
postrotate
kill -HUP `cat /var/run/inn.pid`
endscript
nocompress
}
~/log/*.log {}