Directories
pwd print working directory
cd change directory
mkdir make new directory (-p make all non existing directories)
. current directory
.. previous directory (parent)
~ /home/user
ls -R lists directories and those children
-tr list all reversed in time
-l more details
-i inode
Soft Link Hard Link
Is a pointer to inode (memory)
Points to the main file
Link don't affect with changing
If main file deleted link coraptes
in main file
ln -s /home/user/file /home/Desktop/newName
Same but without -s
Shortcuts
!! runs the last command if you wrote /home/user/dir/file
next command if you need same path
just press Esc then .
Esc. returns the previous argument
Alt + Backspace delete one word
Ctrl + u delete from cursor to beginning
Ctrl + k delete from cursor to end
Files
touch create new file
file returns the file type
cat display the file
wc returns the words count
head -20 n
returns the first 20 line (10 is the default)
tail -20 n
returns the first 20 line (10 is the default)
mv Desktop/file.txt . moves file.txt
from relative path Desktop
to the working directory (.)
cp -r -r to copy all children
rm remove
Set to a certain user in one session
REGEX $Variables Alias
* Matches all chars x = 10; alias myAlias = 'pwd && ls'
? Matches one char echo $x myAlias
--10 unalias x
stores inputs and outputs stores commands
echo "Hello World" prints in screen
echo "text" > file.txt redirect the output to overwrite what in file
echo "text" >> file.txt append to file but don't delete file old data
echo "Today is $(data +%A)" we use $ to print variables
ls - l | tee log.txt will display at screen & redirect
Note:
> redirects the valid output only but the error is displayed at screen
so we use 2> to redirect errors only
or we use &> to redirect both output and errors
Editing Files Using vim
We have 4 main operations while editing
1) Write 2)Select 3)Copy/Delete/Paste 4)Save
So We Have 4 Modes:
vim file.txt will create the file if it not exists
1) Insert Mode
Used to insert (i) used to insert
2) Visual Mode
Used to select text: (v) for word, (V) for line and (ctrl+v) for block
3) Normal Mode
- Used for navigation, copying (y), pasting (p) and deleting (d)
- Shortcuts without selection:
(5yy) copy 5 lines, (5dd) delete 5 lines
(u) Undo, (ctrl+r) Redo
(gg) Go to first line (G) Go to last line
4) Command Mode
Type":" to start command
:x! for quit and save without confirmation (Most Used)
:q for quit
Type "/word" to search for "word" and use \c to case insenstive
man (Manual)
man ls manual for ls (more detailed than --help)
man -K ls search in all manuals to any manual contains ls
Privileges /etc/passwd
/etc/group
Users
whoami returns used user
sudo -i login as root
useradd user creates user (options: -u id -U GroupID)
1 ~ 999 are system users
id user displays user id and groups
passwd user changes password to user
userdel user deletes user
userdel -r user deletes user and all his items
Groups
groupadd hr create group (option: -g GroupID)
groupdel hr deletes group
Users Modifications
usermod -aG hr it user appends groups to user
usermod -g group user forces primary group for a user
Password Policy
Parameters in /etc/login.defs
PASSWORD_MAX_DAYS Maximum days before password expire
PASSWORD_MIN_DAYS Minimum days before change password
PASSWORD_MIN LENGTH Minimum length of password
PASSWORD_WARN DAYS Warning days before the password get expired
ID_MIN Minimum accepted ID when creating user
1 ~ 999 are system users
ID_MAX Maximum accepted ID when creating user
chage (Change Age)
chage -I inactive days after password expires then the account
will be locked (locking can be unlocked by root)
chage -d 0 forces user change password
visudo open file to set sudo but take copy from file (more safer to open the file)
or write the permissions in file in path /etc/sudoers.d/file
it will automatically added to visudo
Permissions
Type User (u) Group (g) Other (o)
d : directory
- : file
l : link
Numeric access for directories
chmod ug + r file append read
rwx
executable for files
read/write/execute
chmod -w remove write
1 1 1
111 ==> 7
so 776 means
chmod u=x assign execute only all permissions for user & group
and read only for others
0 no perm
chmod change mode 0022 is the default U Mask 1 execute
chown change ownership (prevents groups and others 2 write
from writing) 3w+x
chgrp change group 4 read
5 r+x
chown user:group fileName 6 r+w
7 r+w+x
U Mask
By Default the permission of new directory is 777 and file is 666
So we use U mask to clear the undesired persmissions (XOR)
U Mask must change in bash rc to be permenent
Special Permissions
u+s makes modifers login as user 1
g+s makes every file create in directory takes the parent group
not the group of new owner 2
o+t
no user can edit other user 4
(in shared directories)
Processes
jobs list current shell session
ps list all processes over the system
bg moves jobs to background
& starts process in background
fg %pid moves job to foreground
Ctrl+z suspends process
System Services
systemctl
start actives one or more service (Now & Temporary)
end disactives one or more service
is-active return if active or no
enable enable one or more service (On reboot & Persistent)
disable disable one or more service
is-enabled return if enabled or not
reload reload config (without stopping service)
restart stops & starts service forcibly
status detailed status
sosreport collect all logs in system
(used when openning ticket to redhat support)
lsblk list all blocks (display mounted and unmounted)
df display file system (mounted only)
du -h disk usage human readable
find search in all directories
locate search in its database so use updatedb
grep search in certain directory
grep word display any thing not include "word"
grep -v word display any thing not include "word"
grep -i word ignore case sensitive
grep -c word word count
Date and Time
timedatectl display date and time
timedatectl list-timezones list available timezones
chrony sync date and time
chronyc display sync servers
to add server chane in /etc/chrony.conf
Scripts
#!/bin/bash assign the shell # comment
To run Script just write its name with its path
Script Example: Script Example:
#!/bin/bash
#!/bin/bash
echo"the machine name is"
echo "What is your first name?"
hostname
read fn
echo"your username is"
echo "What is your first name?"
whoami
read sn
echo"your current directory is"
echo "Mrs $fn $sn"
pwd
For While
txt1 txt2 txt3
{1..5} c=1
for i in 1 2 3 4 5 while [ $c -le 5 ]
do
do
echo "Welcome $c "
echo "Welcome $i"
(( c++ ))
done done
If Condition
if [ $num -eq 100 ] && [ 1 ]
then
echo "Num is 100"
else
echo "Num is not 100"
fi
File Operation
Comparisions
-e check if file exists
-eq, -ne equal, not equal (nums) -s exists and not empty
==, !== equal, not equal(letters)
-f exists and not directory
-lt less than
-d directory exists
-le less than or equal
-x file is executable
-gt greater than
-w file is writable
-ge greater than or equal
-r file is readable
Scheduling
at
at 16:52 (write task then Ctrl + D ) scheduling
atq list jobs
atrm r remove
crontab (high control of time but if system is down script won't run)
crontab -e open script
crontab -l display crontabs
crontab -r removes all crontabs
min hr day month numericDay Sunday ==> 0
min * * * [1-5] skips sunday and saturday
*/5 * * * * runs every 5mins
cat /var/log/vron view details
anacron (low control of time but if system is down script will run when it back)
/etc/cron.weekly put the script direct
anacron at reboot
queue the scripts if server was down
Logs
create :
/etc/rsyslog.d/debug.conf
/etc/systemd/journald.conf
write:
*.debug /var/log/messages-debug /var/log/journal persistance
and systemctl restart rsyslog
/run/log/journal volatile
journalctl colored logs
more than 10% of hard
journalctl _PID=1 get deleted
logs of user id 1
journal -p err priority error
journal -u sshd.service unit name
journal -f last 10 lines
journalctl --since today today logs
Tuned
change performance
NI (Nice Value) -20 ~ 19 to set the PR priority
profiles change dirty ratio and dirty backgorund ratio
Compress
tar -cvf create tar
bzip2 test-arc.tar to compress
bzip2 -d test-arc.tar to extract
tar -xvf extract from tar
tar -czvf new.tar fileUsed change to tar & compress
tar -czvf new.tar list tar files
tar -xzvf extract & change from tar (in working directory)
File Transfer
ssh username@ip just connect to a server
sftp user@ip secure file transfer protocol
put file.txt (connect and send files)
get file.txt we get or put files from logged in server
ls his server ,, lls my server (local ls)
rsync -av from to sync with other file (change only changed files)
rsync -av /var/log student@serverb/etc