apt-get
, apt-cache
, dpkg
yum
, rpm
, dnf
fstab
ls
, cp
, mv
, rm
, touch
cat
, more
, less
, head
, tail
chmod
, chown
, chgrp
ps
, top
, kill
, jobs
, bg
, fg
top
, htop
, vmstat
, iostat
/dev
directory, major and minor numbersfdisk
, parted
, mkfs
, fsck
/
, /home
, /var
, /etc
, /usr
bash
, sh
, zsh
PATH
, HOME
, USER
#!/bin/bash
), variables, loops, conditionalstar
, gzip
, rsync
useradd
, usermod
, userdel
groupadd
, groupmod
, groupdel
chmod
, chown
, chgrp
ifconfig
, ip
, nmcli
ping
, traceroute
, netstat
, ss
tcpdump
, nmap
iptables
, firewalld
/var/log
)top
, vmstat
, iostat
systemctl
journalctl
, /var/log/messages
CPU (Central Processing Unit):
RAM (Random Access Memory):
Storage Devices:
HDD (Hard Disk Drive):
SSD (Solid State Drive):
Motherboard:
x86 Architecture:
ARM Architecture:
BIOS (Basic Input/Output System):
UEFI (Unified Extensible Firmware Interface):
GRUB (GRand Unified Bootloader):
/boot/grub/grub.cfg
is used to define boot options and settings.LILO (LInux LOader):
/etc/lilo.conf
was used to configure boot options.Ubuntu:
CentOS:
Debian:
Filesystem Types:
Partitioning Schemes:
MBR (Master Boot Record):
GPT (GUID Partition Table):
Common Partitions:
APT (Advanced Package Tool):
sudo apt-get update
: Updates the package list.sudo apt-get upgrade
: Upgrades all installed packages.sudo apt-get install <package_name>
: Installs a package.YUM (Yellowdog Updater, Modified):
sudo yum update
: Updates all packages.sudo yum install <package_name>
: Installs a package.RPM (Red Hat Package Manager):
rpm -ivh <package_name>.rpm
: Installs a package.rpm -e <package_name>
: Uninstalls a package.DNF (Dandified YUM):
sudo dnf install <package_name>
: Installs a package.sudo dnf update
: Updates installed packages.apt-get:
apt-get
is used for managing packages from the terminal.apt-get install <package_name>
: Installs a new package.apt-get remove <package_name>
: Removes a package.apt-get update
: Refreshes the package index.apt-get upgrade
: Upgrades all packages to the latest versions.apt-cache:
apt-cache search <keyword>
: Searches for packages matching the keyword.apt-cache show <package_name>
: Displays detailed information about a package.dpkg:
dpkg
is the base command for handling .deb packages on Debian-based systems.dpkg -i <package_name>.deb
: Installs a package.dpkg -r <package_name>
: Removes a package.dpkg -l
: Lists all installed packages.yum:
yum
is the default package manager for RPM-based distributions like CentOS and RHEL.yum install <package_name>
: Installs a package.yum remove <package_name>
: Removes a package.yum update
: Updates installed packages.yum list installed
: Lists all installed packages.rpm:
rpm
is a package management utility for installing, querying, verifying, updating, and removing RPM packages.rpm -ivh <package_name>.rpm
: Installs a package with verbose output and progress.rpm -e <package_name>
: Removes a package.rpm -qa
: Lists all installed RPM packages.dnf:
dnf
is the next-generation version of yum
and is the default package manager in Fedora and newer versions of CentOS and RHEL.dnf install <package_name>
: Installs a package.dnf remove <package_name>
: Removes a package.dnf update
: Updates installed packages.dnf list installed
: Lists all installed packages.zypper
is the package manager used by openSUSE and SUSE Linux Enterprise.zypper install <package_name>
: Installs a package.zypper remove <package_name>
: Removes a package.zypper update
: Updates all installed packages.zypper search <package_name>
: Searches for a package in the repositories.zypper info <package_name>
: Displays detailed information about a package.zypper list-updates
: Lists available updates for installed packages.zypper refresh
: Refreshes the package repositories.zypper addrepo <repository_url> <repo_alias>
: Adds a new repository.zypper removerepo <repo_alias>
: Removes a repository.zypper clean
: Cleans up the local repository cache.zypper patch
: Applies all necessary patches for the system.zypper dist-upgrade
: Performs a distribution upgrade, updating the entire system to a new release.Understanding of File System Hierarchy:
ls
, cp
).fstab:
fstab
(File System Table) file is used by the system to automatically mount partitions and devices at boot time./mnt/data
).ext4
, xfs
).defaults
, noatime
).ls
(List)
ls -l
: Lists files in long format, showing permissions, owner, size, and modification time.ls -a
: Includes hidden files (those starting with a dot).ls -h
: Human-readable file sizes.ls -R
: Lists directories and their contents recursively.cp
(Copy)
cp [options] source destination
cp -r
: Recursively copies directories and their contents.cp -i
: Prompts before overwriting files.cp -u
: Copies only when the source file is newer than the destination file.cp -p
: Preserves the file attributes (permissions, timestamps, etc.).mv
(Move/Rename)
mv [options] source destination
mv -i
: Prompts before overwriting files.mv -u
: Moves only when the source file is newer than the destination file.mv -v
: Verbose output showing the actions being performed.rm
(Remove)
rm [options] file
rm -r
: Recursively removes directories and their contents.rm -i
: Prompts before each removal.rm -f
: Forces removal without prompts (use with caution).rm -v
: Verbose output showing the files being removed.touch
touch [options] file
touch newfile.txt
creates an empty file named newfile.txt
.touch -c
: Does not create any files; only changes the timestamps if the file exists.cat
(Concatenate)
cat [options] file
cat file.txt
displays the content of file.txt
.cat -n
: Numbers all output lines.more
more file
space
to scroll down one screen, Enter
to scroll one line.less
more
, but with the ability to scroll both forward and backward.less file
space
to scroll down, b
to scroll up, and q
to quit.less -S
: Truncates long lines instead of wrapping them.head
head [options] file
head -n N
: Displays the first N
lines of the file.head -c N
: Displays the first N
bytes of the file.tail
tail [options] file
tail -n N
: Displays the last N
lines of the file.tail -f
: Continuously displays new lines as they are added to the file (useful for log files).tail -c N
: Displays the last N
bytes of the file.file
file [options] file
file myfile.txt
displays the type of myfile.txt
.du
(Disk Usage)
du [options] [directory]
du -h
: Human-readable format (e.g., KB, MB).du -s
: Summary (total size of the specified directory).df
(Disk Free)
df [options] [directory]
df -h
: Human-readable format (e.g., GB, TB).mkdir
(Make Directory)
mkdir [options] directory
mkdir -p
: Creates parent directories as needed.rmdir
(Remove Directory)
rmdir directory
rmdir -p
: Removes parent directories if they are empty.These commands cover a broad range of basic file and directory operations, useful for day-to-day Linux command-line tasks.
cat
(Concatenate)
cat [options] file
cat file.txt
displays the content of file.txt
.more
more file
space
to scroll down one screen, Enter
to scroll one line.less
more
, but with the ability to scroll both forward and backward.less file
space
to scroll down, b
to scroll up, and q
to quit.head
head [options] file
head -n N
: Displays the first N
lines of the file.tail
tail [options] file
tail -n N
: Displays the last N
lines of the file.tail -f
: Continuously displays new lines as they are added to the file (useful for log files).chmod
(Change Mode)
chmod [options] mode file
chmod u+x file
adds execute permission to the user (owner).chmod 755 file
sets permissions to rwxr-xr-x
.chown
(Change Owner)
chown [options] owner file
chown user file
: Changes the owner to user
.chown user:group file
: Changes the owner to user
and group to group
.chgrp
(Change Group)
chgrp [options] group file
chgrp group file
changes the group ownership to group
.ps
(Process Status)
ps [options]
ps aux
: Shows detailed information about all running processes.top
top
q
to quit, h
for help, and k
to kill a process.kill
kill [options] PID
kill -9 PID
forcefully terminates the process with the given PID
.jobs
jobs [options]
jobs -l
: Lists the PID along with job information.bg
(Background)
bg [job_spec]
bg %1
resumes job number 1 in the background.fg
(Foreground)
fg [job_spec]
fg %1
brings job number 1 to the foreground.top
top
top
to monitor system performance and resource usage.htop
top
, offering a more user-friendly and interactive interface.htop
vmstat
(Virtual Memory Statistics)
vmstat [options] [delay [count]]
vmstat 1 5
: Displays stats every 1 second for 5 iterations.iostat
(Input/Output Statistics)
iostat [options] [interval] [count]
iostat -x
: Provides extended statistics, including disk utilization and response times./dev
Directory:
/dev
directory contains special files representing devices attached to the system. These files allow software to interact with hardware through file-like interfaces.Major and Minor Numbers:
fdisk
:
fdisk -l
: Lists all partitions on all devices.fdisk /dev/sda
: Opens the partition table of the specified device for editing.parted
:
fdisk
, used for managing disk partitions, especially on large or GPT-partitioned disks.parted /dev/sda
: Opens the specified device for partitioning.mklabel gpt
: Creates a new GPT partition table.mkpart primary ext4 1MiB 100%
: Creates a primary partition of type ext4 using the entire disk.mkfs
:
mkfs -t [filesystem_type] /dev/[partition]
mkfs.ext4 /dev/sda1
creates an ext4 filesystem on partition /dev/sda1
.fsck
:
fsck [options] [filesystem]
fsck /dev/sda1
checks the filesystem on /dev/sda1
for errors and attempts to fix them.Here’s the updated section with comments and additional details:
/
(Root Directory):
/bin
, /etc
, /lib
, and /sbin
./home
:
/home
for personal files and settings (e.g., /home/username
)./var
:
/var/log
: Contains log files./var/spool
: Holds spools for mail, printing, etc./var/tmp
: Temporary files that need to persist between reboots./etc
:
/etc/passwd
: Contains user account information./etc/fstab
: Lists filesystems to be mounted at boot./etc/network/interfaces
: Configures network interfaces./usr
:
/usr/bin
: User commands and applications./usr/lib
: Shared libraries./usr/share
: Shared data, including documentation and application-specific data./opt
:
/mnt
:
/media
:
/lib
:
/lib64
:
/proc
:
/proc/cpuinfo
for CPU details./sys
:
/dev
:
/dev/sda
for disks)./boot
:
initrd
).ext4
(Fourth Extended Filesystem):
XFS
:
Btrfs
(B-tree Filesystem):
FAT32
(File Allocation Table 32):
NTFS
(New Technology File System):
ntfs-3g
driver.XFS
(Extended Filesystem X):
ReiserFS
:
JFS
(Journaled File System):
ZFS
(Zettabyte File System):
Bash (Bourne Again Shell):
sh
shell, offering more features like improved scripting, command-line editing, and command history.Sh (Bourne Shell):
sh
highly portable.Zsh (Z Shell):
PATH
:
PATH
to find the corresponding executable.export PATH=$PATH:/usr/local/bin
adds /usr/local/bin
to the existing PATH
.HOME
:
cd $HOME
changes the current directory to the user’s home directory.USER
:
echo $USER
prints the username of the current user.Shebang (#!/bin/bash
):
#!/bin/bash
tells the system to use the Bash shell to run the script.#!/bin/bash
echo "Hello, World!"
Variables:
name="Alice"
echo "Hello, $name!"
Loops:
for i in {1..5}
do
echo "Iteration $i"
done
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
count=$((count + 1))
done
Conditionals:
if [ "$name" == "Alice" ]; then
echo "Hello, Alice!"
else
echo "You are not Alice."
fi
Input/Output Redirection:
>
writes output to a file (overwrites existing content), >>
appends output to a file.echo "Hello, World!" > output.txt
echo "Appended line" >> output.txt
<
takes input from a file.sort < unsorted.txt
Pipes:
|
) are used to pass the output of one command as input to another command.ls | grep "file"
tar
:
tar -cvf archive.tar /path/to/files
tar -xvf archive.tar
gzip
:
tar
to create compressed tarballs (.tar.gz
).gzip file.txt
creates file.txt.gz
gunzip file.txt.gz
rsync
:
rsync -avz /source/path /destination/path
Creating Archives:
tar
is commonly used for this.tar -czvf archive.tar.gz /path/to/directory
creates a compressed archive.Extracting Archives:
tar
or unzip
depending on the format.tar -xzvf archive.tar.gz
extracts the contents of the compressed archive.useradd
:
useradd [options] username
-m
: Creates a home directory for the user if it doesn’t exist.-s /bin/bash
: Specifies the default shell for the user.-G groupname
: Adds the user to a specified group.-u UID
: Sets a specific user ID (UID).-d /home/username
: Specifies a custom home directory.useradd -m -s /bin/bash -G sudo -u 1001 -d /home/alice alice
creates a user named “alice” with a home directory /home/alice
, Bash as the default shell, adds them to the “sudo” group, and assigns the UID 1001.passwd
:
passwd [options] username
-d
: Deletes the user’s password (making it possible for the user to log in without a password).-l
: Locks the user account by disabling the password.-u
: Unlocks the user account.passwd alice
prompts you to enter and confirm a new password for the user “alice”.usermod
:
usermod [options] username
-c "Comment"
: Adds or modifies the comment field (often used for the full name).-G groupname
: Changes or adds supplementary groups.-l newname
: Changes the user’s login name.-aG groupname
: Adds the user to a supplementary group without removing them from other groups.usermod -aG developers alice
adds user “alice” to the “developers” group while retaining their existing group memberships.userdel
:
userdel [options] username
-r
: Removes the user’s home directory and mail spool.-f
: Forces the deletion of the user account even if the user is logged in.userdel -r alice
deletes the user “alice” and their home directory.groupadd
:
groupadd [options] groupname
-g GID
: Specifies a custom group ID (GID).groupadd -g 1002 developers
creates a group named “developers” with the GID 1002.groupmod
:
groupmod [options] groupname
-n newname
: Renames the group.-g GID
: Changes the group ID (GID).groupmod -n devs developers
renames the “developers” group to “devs.”groupdel
:
groupdel groupname
groupdel devs
deletes the group “devs.”Create a New User:
useradd
useradd [options] username
-m
: Creates a home directory for the user if it doesn’t exist.-s /bin/bash
: Specifies the default shell for the user.-G groupname
: Adds the user to a specified group.-u UID
: Sets a specific user ID (UID).-d /home/username
: Specifies a custom home directory.useradd -m -s /bin/bash -G sudo -u 1001 -d /home/alice alice
creates a user named “alice” with a home directory /home/alice
, Bash as the default shell, adds them to the “sudo” group, and assigns the UID 1001.Set or Change User Password:
passwd
passwd [options] username
-d
: Deletes the user’s password (making it possible for the user to log in without a password).-l
: Locks the user account by disabling the password.-u
: Unlocks the user account.passwd alice
prompts you to enter and confirm a new password for the user “alice”.Modify Existing User:
usermod
usermod [options] username
-c "Comment"
: Adds or modifies the comment field (often used for the full name).-G groupname
: Changes or adds supplementary groups.-l newname
: Changes the user’s login name.-aG groupname
: Adds the user to a supplementary group without removing them from other groups.usermod -aG developers alice
adds user “alice” to the “developers” group while retaining their existing group memberships.Delete a User:
userdel
userdel [options] username
-r
: Removes the user’s home directory and mail spool.-f
: Forces the deletion of the user account even if the user is logged in.userdel -r alice
deletes the user “alice” and their home directory.Lock a User Account:
passwd -l
passwd -l username
passwd -l alice
locks the user account for “alice”, preventing login.Unlock a User Account:
passwd -u
passwd -u username
passwd -u alice
unlocks the user account for “alice”, allowing login.Set Password Expiration Policies:
chage
chage [options] username
-M DAYS
: Sets the maximum number of days between password changes.-W DAYS
: Sets the number of days before expiration during which the user is warned.-I DAYS
: Sets the number of days after password expiration before the account is disabled.chage -M 90 -W 14 alice
sets the password for “alice” to expire every 90 days and warns them 14 days before expiration.View User Account Information:
finger
(if installed)finger username
finger alice
provides details about the user “alice”, such as login name, home directory, and shell.chmod
:
chmod [options] mode file
u
(user), g
(group), o
(others), r
(read), w
(write), x
(execute).r=4
, w=2
, x=1
.chmod u+rwx,g+rx,o+r file.txt
: Gives the owner full permissions, the group read and execute, and others read permission.chmod 755 file.txt
: Sets the permissions to rwxr-xr-x
.chown
:
chown [options] owner[:group] file
chown alice file.txt
: Changes the owner of “file.txt” to “alice”.chown alice:developers file.txt
: Changes the owner to “alice” and the group to “developers”.chgrp
:
chgrp [options] group file
chgrp developers file.txt
changes the group of “file.txt” to “developers.”setfacl
:
setfacl [options] acl_spec file
setfacl -m u:alice:rwx file.txt
gives user “alice” read, write, and execute permissions on “file.txt”.getfacl
:
getfacl file
getfacl file.txt
displays the ACLs set on “file.txt”.ifconfig
:
ifconfig [interface] [options]
ifconfig
ifconfig eth0 192.168.1.100 netmask 255.255.255.0
ifconfig eth0 up
or ifconfig eth0 down
ifconfig
is now deprecated on many Linux distributions in favor of the ip
command.ip
:
ifconfig
for managing network interfaces, IP addresses, routes, and tunnels.ip [options] object command
ip addr show
ip addr add 192.168.1.100/24 dev eth0
ip link set eth0 up
or ip link set eth0 down
ip route show
nmcli
:
nmcli [options] [command]
nmcli connection show
nmcli connection up id "connection_name"
nmcli connection modify "connection_name" ipv4.addresses 192.168.1.100/24
nmcli networking off && nmcli networking on
Purpose: Network interfaces are physical or virtual devices that enable a system to connect to a network.
Configuration:
/etc/network/interfaces
(Debian-based systems) or /etc/sysconfig/network-scripts/ifcfg-*
(Red Hat-based systems).dhclient
.Commands:
ip link
: Used to manage network interfaces.
ip link set eth0 up
to bring an interface up.ethtool
: Used to display or change Ethernet device settings.
ethtool eth0
displays the link status and speed.ping
:
ping [options] destination
ping google.com
sends ICMP echo requests to google.com to check connectivity.traceroute
:
traceroute [options] destination
traceroute google.com
displays the route packets take to reach google.com.netstat
:
netstat [options]
-t
: Shows TCP connections.-u
: Shows UDP connections.-l
: Lists listening ports.netstat -tuln
displays listening TCP and UDP ports in numerical form.ss
:
netstat
and is considered faster.ss [options]
-t
: Shows TCP sockets.-u
: Shows UDP sockets.-l
: Lists listening sockets.ss -tuln
displays listening TCP and UDP sockets.tcpdump
:
tcpdump [options]
tcpdump -i eth0
tcpdump port 80
tcpdump -w file.pcap
tcpdump -i eth0 port 80
captures all HTTP traffic on interface eth0.nmap
:
nmap [options] target
nmap 192.168.1.1-254
nmap -sT target
nmap -p 22,80 target
nmap -sT -p 22,80 google.com
scans for SSH and HTTP ports on google.com.-rwxr-xr--
).-
for a regular file, d
for a directory).chmod
: Command to change file or directory permissions.
chmod [permissions] [file]
chmod 755 myfile
gives read, write, and execute permissions to the owner, and read and execute permissions to the group and others.SUID (Set User ID):
s
replaces the execute (x
) bit in the owner’s permission set.chmod u+s /path/to/file
sets the SUID bit.SGID (Set Group ID):
s
replaces the execute (x
) bit in the group’s permission set.chmod g+s /path/to/directory
sets the SGID bit.Sticky Bit:
t
replaces the execute (x
) bit in the others’ permission set.chmod +t /path/to/directory
sets the sticky bit.iptables
:
iptables [options] [chain] [rule-specification]
iptables -A INPUT -s 192.168.1.100 -j DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables-save > /etc/iptables/rules.v4
firewalld
:
firewall-cmd [options]
firewall-cmd --get-active-zones
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload
Password Policies:
/etc/login.defs
to set password expiration rules.chage [options] [username]
chage -M 90 username
sets the maximum number of days before a password must be changed to 90.Account Locking:
passwd -l [username]
: Lock a user account.passwd -u [username]
: Unlock a user account.faillog
: Display and manage user account failed login attempts.faillog -u [username]
shows the failed login attempts for a user.Keeping Software Up-to-Date:
Common Package Managers:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
(handles dependencies more intelligently and might remove some obsolete packages).sudo yum check-update
or sudo dnf check-update
sudo yum update
or sudo dnf upgrade
sudo zypper refresh
sudo zypper update
Importance of System Logs:
/var/log
directory, with various logs tracking different aspects of system operation.Common Log Files:
Viewing and Managing Logs:
tail -f /var/log/syslog
for live updates)./etc/logrotate.conf
and /etc/logrotate.d/
.Monitoring System Performance:
Key Tools:
top
, and press q
to quit.vmstat [interval] [count]
vmstat 2 5
provides five updates every two seconds.iostat [options] [interval] [count]
iostat -d 2 5
reports disk I/O statistics every two seconds, five times.Optimizing System Performance:
Key Performance Metrics:
GRUB Issues:
sudo mount /dev/sda1 /mnt
sudo grub-install --boot-directory=/mnt/boot /dev/sda
sudo update-grub
set root=(hd0,1)
linux /vmlinuz root=/dev/sda1
initrd /initrd.img
boot
Kernel Panics:
fstab
or boot parameters).systemctl
to check the status:sudo systemctl status <service_name>
sudo systemctl restart <service_name>
journalctl
:sudo journalctl -u <service_name>
Using journalctl
:
journalctl
is used to view system logs managed by systemd
, which includes logs for boot processes, service management, and more.sudo journalctl
sudo journalctl -b
sudo journalctl -u <service_name>
sudo journalctl -f
sudo journalctl | grep "<search_term>"
/var/log/messages:
cat /var/log/messages
grep -i 'error\|warning' /var/log/messages
tail -n 100 /var/log/messages
Booting into Recovery Mode:
Shift
(for BIOS systems) or Esc
(for UEFI systems).Rescuing Files:
sudo mount /dev/sda1 /mnt
sudo cp -r /mnt/home/user/important_files /external_drive/