Linux Basics
Wisoot Amatariyakul
Roadmap
What is Unix?
What is Linux?
Which Linux Distribution is better?
UNIX structures
Directories, Files, Inodes
File type
Access permissions
Special characters
Control characters
Roadmap
Pipes
Shell Variable
Process
Manual
Login & Log out
Basic Commands
vi Editor
Q&A
Quiz
What is Unix?
A multi-task and multi-user Operating System
Developed in 1969 at AT&T’s Bell Labs by
Ken Thompson (Unix)
Dennis Ritchie (C)
Douglas Mcllroy (Pipes - Do one thing, do it well)
Written in C Programming language
Some other variants: System V, Solaris, SCO
Unix, SunOS, 4.4BSD, FreeBSD, NetBSD,
OpenBSD, BSDI
What is Linux?
A clone of Unix
Developed in 1991 by Linus Torvalds, a Finnish
graduate student
Inspired by and replacement of Minix
Linus' Minix became Linux
Consist of
Linux Kernel
GNU (GNU is Not Unix) Software
Software Package management
Others
What is Linux?
Originally developed for
32-bit x86-based PC
Ported to other
architectures, eg.
Alpha, VAX, PowerPC,
IBM S/390, MIPS, IA-64
PS2, TiVo, cellphones,
watches, Nokia N810,
NDS, routers, NAS, GPS,
…
Which Linux Distribution is better?
> 300 Linux Distributions
Slackware (one of the oldest, simple and stable distro.)
Redhat
RHEL (commercially support)
Fedora (free)
CentOS (free RHEL, based in England)
SuSe ( based in German)
Debian (one of the few called GNU/Linux)
Ubuntu (based in South Africa)
…
More info. https://lwn.net/Distributions/
Which Linux Distribution is better?
Which Linux Distribution is better?
Ask yourself these questions (from Linux
Admin Handbook(LAH))
Is it going to be around in 5 yrs?
Is it going to stay on top of the latest security
patches?
Is it going to release updated software promptly?
If I have problems, will the vendor talk to me?
UNIX/Linux structure
UNIX/Linux structure
Kernel
is the heart and brain of the operating system.
is a layer of software that sits between the user of
a computer and its hardware, and is responsible
for efficiently managing the system's resources.
Interfacing with hardware devices(network adapters,
hard disk and so on)
Allocating memory to individual programs
Allocating CPU time to individual programs
In UNIX, core kernel and service layers(like
MemoryMgmt/ProcessMgmt/IPC/..) are statically
compiled and linked to create a kernel executable.
A single static binary file.
UNIX/Linux structure
Kernel
In GNU Hurd, core kernel only is built into a static
executable and services are built as dynamic
modules. At boot up time core kernel gets loaded
and stay resident in memory. Core kernel will
dynamically load a service layer when it is
required. Services will be unloaded when they are
not required. Kernel with loadable modules.
Linux kernel integration(build) policy called
“Modular kernel”, which is a mixture of above two
policies.
UNIX/Linux structure
Kernel Adv/Disadv:
A single static binary file has less response time.
Kernel with loadable modules has more response
time
A single static binary file takes more memory
since the foot print carries complete static image
with all services
Kernel with loadable modules take less memory
UNIX/Linux structure
Shell
shell is not part of the kernel, but it does communicate
directly with the kernel. It is the "shell around the kernel.“
is a command line interpreter that executes the commands
you type in. It translates your commands from a human-
readable format into a format that can be understood by the
computer.
Shell program
Bourne Shell (sh)
C Shell (csh)
Bourne Again Shell (bash)
Korn Shell (ksh)
LINUX structure
LINUX file system
(kernel file)
LINUX file system
(kernel file)
Linux file system
The Linux file system looks like an inverted tree
structure.
You start with the root directory (/) at the top and
work down through sub-directories.
Each node is either a file or directory of files,
where the latter can contain other files and
directories.
You specify a file or directory by its path name,
either the full (or absolute) path name or the one
relative to a location
Pathname
Absolute Pathname
Specify the location of file/directory from the root
directory(/)
To write an absolute pathname:
Start at root directory(/) and work down
Write a slash(/) after every directory name
Example:
$ cat /var/log/messages
Pathname (Cont.)
Relative Pathname
Specify the location of file/directory from your
current directory(pwd) and never starts with /
Use . and .. in relative pathname
Example:
$ pwd
/home/guest1
$ cd ../guest2
$ ls dir2/*
Directories, Files, Inodes
Everything is Linux called file.
Every directory and file is listed in its parent
directory
A directory is a file that contains a table listing the
files contained within it, giving file names to the
inode numbers in the list.
Directories, Files, Inodes (Cont.)
The information about all the files and directories is
maintained in INODE TABLE
An inode (Index Nodes) is an entry in the table
containing information about a file (metadata)
including file permissions, UID, GID, size, time
stamp, pointer to files data blocks on the disk etc.
You can use “ls –li” to show detail of file structure.
1 file 1 inode ?
1 inode 1 file ?
Directories, Files, Inodes (Cont.)
File type
Ordinary file (regular file, text file) (-)
-rw-r--r– 1 root root 2306 Apr 22 13:20 /etc/passwd
Directory file (d)
dr-xr-xr-x 17 root root 284 2017-02-09 12:01 /
Link file (l) – refer to file without data in file
lrwxrwxrwx 1 root root 7 Jun 13 2018 /bin -> usr/bin
File type
Character device special file (c) – mouse, keyboard
Direct access to device
crw--w---- 1 root tty 4,1 2017-02-09 12:01 /dev/tty1
Block device special file (b) – disk, cdrom, memory
brw-rw---- 1 root disk 8,0 2017-02-09 13:00 /dev/sda
Note: Check Major No. for device from /proc/devices
File type
Block device special file – access through buffer cache
File type
Name pipes (FIFO - IPC) (p) – exchange data
between processes ex. spooler file for Printer
# find / -type p
File type
Socket (s) – control some jobs or data ex: network
connection
# find / -type s
Command to determine file type – file
Users, groups and access permissions
In UNIX/LINUX, there is a concept of user and an
associated group
Each user has unique user-id number (uid)
The system determines whether or not a user or group
can access a file or program based on permission assign
to them
Apart from all the users, there is a special user call
Super User or the root which has permission to access
any file or directory
Access Permissions
There are three permissions for any file,
directory or application program
The following lists the symbols used to denote
each, along with a brief description on file or
application:
r – Indicates that a given category of user can
read a file
w – Indicates that a given category of user can
write to a file
x – Indicates that a given category of user can
execute a file
Access Permissions
The following lists the symbols used to denote
each, along with a brief description on directory:
r – Indicates that a given category of user can
look at the file inside the directory
w – Indicates that a given category of user can
add or delete files from the directory
x – Indicates that a given category of user can
execute a file within the directory
Access Permissions
Each of the three permissions are assigned to
three defined categories of users
The categories are:
owner – The owner of the file or application
group – The group that owns the file or
application
other – All users with access to the system
Access Permissions
One can easily view the permissions for a file by
invoking a long format listing using the
command “ls -l”
For instance, if the user jame creates an
executable file name “test”, the output of the
command “ls -l test” would look like this:
-rwxrwxr-x 1 jame student 0 Jan 26 12:25 test
Access Permissions
The permissions for this file are listed are listed
at the start of the line, starting with rwx
This first set of symbols define owner access
The next set of rwx symbols define group access
The last set of symbols defining access
permitted for all other users
Access Permissions
This listing indicates that the file is readable,
writable and executable by the user who owns
the file (user jame) as well as the group owning
the file (which is the group named student)
The file is also world-readable and world-
executable, but not world-writable
Special Characters
. .. ~
. current working directory
.. parent directory
~ home directory
$ cd ~guest2
$ cd ~/dir2
$ cd; cd ../guest3
$ cd ..
$ cd ~/scripts
$ sh ./test1.sh
Question ?
$ pwd
/home/guest1/dir2
$ cd ../..
What’s the result of “cd ../..” ?
Answer:
Special Characters
< > >>
Standard input (stdin), default input receive from keyboard
and file descriptor is 0, redirect stdin used <
Standard output(stdout), default output send to screen and
file descriptor is 1, redirect stdout used >
Standard error(stderr), default error send to screen and file
descriptor is 2, redirect stderr used 2>
>> redirect output to a file appending the redirected output
at the end
> redirect output to a file, overwriting the file
Special Characters
Redirection example:
$ cal > foo
Clear space in file to 0 byte
$ cat /dev/null > foo
$ cat /dev/null > /var/log/messages
$ cat /etc/motd >> foo
$ cat < /etc/passwd ; wc < log.sh
$ ~/prog1.sh > prog1.log 2>&1
Send both std. out & std. error to file
Special Characters
&
Start process in background process by add
ampersand (&) at the end of the command
$ ~/bin/prog1.sh &
Special characters (Cont.)
* ? [ ]
* String wildcard
? Single-character wildcard
[ Start character-set wildcard
] End character-set wildcard
Special characters (Cont.)
` ( ) $ # \
` command substition
( Start subshell
) End subshell
$ Variable expression
# Comment
\ Quote next character
Special characters (Cont.)
backtick
$ echo “The date is `date`”
$ echo `seq 1 10`
Special characters (Cont.)
; ’ ” / !
; Shell command separator
‘ Strong quote
“ Weak quote
/ Pathname directory separator
! Pipeline logical NOT
Control characters
Ctrl-c break. Terminate a foreground job
Ctrl-d
Log out from shell(similar to “exit”)
EOF (end-of-file) terminate input from stdin –Input Completed
Ctrl-\ force terminate a foreground job
Ctrl-s Suspend (XOFF)
Ctrl-q Resume (XON)
Ctrl-z Pause a foreground job
*** After using Ctrl-z, you can run the process in
background process by typing “bg” or running in
foreground bb typing “fg” ***
Control characters (Cont.)
Ctrl-z example:
Control characters (Cont.)
Ctrl-d example:
Control characters (Cont.)
List of control character setting
$ stty -a
Pipes
Provide one-way communications between
related processes, pass output of one tool to the
input of another
Implemented based on the kernel I/O buffers
Pipes (Cont.)
Pipes are accessed in a similar way as ordinary
files:
They have assigned inodes
Reading and writing are through file descriptors
Pipes (Cont.)
Differences between pipes and files:
The pipe inode resides in the kernel, not in the
physical file system
A pipe can exist only during the existence of its creator
process
A pipe has limited size – check “/proc/sys/fs/pipe-
max-size”
Pipes (Cont.)
Example: ls | wc -w
Same result ls > tmpfile;wc -w tmpfile;rm tmpfile
Pipes (Cont.)
Pipe example:
$ who | cut -d’ ‘ -f1 | sort | uniq | wc -l
Pipes (Cont.)
Pipe example:
$ grep "/home" /etc/passwd | cut -f1 -d':’ | wc -l
Shell Variable
command
$ set display a list of environment variables
$ env display a list of shell variables
Shell Variable
command
$ echo $VAR display value of variable “VAR”
$ VAR=value Set value of variable
$ export VAR export the variable to global
Shell Variable
Shell variable
PATH tell shell where to look for commands /
executable files
PS1 Shell command prompt
$ normal user
# Superuser
PS2 prompt used for continuation interpretation
>
TERM Terminal type
HOME Home directory
PWD Current working directory
$ man bash
Process
Parent and Child process
Parent process (ppid) means process that create the child
process
Child process (pid) means running process id
You can see PID and PPID with command “ps -ef”
Process
Zombie process
Normally, when child process is killed, the parent process
will updated vid SIGCHLD signal
Sometime the parent process is killed before its child is
killed. The init process become the new ppid.
This is zombie or defunct process
Daemon process
Daemons are system-related background processes that
often run with permission of root and services requests
from other processes
Manual (help)
Manpage 4 Device drivers and
$ man ls
network protocols
/dev/tty
$ man 2 mkdir
5 Standard file formats
$ man man
/etc/hosts
$ man -k mkdir
6 Games and demos
Manpage sections (LAH /usr/games/fortune
Table 1.2 @ page 12)
7 Misc. files and docs
1 User-level cmds and apps
man 7 locale
/bin/mkdir
8 System admin. Cmds
2 System calls
/sbin/reboot
int mkdir(const char *,
…);
3 Library calls Help
int printf(const char *, command --help
…); $ ls --help | more
Login & Log out
Login Log out
(via console or network $ Ctrl-d
access) $ exit
Run login scripts/profile
ksh or sh
/etc/profile
$HOME/.profile
bash
/etc/profile
$HOME/.bash_profile
$ or #
Command
Command Syntax
To execute a command, type its name and/or
arguments at the command line (case
sensitive)
$ Command [Options] [Arguments]
$ ls -l /etc
Basic Commands
ls – list directory contents
$ ls -l
$ ls -a
$ ls -la
Basic Commands
ls – list directory contents
$ ls -l --sort=time
Basic Commands
ls – list directory contents
$ ls -l --sort=size -r
Basic Commands
ls – list directory contents
$ ls -li
$ ls -lia
Basic Commands
ls – list directory contents
List subdirectories recursively (-R)
$ ls –lR
$ ls –lR dir[12]
Basic Commands (Cont.)
cd
$ cd /usr/bin
pwd
$ pwd
~
$ cd ~
$ ls -ld ~
~user
$ cd ~jame
What will “cd ~/jame” do?
Ans: change directory to jame subdirectory
under user’s home directory
Basic Commands (Cont.)
which – show full path of commands
$ which ls
whereis – locate the binary, source, manual page files for a command
$ whereis ls
Basic Commands (Cont.)
find – search for files in a directory hierarchy
$ find / | grep stdio.h
$ find /usr/include | grep stdio.h
Basic Commands (Cont.)
find – search for files in a directory hierarchy
$ find / -name stdio.h
Basic Commands (Cont.)
find – search for files in a directory hierarchy
$ find / -name “*stdio.h”
Basic Commands (Cont.)
su – switch user
Switch user to root and execute “root” profile
$ su -
Switch user to root and still use “user” profile
$ su
Switch user to any user
$ su - guest1
sudo – run command with root privilege
$ sudo <command>
$ sudo whoami
Need to add user to “wheel” group to have
“sudo” privilege (Ubuntu use “sudo” group)
Basic Commands (Cont.)
rpm
$ rpm -q bash
$ rpm -qa
$ rpm -qa | sort | more
Basic Commands (Cont.)
yum
$ yum list updates
Basic Commands (Cont.)
yum
$ yum list installed
$ yum list installed httpd
Basic Commands (Cont.)
yum
$ sudo yum install <package_name>
Basic Commands (Cont.)
yum
$ sudo yum update <package_name>
$ sudo yum update
New Version
Basic Commands (Cont.)
yum
$ sudo yum remove <package_name>
Basic Commands (Cont.)
apt (Debian/Ubuntu)
$ sudo apt list –installed
$ sudo apt install <package_name>
$ sudo apt install <package_name> --no-upgrade
$ sudo apt install <package_name> --only-upgrade
$ sudo apt remove <package_name>
$ sudo apt purge <package_name>
Basic Commands (Cont.)
apt (Debian/Ubuntu)
Update package database
$ sudo apt update
List upgradable package versions
$ sudo apt list --upgradable
Upgrade installed package
$ sudo apt upgrade
$ sudo apt upgrade -y
Basic Commands (Cont.)
echo – display a line of text
$ echo “Hello World”
$ echo -n “Hello World”
cat
1. Create simple file
$ cat > $HOME/prog1.sh
2. Display content of file
$ cat /etc/motd
$ cat /proc/cpuinfo
3. Merge/Concatenate files and print on std. output
$ cat /etc/motd /etc/TIMEZONE
$ cat /etc/hosts /etc/TIMEZONE > testing.log
Basic Commands (Cont.)
cp – copy files or directories
(create new file from existing file with difference
i-node number)
Copy file
$ cp fox bar
$ cp -p fox bat
Copy directory
$ cp -R dir1 dir2
$ cp -Rp dir1 dir3
Basic Commands (Cont.)
mv – move/rename files
(change the name of file only, no change
other information)
$ mv foo box
$ mv -i gt4 gt5 (ask to confirm if target file exists)
Basic Commands (Cont.)
mkdir – create directory
$ mkdir foo
$ mkdir -m 444 far (create with permission)
$ mkdir -p dir1/dir2/dir3
Basic Commands (Cont.)
rm – remove files/directories
$ rm food (remove file)
$ rm -rf dir1 (remove directory)
$ rm -i food
$ rm -- -foo
** Check the corrected directory with “pwd”
before remove directory with “rm -rf” command **
Basic Commands (Cont.)
chgrp – change group ownership of file
$ chgrp training /home/guest2/gt3
chsh – change login shell
$ chsh foo
Basic Commands (Cont.)
chown – change file owner/group
$ chown -R guest1:training /home/guest2/dir2
Basic Commands (Cont.)
file – determine file type
$ file /home/foo
id – print user/group IDs
$ id
newgrp – change default gid to new group
$ newgrp training
Basic Commands (Cont.)
grep – print line matching pattern
$ grep ^pattern file
$ grep pattern file
Basic Commands (Cont.)
grep – print line matching pattern
$ grep -v pattern file
-v - print all lines except the “pattern”
Basic Commands (Cont.)
grep – print line matching pattern
$ grep -r pattern directory
-r - read all files under each directory, recursively
Basic Commands (Cont.)
chmod – change file mode bits
Basic Commands (Cont.)
chmod – change file mode bits
$ chmod 755 foo
$ chmod ug+x foo
Basic Commands (Cont.)
chmod – change file mode bits
$ chmod -R 775 dir1;chmod u+x,go=rx foo1
Basic Commands (Cont.)
umask – get or set the file mode creation mask
Permission = Default_permission - umask_value
$ umask
$ umask 027
Question ?
What’s meaning of command?
$ cd
$ cd ~
$ cd $HOME
Answer:
How do you do if you have the shell script that
using the same name as UNIX command?
Answer:
Question ?
What’s meaning of “-rw-r--r-- … file1”?
Answer:
How can you change permission to “-rwxr-x---”?
Answer:
What’s meaning of command “chmod 756 file1?
Answer:
Basic Commands (Cont.)
Hard, soft (symbolic) link
Hard link – create new file from existing file
with same inode
$ ln vmlinuz-2.6.24.4 vmlinuz
$ ln test1 test3
Symbolic link – shortcut to file location
Most useful when need to point crossing filesystem
$ ln -s test1 test4
Basic Commands (Cont.)
last – show listing of last logged in users
$ last -20
$ last | more
Last searches back through the file /var/log/wtmp
Basic Commands (Cont.)
who – show who is logged on
$ whoami
$ who
$ who -a
$w
get information from /var/run/utmp, /var/log/wtmp
Basic Commands (Cont.)
tty – show logged on terminal of user
$ tty
Clear the terminal screen
$ clear
Question ?
How to redirect output of following command to
save in “testing.out” without overwritten?
$ who -a
$ id
Basic Commands (Cont.)
touch – change file timestamps
$ touch log.sh
more – filter for viewing
$ more foo
tail – output the last part of files
$ tail -30 /var/log/messages
$ tail -f /var/log/messages
Basic Commands (Cont.)
ps – report a snapshot of the current processes
$ ps -ef
Basic Commands (Cont.)
ps – report a snapshot of the current processes
$ ps aux (refer to CPU utilization(%CPU))
Basic Commands (Cont.)
ps – report a snapshot of the current processes
$ ps avx (refer to Memory utilization(%MEM))
Basic Commands (Cont.)
top – display Linux processes
$ top
Basic Commands (Cont.)
top – display Linux processes
$ top
Press “h” for more commands in ‘top’
Basic Commands (Cont.)
nohup – no hangup processes after user log out
$ nohup prog1.sh &
jobs – display status of jobs in the current
session (background processes)
$ jobs -l
kill – terminate process
$ kill -9 1234
$ kill 1234
See signal(7) for more information ($ man 7 signal) ($ kill -l)
Basic Commands (Cont.)
tar – archiving utility
$ tar cvfp lab1.tar lab1
$ tar cvzfp lab1.tgz lab1 (archive with compress)
gzip – compress or expand files
$ gzip -9 lab1.tar
untar & ungzip
$ tar xvfz lab1.tar.gz
Display contents of gzip compression tar archive
$ tar tvzf lab1.tar.tgz
Basic Commands (Cont.)
Disk usage (filesystem usage, available)
$ df -h /
$ df -h
$ df -i
Basic Commands (Cont.)
File space usage
$ du -sh ~/
$ du -sh ~/*
$ du -h Pictures/
$ du -sh Pictures/
Basic Commands (Cont.)
uname - print system information
$ uname -a
(Kernel name)
(Node name)
(Kernel release)
(Kernel version)
(machine)
(processor)
(Hardware platform)
(Operating system)
hostname - show the system's host name
$ hostname
Basic Commands (Cont.)
Showing disk partition table (root privilege)
# fdisk -l
Basic Commands (Cont.)
Showing disk partition table
$ cat /proc/partitions
sda = the first scsi disk, s – scsi, a – 1st disk
(b=2nd,c=3rd,…)
Basic Commands (Cont.)
Showing CPU information
$ cat /proc/cpuinfo
Basic Commands (Cont.)
Showing CPU information
$ lscpu
$ nproc
Basic Commands (Cont.)
Showing memory information
$ cat /proc/meminfo
$ free
Showing swap information
$ cat /proc/swaps
$ swapon -s
Basic Commands (Cont.)
Showing network interface information
$ ifconfig -a
Basic Commands (Cont.)
Showing network interface information
$ ifconfig ens192
Basic Commands (Cont.)
Showing network interface information
$ ip addr
Basic Commands (Cont.)
Showing network routing information
$ netstat -rn
Flag “G” for gateway
$ ip route
vi Editor
$ vi filename
2 modes
Input mode Command mode
ESC to back to
command mode
vi Editor
Input text mode
Switch to input mode
a (append)
i (insert)
o (insert line after)
O (insert line before)
A (append to end of current line)
I (insert at beginning of current line)
vi Editor
Command mode
Cursor movement
h (left), j (down), k (up), l (right)
^f (page down)
^b (page up)
^ (first char.)
0 (first char.)
$ (last char.)
G (bottom page)
:1 or 1G (go to first line)
w (forward one word)
b (backward one word)
vi Editor (Cont.)
Change
cc (change the entire current line) (*until <Esc> hit)
10cc or c10c (change 10 lines) (*until <Esc> hit)
cw (change the current word with new text) (*until
<Esc> hit)
r (replace single character under cursor (no <Esc>
need)
vi Editor (Cont.)
Delete Paste
dd (delete current line) p (paste after)
d10d or 10dd (delete 10 P (paste before)
lines)
Undo
d$ (delete till end of line)
u
dG (delete till end of file)
Search
x (delete current char.)
/string
Copy to buffer
N (next search in
yy (copy a line)
backward)
n (next search in
forward)
vi Editor (Cont.)
Line number
:set nu Copy content in line
:set nonu :n1,n2tn3
Replace word :3,6t15
:n1,n2s/word1/word2/g :10t20
:n1,n2s/word1/word2 Read from file
:%s/word1/word2/g :r filename
:1,$s/word1/word2/g (read file “filename”
and insert after
Note: n1/n2/n3 = 1,2,3 or 4, … current line)
vi Editor (Cont.)
Save/Quit
:w (write)
:w newfile (write to new file named
“newfile”)
:10,30w smallfile (write content of lines
10 through 30 to file “smallfile”)
:q (quit)
:wq (write and quit)
:x (write and quit)
:wq! Or :x! (force write and quit)
:q! (give up changes)
Q&A
References
Unix history
http://en.wikipedia.org/wiki/Unix
http://upload.wikimedia.org/wikipedia/commons/7/77/Unix_
history-simple.svg
Linus Torvalds
http://en.wikipedia.org/wiki/Linus_Torvalds
Linux Kernel
http://www.kernel.org/
References
GNU (Gnu’s Not Unix)
http://www.gnu.org/
Linux Distribution
http://en.wikipedia.org/wiki/Linux_distribution
http://en.wikipedia.org/wiki/List_of_Linux_distributions
PS2: Computational Cluster
http://arrakis.ncsa.uiuc.edu/ps2/cluster.php
Linux Gadgets
http://linuxdevices.com/articles/AT4936596231.html
TiVo
http://dynamic.tivo.com/linux/linux.asp
References
Nintendo DS Lite
http://www.nintendo.com/ds/
Nokia N810
http://www.nokia.com/
http://www.engadget.com/2007/10/17/nokia-n810-gets-official/
Linux Distribution
http://en.wikipedia.org/wiki/Linux_distribution
GNU/Linux Distro Timeline: http://futurist.se/gldt/
http://www.distrowatch.com/
http://www.linux.org/dist/
Google Advance Search
http://www.google.com/intl/en/help/refinesearch.html
Quiz
Scan QR-Code
Or
https://join.quizizz.com
Login with gmail or ignore
Click “Enter Code”
Enter game code : 777199
Enter game code : 174016
Enter Name: Name & Employee-ID