Jan 25 Review
/etc/passwd UID < 1000 - system users
UID >=1000 - regular users
/etc/shadow
/etc/group
Permissions
Ls -l
_ rwx rwx rwx
rwx rwx rwx
(c/l/-/d) Owner user (u) Group (g) Others (o)
File Directory
r cat ls
w vi Rm, mv, touch…
Mkdir, rmdir
x /file Cd, lss
Jan 25 Class
How to change permission
Chmod [taget permission] file/dir
● 3 different ways to do this:
○ Using the Octal Value (most ideal)
■ Rw- r- - - -x ((4+2), 4, 1 —> 641)
● Readable for user group others, writable for users only,
executable for none (octal value: 644, how to do: chmod
644 file)
○ Using the assign function by assigning directly (ideal when there are a
lot of changes)
■ chmod u=rw,g=r,o=r file
● Another example = u=rx,g=x,o=w [no spaces - execute as:
chmod u=rx,g=x,o=w file]
○ Add and remove (=/-) (ideal when the changes are minimal)
■ If we want to change from r-x -w- -wx to -w- r-x rwx
● chmod u-rx,u+w,g-w,g+rx,o+r file
Default permission: System is configured for new file/directories when created
● When you make a new file, the default permissions are: - rw- rw- r- -
● When you make a new directory, the default permissions are: d rwx rwx r-x
● Base permissions for files are rw- rw- rw- (666)
● Base permissions for directory are rwx rwx rwx (777)
umask
● The higher the number, the more secure it is
○ However, this is less convenient for the users
● The smaller the umask, the less secure
○ More convenient for the users
Sticky bit u g o
● Example: 0 002
○ Step 1: put in binary value
■ 0 0 2 = 000 000 010
○ Step 2: flip the binary value
■ 000 000 010 → 111 111 101
○ Step 3: logical AND
Flipped binary 1 1 1 1 1 1 1 0 1
value
Default file 1 1 0 1 1 0 1 1 0
value
Logical AND 1 1 0 1 1 0 1 0 0
Permission r w - r w - r w -
In class work through example 243
● Step 1
○ 2 4 3 = 010 100 011
● Step 2
○ 010 100 011 → 101 011 100
● Step 3
Flipped binary 1 0 1 0 1 1 1 0 0
value
Default file value 1 1 0 1 1 0 1 1 0
Logical AND 1 0 0 0 1 0 1 0 0
Permission r - - - w - r - -
○ Umask 243
Subtraction
● For directory
○ Example
■ 777 - 243 = 534
○ Example
■ 777 - 537 = 240
○ Example
■ 777 - 431 = 346
● For file
○ Example 666 - 243 = 423 DOES NOT WORK
■ 243 is an odd number (executable can’t work in a file) - if it is even, we
can complete it correctly
● We would need to minus each digit by one if it is an odd number
○ 666 - 242 = 424
○ Example 537
■ 537 → 426
■ 666 - 426 = 240
○ Example 431
■ 431 → 420
■ 666 - 420 = 246
Quiz 2
● Cat, more, less, head (top 10 lines), tail (bottom 10 lines)
Cat Displays the text of the file Cat [file]
line by line
more Enables user to view the More [file]
contents of a file one screen
at a time
less Enables user to view the Less [file]
contents of a file one screen
at a time
head Enables user to view n Head [file]
number of lines from the top. Head -5 [file]
Default 10
tail Enables user to view n Tail [file]
number of lines from the Tail -5 [file]
bottom. Default 10
● Filtering
○ Wc, sort (-n, -k), cut (-f, -d, -b), uniq, | (STUDY)
wc Gives the number of lines, Wc [file]
words, and characters in the output:
data L# W# C# file name
sort Sorts the content of a file. Sort [file]
Default alphabetically
Sort -n Sorts the content of a file Sort -n [file]
according to string numerical
value
cut
Cut -f
Cut -d
Cut -b
uniq Removes duplicate lines that Uniq [file]
are duplicated in succession.
If you want to sort and then
delete duplicates, you would
need to do: sort [file] | uniq
| (pipeline) Connect the output of one
command directly into the
input of another
grep Used to search particular Grep [word to search] [file]
information from a text file Output:
[word] line content
[word] line content
● Ln, ln-s (hard vs soft)
● User permissions
● Chmod (no umask)
Jan 30 - Class
Sudoers - sudo command arguments
1. Customize certain admin privilege by adding user to a group such as adm, sudo
2. Maintain all activity log for using sudo
3. No admin password needed, use user’s own password
Quiz 2
Sort -n -k2
cut -d” “ -f1,2
Cur -d” “ -f2 sort | -n uniq | wc -l
Feb 12: Lab and A2 Notes
Shell Special Character Meaning Example
Double Quotes “ ” Characters inside the double
quotes are treated as
ordinary characters; special
characters lose their
meaning. Exceptions: $, \, `
Single Quotes ‘ ‘ Suppresses ALL expansions,
no exceptions. If the single
quotes are inside double
quotes, however, they lose
their special character status
and are just regular.
Escape Character \ “Quotes” only a single Echo “The balance for
character (precede the $USER is: \$5.00”
character you want the
expansion suppressed on. The balance for user me is:
Can escape and escape $5.00
character by putting two \\. In
single quotes the \ loses
meaning and is treated as a
regular character.
The following strings are stored in a file called regexfile.txt (one string per line).
cat, cot, cut, ct, caat, caut, caout, caouaout, cAt, cAot,cabt
Write the output of the following grep commands. (If there is not a match, just
write down “no match”).
grep -E c[aou]t regexfile.txt
grep -E c[aou]?t regexfile.txt
grep -E c[aou]{2\,4}t regexfile.txt
grep -E c[^aou]t regexfile.txt
grep -E c[aou]*t regexfile.txt
Grep -e c[^aou]*t
Grep -E c.?t regexfile.txt
If, then, else
-f = file
-d = directory
-e = exist
if…Elif - ls; who; date
read -p “Please provide your input.” number (where -p indicates the user prompt)
if [ $number -gt 0 ] ; then (note the space before and after square brackets)
Echo “$number is a positive”
elif [ $number -lt 0 ]
then
Echo “$number is a negative”
elife [ $number -eq 0 ]
then
echo “$number is zero”
else
echo “not a number”
Nested If
FILE=$1 ($1 command line argument)
if [ -e “$1” ]; then
if [ -f “$FILE” ] ; then
Echo “This is a regular file”
fi
if [ -d “$FILE” ] ; then
Echo “this is a dir:
fi
if [ -r “$FILE” ] ; then (-r = readable -w = writable -x = executable)
Echo “This is readable by owner”
fi
else
echo “file does not exist”
fi
For Loop
Example 1
for index in 1 2 3 4
do
Echo $index
done
Example 2
for loop_variable in 1 2 3
do
Echo $loop_variable
done
Example 3
for i in * (*= file name expansion)
do
if [ -d “$i” ]
then
echo dir found “$i” ($ indicates variable (keeps the special character
properties within double quotation) “” = boundary that is protected)
fi
done
Example 4
for i in $(ls) (command substitution)
do
if [ -f $i ]
then
count=$((count+1)) ((())=arithmetic evaluation)
fi
done
echo total number of files are
$count (when referencing the variable, need $)
Midterm Review
[CHAPTER 1 - NOT ON MIDTERM]History of Linux/Unix
● GNU + linux kernel; GPL
● Linux distribution
○ Fedora
○ Ubuntu
○ Mint
○ Etc… (Over 800 in existence)
Linux utilities (20 COMMANDS)
○ Ls
■ -l
■ -a
■ -r
○ Mkdir
■ -p (parent directories)
○ Rm
■ -r directory name
■ -f (don’t ask, just delete)
○ Rmdir (ONLY WORKS ON EMPTY DIRECTORIES)
○ Cd
■ .
■ ..
■ / (root)
■ ~ (home)
■ - (previous working directory)
○ Cut
■ -f
■ -d
■ -b
○ Sort (like in assignment and quizzes)
■ | (pipeline)
○ Uniq
○ Wc
■ -l
■ -w
■ -c
File system (10 NEED TO KNOW)
● / (root)
● /etc
○ /etc/passwd
○ /etc/group
● /bin
● /var
○ /var/log
Absolute vs relative pathname
Permissions
What does r w x mean for file and directory
How to give permissions to file and directory with octal value (chmod)
● Chmod
○ Octal value
○ =
○ +/-
Octal value ← → rwx rwx rwx
Umask
● To change and evaluate your systems securite
● What are base permissions and default permissions
○ Base permissions for file: 666
○ Base permissions for directory: 777
○ Umask with base permissions gives the default permission
○ Example
■ Umask 022
● File 666-022=644
● Directory 777-022=755
■ Umask 035
● File 666-035 (035 → 024)
○ 666-024=642
● Directory 777-035=742
Special character
● I/O redirection (quiz 3/lab 4)
● $
● ;
● (space)
● \
● “”
● ‘’
● #
● ` (back tick)
● *
● ?
● &
Single and double quotation examples: (assignment 2)
name=mark
Echo “\$name” → $name
Echo “ ‘$name’ ” → ‘mark’
Echo ‘ “\$name” ’ → “\$name”
User management
● Adduser useradd
● Groupadd addgroup
● Usermod
○ -g (primary group)
○ -G (supplement group)
● Userdel
Regex (lab 4)
● Phone number (midterm)
○ 403/587/895
● Credit card number (midterm)
● IP address (hard… possibly for the final)
Scripting (up to lab 5)
● No conditions, loops, ifs…
ALL
● Labs
● Quizzes
● Assignments