File Permissions and Ownership:
-------------------------------
chmod: Changes file or directory permissions.
The chmod (change mode) command in Unix/Linux is used to change the file or
directory permissions.
Permissions control who can read, write, or execute a file or directory. Here's how
to use chmod effectively:
Basic File Permissions in Unix/Linux
Each file and directory has three types of permissions:
Read (r) – Allows reading the file or listing the directory contents.
Write (w) – Allows modifying the file or adding/removing files in a directory.
Execute (x) – Allows running the file (if it's a script or program) or entering the
directory.
1. Viewing File Permissions
Before changing permissions, you can view the current permissions using the ls -l
command:
ls -l filename
Output:
-rwxr-xr-- 1 user group 1234 Sep 19 12:00 filename
This means:
Owner (u): rwx (read, write, execute)
Group (g): r-x (read, execute)
Others (o): r-- (read only)
2. Changing Permissions Using Numeric (Octal) Mode
In this mode, permissions are represented by numbers:
4 = Read (r)
2 = Write (w)
1 = Execute (x)
0 = No permission (-)
To set permissions, you sum the values for each category (user, group,
others):
7 = Read (4) + Write (2) + Execute (1) = rwx
6 = Read (4) + Write (2) = rw-
5 = Read (4) + Execute (1) = r-x
4 = Read (4) = r--
Example:
chmod 755 filename
This sets:
Owner: 7 (rwx)
Group: 5 (r-x)
Others: 5 (r-x)
2. Changing Permissions Using Symbolic Mode
In symbolic mode, you modify permissions by specifying the category (u, g, o), the
operation (+, -, =), and the permission (r, w, x).
+ to add permission
- to remove permission
= to set exact permission
--------------------------------
chown
----------------
chgrp
----------------
----------------------------------------------------------------------
Monitoring System Resources:
top:
Displays real-time system processes and resource usage (CPU, memory).
1. Change Update Interval (-d):
Use the -d option to set the delay between updates in seconds.
Example:
top -d 5
This updates the display every 5 seconds.
2. top:
top - 14:03:05 up 10 days, 3:12, 2 users, load average: 0.35, 0.42, 0.45
Tasks: 154 total, 1 running, 153 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.2 us, 1.5 sy, 0.0 ni, 94.3 id, 0.6 wa, 0.0 hi, 0.4 si, 0.0 st
KiB Mem : 8095636 total, 2039320 free, 2020148 used, 403216 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 2176200 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 root 20 0 162456 10256 2568 S 0.0 0.1 0:00.12 bash
2345 user1 20 0 1048952 134232 84516 S 0.0 1.6 2:23.34 firefox
3456 user2 20 0 456789 34128 19016 R 0.0 0.4 0:01.56 top
--------------------------------------------------------------------------------
ps
1. ps aux
Displays a detailed list of all running processes.
a: Show processes for all users.
u: Show process details including the user and CPU/memory usage.
x: Show processes not attached to a terminal.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 22564 1860 ? Ss Sep01 0:00 /sbin/init
user1 2345 5.2 1.6 1048952 134232 ? Sl Sep15 25:36
/usr/lib/firefox/firefox
user2 3456 0.0 0.4 456789 34128 pts/0 R+ 14:30 0:01 ps aux
---------
2. ps -e or ps -A:
Shows all processes in a simple format.
e or A: Show all processes
ps -e
PID TTY TIME CMD
1 ? 00:00:00 init
2345 ? 25:36:54 firefox
3456 pts/0 00:01:00 ps
-------
3. ps -u <username>:
Shows processes for a specific user.
-u <username>: Specify the username to filter processes.
ps -u user1
PID TTY TIME CMD
2345 ? 25:36:54 firefox
-----------------------------------------------
df
1. df: Displays disk space usage for all mounted file systems.
2. df -h: -Displays disk space in a human-readable format (e.g., KB, MB, GB).
-Check if the disk is running out of space.
-Check how much free space available
-----------------------------------------------
du --> Shows disk usage for files and directories (e.g., du -sh to see human-
readable sizes).
1. Shows only the total size of the specified directory in human readable format,
not the size of each subdirectory.
du -sh /path/to/directory
2. Display Disk Usage for All Files and Directories:
du -ah /path/to/directory
3. Display Disk Usage for All Files and Directories in current working directory:
du -h
--------------------------------------------------------
free
1. free - free command in Unix/Linux is used to display information about free and
used memory in the system,
including RAM and swap space.
2. -h option makes the output easier to read by using human-friendly units (KB, MB,
GB).
free -h
total used free shared
buff/cache available
Mem: 16Gi 8.1Gi 4.5Gi 1.0Gi 3.4Gi
6.5Gi
Swap: 2.0Gi 0B 2.0Gi
3. -s allows monitoring memory usage at regular intervals, which is useful for
tracking memory over time.
free -s 10
--Display Memory Usage Every 10 Seconds
---------------------------------------------------------------
uptime
Shows how long the system has been running and the system load averages.
uptime
output:
14:03:05 up 10 days, 3:12, 2 users, load average: 0.35, 0.42, 0.45
----------------------------------------------------------------
iostat
1. it provides detailed information about CPU and I/O device usage,
helping in system performance monitoring and troubleshooting.
iostat
-------------------------------
Linux 5.4.0-42-generic (hostname) 09/19/2024 _x86_64_ (4 CPU)
avg-cpu: %usr %sys %iowait %idle
15.00 10.00 2.00 73.00
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 30.00 100.00 50.00 20000 10000
-------------------------------
-x option gives more detailed device statistics, useful for in-depth analysis.
-c focuses on CPU statistics, while -d focuses on I/O device statistics.
$ iostat -c
Linux 5.4.0-42-generic (hostname) 09/19/2024 _x86_64_ (4 CPU)
avg-cpu: %usr%sys %iowait %idle
20.00 10.00 5.00 65.00
-------------------------------
$ iostat -d
Linux 5.4.0-42-generic (hostname) 09/19/2024 _x86_64_ (4 CPU)
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 30.00 150.00 75.00 30000 15000
sdb 40.00 200.00 100.00 40000 20000
-------------------------------
Using interval and count allows for real-time monitoring over specified periods.
---------------------------------------------------
vmstat: Displays system performance including memory, CPU, and I/O usage.
$ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 10240 10240 1024 2048 0 0 12 20 100 50 10 5 85 0
---------------------------------------------------
PROCESS MANAGEMENT
kill: Terminates processes by process ID (PID) (e.g., kill -9 <PID>).
The kill command in Unix/Linux is used to terminate processes by sending signals to
them.
The most common use is to stop a process by specifying its Process ID (PID).
1. Terminate a Process Gracefully: kill pid
kill 1234
This sends the default SIGTERM signal to the process with PID 1234.
2. Forcefully Kill a Process: kill -9 pid
kill -9 1234
Forcefully terminates a process. This signal cannot be caught or
ignored by the process.
This will forcefully kill the process with PID 1234.
3. Terminate Multiple Processes:
kill 1234 5678
--------------------------------------------------------
killall: The killall command in Unix/Linux is used to terminate processes by name
rather than by Process ID (PID).
1. Terminate All Processes Named firefox:
killall firefox
This sends the default SIGTERM signal to all processes with the name
firefox.
2. Forcefully Kill All Processes Named firefox:
killall -9 firefox
This sends a SIGKILL signal to all processes with the name firefox.
3. Terminate All Processes Named nginx Belonging to User www-data:
killall -u www-data nginx
4. Interactive Mode for Killing Processes Named vim:
killall -i vim
This will prompt for confirmation before killing each vim process.
-------------------------------------------------------
nice / renice:
Adjusts the priority of running processes.
nice [options] [command]
------------------
nohup:
Runs a command in the background, allowing it to continue after logging out.
nohup long_running_command &
-----------------------------------------------------
ping:
The ping command is a common network utility used to test connectivity between your
computer and a remote host,
such as a server or another device on a network.
It helps determine if a networked device is reachable and measures the round-trip
time for packets sent to and from the host.
ping google.com
--------------------------------------------
ifconfig: Configures network interfaces.
-----------------------------------------------
netstat
1. Displays all network connections and listening ports.
netstat -a
2. Shows all TCP connections.
netstat -t
-----------------------------------------------
scp: Securely copies files between hosts.
ssh: Connects to a remote machine securely.
-----------------------------------------------
nslookup:
nslookup stands for "name server lookup."
It is used to query DNS to obtain domain name or IP address mapping information.
It provides a simple way to perform DNS queries and is available on most operating
systems.
----------------------------------------------
who:
Displays information about who is currently logged in.
whoami:
Shows the current user.
useradd, usermod, userdel:
Commands to manage users.
passwd: Changes the user's password.
1.Changing Your Own Password:
passwd
------------------
Changing password for user john.
Current password:
New password:
Retype new password:
2. Changing Another User's Password (as root):
sudo passwd alice
--------------------
Enter new UNIX password:
Retype new UNIX password:
-------------------------------------------------