Sharing some of my Linux notes made during learning time.
Linux Core Concepts
Kernel versions
uname
Linux
uname -r
4.15.0-72-generic
4-kernal version
15-major version
0-minor version
72-patch release
Generic -Distro specific info
Memory
Kernal Space - Kernal code,Kernal Extentions, Device Drivers
User Space - Application/Programs
System Calls
working with Hardware
usb
Device Driver (Kernel space)
uevent -->
udev (user space)
/dev/sdb1
dmesg
dmesg | grep -i usb
udevadm
udevadm info --query=path --name=/dev/sda5
udevadm monitor
lspci
ethernet card, raid controller, wireless,
lsblk
block devices
sda
sda1
lscpu
cpu archetecture
core, threads, model
lsmem
lsmem --summary
online mem
offline mem
free -m
total vs used memory
lshw
entire hardware configuration
sudo
(root user previledge)
Linux Boot Process
Bios post
Boot Loader
Kernel Initialization
INIT Process
ls -l /sbin/init
-> /lib/systemd/systemd
systemd Targets
runlevel
N 3
N 5
RunLevel
5 - Boots in a graphical Interface (display manager service enabled)
3 - Boots into a command line interface
systemctl get-default
graphical.target
ls -ltr /etc/systemd/system/default.target
systemctl set-default multi-user.target
File Types in Linux
Regular - Images, scripts, config/data files
directory - /hom/bob, /root, /home/bob/code-directory
special files
character files - mouse keyboard
Block files - Block devices
Links - Hard links, Soft Links
Sockets files
Named Pipes
display the file type
file /home/michael/
:directory
file bash-script.sh
:Bourne-Again Shell script, UTF-8
file insync1000.sock
: socket
file /home/michael/bash-script
:symbolic link to /home/sara/bash-script.sh
ls -ld /home/micheal/
drwxr-xr-x
Filesystem Hierarchy
/opt - to install web application third party
/mnt - temporary mount location
/tmp - store temporary data
/media - all external media
/bin - mkdir
/etc - store config files
/lib - shared libery
/usr - user based application, thunderbird
/var - logs stored, cache
df -hp
(list of mounted device)
Linux Package Management
DPKG/APT
Ubuntu / Debian
RPM
RedHat, CentOS
.DEB - Ubuntu, Debian, Linux Mint
.RPM - RHEL, CentOS, Fedora
Types of Package Managers
DPKG
APT
APT-GET
RPM - Redhat Linux, CentOS, Fedora
YUM
DNF
RPM
====
Installing
rpm -ivh telnet.rpm
Uninstalling
rpm -e telnet.rpm
Upgrade
rpm -Uvh telnet.rpm
Query
rpm -q telnet.rpm
Verifying
rpm -Vf <path to file>
YUM
===
yum install httpd
yum repolist
yum provides scp
yum remove httpd
yum update telnet
yum update
DPKG and APT
================
DPKG Utility
Debien package manager
Installing
dpkg -i telnet.deb
Uninstalling
dpkg -r telnet.deb
List
dpkg -l telnet
Status
dpkg -s telnet
Verifying
dpkg -p <path to file>
APT /APT-GET
============
Higher level debien package manager
apt install gimp
apt-get install gimp
APT
====
apt update
apt upgrade
apt edit-sources
apt install telnet
apt remove telnet
apt search telnet
apt list | grep telnet
File Compression and Archival
Viewing file sizes
du -sk test.img
in kb
du -sh test.img
98m
ls -lh test.img
-rw-rw-r-- 1 99M Mar 13 15:48 test.img
Archiving files
tar -cf test.tar file1 file2 file3 (c-archive, f-mention filename)
ls -ltr test.tar
tar -tf test.tar (use to see the contents)
./file1
./file2
./file3
tar -xf test.tar
(used to extracts contents for tar)
tar -zcf test.tar file1 file2 file3
(to compress)
compressing
============
bzip2
gzip
xz
Uncompressing
===============
bunzip2
gunzip
unxz
zcat/bzcat/xzcat
can read the file without uncompressing
Searching for files and directories
locate city.txt
updatedb
find /home/michael -name city.txt
GREP
====
to search a word in a file
case sensitive
grep second sample.txt
insensitive
grep -i capital sample.txt
grep -r "third Line" /home/michael
grep -v "printed" sample.txt
prints do not match a particular string
grep -w exam examples.txt
to search whole word
grep -vw exam examples.txt
to search words not a whle word
grep -A1 Arsenal premier-league-table.txt
serach and print the matching word and one line below it also
grep -B1 4 premier-league-table.txt
to print the search word and one line above it
grep -A1 -B1 Chelsea premier-league-table.txt
will print the search work and one line below and above it
IO Redirection
IO Redirection
==============
Standard Input
Standard Output
Standard Error
Redirect STDOUT
---------------
echo $shell > shell.txt
(will overwrite)
echo "this is the bash shell" >> shell.txt
(will uppend the file)
Redirect STDERR
-----------------
cat missing_file 2> error.txt
(will create a file and overwrite it)
cat missing_file 2>> shell.txt
(will uppend)
cat missing_file 2> /dev/null
(redirect without display error on screen)
/dev/null is refferred as bit packet
command line Pipes
grep Hello sample.txt | less
Hello There!
(END)
less sample.txt
(will display all linesin file)
echo $SHELL | tee shell.txt
(to redirect and overwrite error on shell.txt)
echo "this is the bash shell"| tee -a shell.txt
(to redirect and uppend error on shell.txt)
Files System
ext2
max file system 2TB
max vol size 4TB
ext3
quicker startup after ungrateful shutdown
ext4
max 16TB file size
max 1 Exabyte volue size
mkfs.ext4 /dev/sdb1
mkdir /mnt/ext4;
mount /dev/sdb1 /mnt/ext4
mount | grep /dev/sdb1
df -hp | grep /dev/sdb1
/etc/fstab
echo "/dev/sdb1 /mnt/ext4 ext4 rw 0 0" >> /etc/fstab
creating partition
lsblk
sdb
gdisk /dev/sdb
(like fdisk inGPT)
?
to list all options
n
1
2048
4194306
W
will create /dev/sdb1
sudo fdisk -l /dev/sdb
to see the partition details
storage basics
/dev
block stograge device
ssd/HDD
lsblk
ls -l
b (first character)
sda - entire disk
sda1, 2, 3 - partitions
maj: 8 SD
Min: partition numbers
sudo fdisk -l /dev/sda
print partition info
partition types
primary - to boot OS
extended - cannot be used as its own. can have four logical partitions
MBR - master boot record
can have only four primary per disk
2 TB max
GPT
GUID partition Table
can have unlimited primary partion
no disk space limitation
Troubleshooting Network
ip link
to see the interface is set up
nslookup caleston-repo-0l
to see whether can resolve the IP
ping caleston-repo-01
traceroute 192.168.2.5
netstat -an | grep 80 | grep -i LISTEN
to see the port 80 is listening
ip link
(on server side)
ip link set dev enp1s0f1 up
(to set the network interface of server up)
Record types
A - websever - IPV4 192.168.1.1
AAAA - websever - IPV6
CNAME - food.web-server - eat.web-server, hungry.web-server
nslookup www.google.com
server: 8.8.8.8
Address: 8.8.8.8#53
Switching & Routing
ip link
(to check the interface for the host)
ip addr
ip addr add 192.168.1.10/24 dev eth0
(add host ip to the network)
Router
------
Gateway
route
(to check the routing info, gateway)
ip route
ip route add 192.168.2.0/24 via 192.168.1.1
ip route add default via 192.168.2.1 (network gateway add)
(default can be 0.0.0.0)
/etc/network/interface
SSH and SCP
SSH
login in remote computer
ssh <hostname or IP Address>
ssh <user>@<hostname or IP address>
ssh -l <user> <hostname or IP address>>
ssh devapp01
keypair
private+public
password-less ssh
ssh-keygen -t rsa
public key stored under
/home/bob/.ssh/id_rsa.pub
Private key
/home/bob/.ssh/id_rsa
to copy public key to transfer to remote system
ssh-copy-id bob@devapp01
stored in remote server under
cat /home/bob/.ssh/authorized_keys
scp
----
copy files from client to webserver
scp /home/bob/calseton-code.tar.gz devapp01:/home/bob
scp /home/bob/caleston-code.tar.gz devapp01:/root
scp -pr /home/bob/media/ devapp01:/home/bob
DNS
====
/etc/hosts
/etc/resolv.conf (point to a name server)
nameserver 192.168.1.100
search mycompany.com
/etc/nsswitch.conf (order change to lookup first)
Access control files
grep -i ^bob /etc/passwd
username:password:uid:gid:gecos:homedir:shell
/etc/shadow
(password are stored, contents are hashed)
username:password:lastchange:minage:maxage:warn:inactive:expdate
/etc/group
name:password:gid:members
Linus file permissions
-----------------------
-rwx rwx r-x
-regular file
first 3 -owner
second 3 - group
third 3 - others
r - 4
w - 2
x - 1
- - no permission 0
modifying file permission
-------------------------
chmod <permission> file
chmod u+rwx test-file
chmod ugo+r-x test-file
chmod o-rwx test-file
chmod u+rwx, g+r-x,o-rwx test file
chmod 777 test-file
chmod 555 test-file
chmod 660 test-file
chmod 750 test-file
change ownership
----------------
chown owner:group file
chown bob:developer test-file
chown bob andoid.apk
chgrp android test-file
managing users
useradd bob
grep -i bob /etc/passwd
grep -i bob /etc/shadow
passwd bob
whoami
passwd (user change password)
delete user account
userdel bob
groupadd -g 1011 developer
groupdel developer
switching users
---------------
su ~
password:
su -c "whoami"
password:
(not recommended as u need password of the switching user)
better
sudo apt-get install nginx
[sudo] password for michael:
cat /etc/sudoers
only users listed here can use sudo command
visudo
(used to edit the list)
/etc/sudoers
no login shell
grep -i ^root /etc/passwd
(no one can login to root with password directly)
user privilege specification
(refer screenshot)
Account Types
User account
Superuser account - root
System Accounts - ssh, mail
Service Accounts - nginx, mercury
id
who - who currently logged in the system
last - display all logged in users, reboot date and time