0% found this document useful (0 votes)
40 views

Linux Admin 1 Commands

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Linux Admin 1 Commands

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Command

Command Jop Example


Managing Files
date Display the current date and
time on the machine
date +%R Show only time
date +%x Show only date
passwd Change login user password
passwd + user Change pass for another usr
file Everything is a file in Linux file /etc/passwd
so we use this to scan file
content & replay with the
type of file
cat Display the all content for cat file1 file2
one or multiple txt files cat /etc/redhat-release
less Display one page of file at a less /etc/passwd
time
head Display the first 10 lines
from the file
tail Display the Last 10 lines
from the file
head/tail -n Define the number of lines
you need to display
history List all the last executed !4 to execute command number 4
commands in CLI from history commands
\ Continuing long command
in another line
pwd Display your current
working directory
cd Change your working If you use it without any argument with
directory change your working directory to your
home directory
ls List all files and Dir in your
location
Ls -l Long list
Ls -a Show also hidden files ls -al
ls -lt List using the last
modification time
ls -ltr Show the last created files
at first
ls -lR List all files and directories
and sub directory
ls -lh Make file size more
readable
ls -li to list files with inode
number
dir Same as ls but output, not
collard
dir --color = ls
touch Create file or more than one touch file1
files at same time touch file1 file2 file3
touch {file1,file2,file3}
mkdir Create on or more mkdir folder1
directories at same time mkdir folder1 folder2 folder3
mkdir -p …./…/….. Make more than one mkdir -p dir1/dir2/dir3
directory inside them selves
mkdir ‘ ’ = mkdir “ “ Make dir with long name mkdir ‘mohamed Elshafie’
and name have spaces
cp Copy file from place to cp -a …/… /…. /…
another copy a file and ensure that its attributes
(such as ownership, permissions, and
timestamps) are preserved,
cp -r Copy dir with his content cp -r /etc/ .
mv To move or rename file mv mohamed ahmed (rename)
mv ali /etc (cut)
rm Remove file and will ask you
for remove
rm -f To force remove files/dir
rmdir + dir name Remove empty directory
rm -r + dir name Remove dir with content
rm -rf +dir name Force remove dir with
content
rm -rf * Remove all files and dir
Soft / Hard Link
ln filename new-name Make a hard link for the file ln file1.txt new-hardlink.txt
cannot make a hard link for when make ls -l on file it will show
the directory number of hard link exist in file after
permissions
ln -s file/dir new file /dir Make soft link for files or Output for ls -li file will show
directories …original file…… -> …soft link file….
(this is called soft link)
When the link is blue it exists
If it is in red, it does not exist or the
original file was deleted
Regular expression Pattern Matching RegEx
ls f* List all files or directories
starting with f
ls ???? List all files or directories
their name consists of 4
chars
ls [fa]* List all files or directories
starting with f or a
ls ![fa]* List all files or directories
not starting with f or a
ls [a-c]* List all files or directories
starting with a range of
chars from a to c
ls [^a-c]* List all files or directories
not starting with a range of
chars from a to c
grep Search using the pattern grep mohamed /etc/passwd
inside the file and if any line grep bash /etc/passwd
matches this pattern will
display it
grep -l Ensure me your search grep -l mohamed /etc/passwd
exists or not in the searched output: /etc/passwd This means
file mohamed exists in file
grep -i Search using case- Output in upper and lower case
insensitive
grep -v Output all lines except lines
that match pattern
gerp -w Search for a specific word grep -v shut /etc/passwd
grep -A Search for a pattern and grep -A 2 mohamed /etc/passwd
display it and also display show matched line and also 2 lines after
some lines after this line that
grep -B Search for a pattern and grep -B 2 mohamed /etc/passwd
display it and also display show matched line and also 2 lines
some lines Before this line before that
grep -r Search for a pattern inside grep -r mohamed /etc/
the directory and all its
content (all files and
directories inside directory )
grep -lr To show me files that have
this pattern only display
files name not lines
grep -e Search for more than one grep -e mohamed -e root /etc/passwd
pattern at the same time
. Match exactly one char and grep c.t /usr/share/dict/words
use with grep command list all words that start with c and end
with t and only has one char between
them
? Match exactly one char and
use with ls command
^ Matches start of the string grep ^cat /usr/share/dict/words
list all lines that start with cat
ps aux | grep ^ root
$ Matches end of the string grep cat$ /usr/share/dict/words
list all lines that end with cat
grep ^cat$ /usr/share/dict/words
list all lines that start and end with cat
* More or zero
\ Represent special char
() Groups regular expressions grep ^c(aou)t$ /usr/share/dict/words
list all words that start with c and end
with t and between them a or o or u
cut Used for cutting out the cut -f 1 -d : /etc/passwd
section from each line or file cut -c 1-7 /etc/passwd
and displaying it display first 7 chars
cut -c 25- /etc/passwd
display from char 25 to the end of file
tr Used to translate or display echo hello world | tr a-z A-Z
text from one status to convert lower case to Upper case or can
another one do the reverse
echo hello world | tr -d ‘l’
delete l char from output
echo hello world | tr [:space:] ‘\t’
delete space and make tab
echo hello 124595 | tr -d [:digit:]
remove digital numbers
Manual pages exist on Help Section 1 for manual page for command
Section 5 manual for the config file
/usr/share/man Section 8 manual for System Admin
commands
Section 4 for special file
man + command Display manual pages man passwd
man 5 + command Display section 5 man 5 passwd
man +K + pattern Search for specific pattern in man +K print
man
which + command To display the location that which passwd
the command stored
whereis +command To display the location whereis passwd
where the manual pages for
commands is stored
Command --help To list basic options for passwd --help
command
Can also use online docs Docs.redhat.com
Tldp.org
Reading documents from That has more data than Firefox /usr/share/doc/
the system man
Stdin Input-output redirection When execute command you can
Stdout change the standard input and output
stderr devices
> File.txt Redirect stdout to overwrite Cal > file.txt
a file
>> file.txt Redirect stdout to append Cal >> file.tex
to a file
2> Redirect stderror to
overwrite a file
2> /dev/null Discard stderr error
messages by redirecting
them to /dev/null
> file 2>&1 Redirect stdout and error
&> file and overwrite it to a file
>> file 2>&1 Redirect stdout and error
&>> file and stderr to append it to
file

VIM

Insert (edit mode) Command(default) Visual


i from command Can read – print – copy – cut V or v or Ctrl+v
Esc to command Past- search -jump To and from command
: from command
Enter to command
Extend command (save)
Command mode
yy Copy a line
3yy Copy three lines
dd Cut a line
p (small) Paste after the current
cursor
P (capital) Paste before the current
cursor
/ Search forward
? Search backward
n Find the next match Used with the previous search
N Find the previous match Used with the previous search

Insert Mode
i Open insert mode and write
before the cursor
a Open insert mode and write
after the cursor
A Open insert mode and
move the cursor to the end
of the line
I Open insert mode and
move the cursor to the start
of the line
o Open insert mode and make
a new line after line I stop
on it
O Open insert mode and make
a new line before the line I
stop on it

Extend Mode
:wq Save and exit the current
line
:x Save the file if it is not saved
then quit
:w Save and remain in the
editor
:q Quit the current file if there
are no unsaved changes
:qi Quite and ignore any
unsaved changes
:10 Jump to line number 10

Visual mode Use it for Highlight the txt


or delete or copy or move
txt
v Use to highlight to special
char
Shift+V To highlight to line
Ctrl+V Highlight to Block
$PATH This variable has the Echo $PATH
locations for all commands
that exist in the system

Users & Groups


Id or id + username

/etc/login.defs
The file that exists id for
users
Super User 0 root
System User 1-200 Assigned statically to system process by
RedHat
System User 201-999 Dynamic range to any system account
created in the system
Regular User 1000 - 6000

/etc/passwd All users exist in this file


/etc/shadow All user's password file

su - Switch to the root user


su + username Switch to the specified user
Whoami To know the login user
/etc/sudoers Use it to specify sudo For user
permission to a specific user Username ALL=(ALL) ALL
Can open the file using any
editor or using the For Group
command %groupname ALL=(ALL) ALL

visudo All any host


All any user
All any command

Useradd --help To get all options


useradd + username To add a new user
usermod + username To modify existing user
usermod -L karim Lock karim user Usermod -l -e 2024-9-9 karim
Lock their account and make his
password will expire in this date
usermod -U karim To unlock Karim user
userdel + username To delete the existing user
home directory, exist
userdel -r ali Delete the user with his
home directory
useradd -md To create home dir for user useradd -md /home/Ali
…………. -c To add comments to user
…………. -s To define login shell useradd -s /bin/bash
---------- -g To add it to the private
group
---------- -G To add to the secondary usermod -aG Sales Karim
---------- -aG group if I am already in one
it will overwrite it add karim to group named sales
-aG to append the
secondary group
---------- -u To define user-id
All these options can add it
in one command
All option used with user
add can used it with
usermod

groupadd --help /etc/group all groups


groupadd -g 5000 + Create group and add it GID
groupname 5000
groupdel groupdel sales
groupmod groupmod -g 4000 Sales
groupmod -n sales sales2
rename sales group to sales2

Password Parameters
chage + options + user -m Mini days are required after the
password is changed to enable the
change password again
chage -m 0 ali
disable it

-M Maximum number of days after that


password will expire
-W Warning period through it that informs
you to change your password before
expiration
-l This is the day after the maximum
number of days is finished to change
your pass after that the account will
lock
‫هنا هيديلك سماحيه كام يوم بعد‬
‫مالوقت األساسي خلص‬

Example
chage -m 0 -M 40 -W 7 -l 14 Ali
Every option shows in /etc/shadow

Make your Account cannot usermod -s /sbin/nologin


login to the system without Ali
locking it and also used with
system users

Controlling access to files


Permissions applied on User owner (u) – group Other (o)
owner(g)
Read (r)
Write (w) Create or delete files in dir
Execute (x) Cd inside directory

chmod Used to change permissions


Symbolic method
chmod u + r file1 Add read permission to
user on file 1
chmod u – r file1 Remove read permission to
user on file 1
chmod u-wx,g-x,o-wx file1
chmod a=x Remove all old permissions
and add execute
permissions to all
chmod a=- file Remove all permissions
chmod -R o+rwx dir Chang directory and sub-
folders permissions

Numerical Method R = 4 w=2 x=1 R+w=6 , r+x=5 , rwx=7


Chmod 6 dir/file
chmod 47 dir/file
chmod 777 dir/file

chown Change user ownership for chown mohamed file1


File or dir
Chgrp Change group ownership for chgrp sales Dir1
file or dir
chown root: root file1 Change user and group
ownership for file1
chown :mohamed file1 Change group owner ship
chown -R Change dir and sub folder
mohamed:mohamed dir permissions

Special Permissions

Monitoring and Managing


Linux Processes
ps List all processes that are Result is once
running in your current
terminal
echo $$ Display PID for your current
Bash
ps aux List all processes in your
system
ps lax Show process by columns
ps -ef Show process by columns
Top The display process Used as a monitor it is like a task
dynamically updates manager
1. Top
2. Top -n 2
Define that after 2 iterations the top
terminates

1. Top -d 2
Will refresh every 2 seconds

There are many options in the top


pstree = ps fax Display process in tree
format
pstree -p Display PID of each process
pstree -p karim Display the running process
for Karim
pgrep -u mohamed -l List mohamed processes\
To run any command in At the end of the command
Background, write the write &
command
Jobs = ps j To display all running jobs in
the background
If I am in the background To return Job to the fg %1 (1 mean job number)
and need foreground
If I am in the foreground To return Job to Background bg %1 (1 mean job number)
and need
pidof Use to know PID for running pidof vim
service
Kill Use for many thing like the
kill process
kill -l To list all kill signal Signal 1 reload process
Signal 9 terminates like power off
Signal 15 default for terminate
kill + signal + pid Kill -9 2012
Kill -SIGKILL 2012
KILL 2012
Kill -9 3854 3652 4953
Killall + process name killall vim
pkill -u mohamed Kill all process for this user
pkill -SIGKILL -u mohamed

Process inherit their nice Value or priority from Parent


nice Used to start a process with nice vim & = nice -n vim
a user-defined priority start vim process with default priority
value = 10
nice -n 2 vim
renice Used to change priority for renice 19 9181
running process change priority value for PID 9181 to 19
Priority values from -20 to 19 -20 highest priority
19 lowest priority

Controlling Services and


Daemons
systemctl list-units -- List all active services in the
type=service system
systemctl list-units -- List all active and non-active
type=service --all services in the system
systemctl --failed -- List all failed services
type=service
Systemctl status + service To get service status systemctl status ssh.service
name
systemctl is-active Check if the service is active
sshd.service or not
systemctl is-enabled Check if the service is
sshd.service enabled or not
Systemctl is-failed Check if the service Is active
sshd.service or failed
systemctl start sshd.service Start service
systemctl restart service Restart service Stop service and start it again
systemctl reload service Reload service Service reloads their config file without
any stop
systemctl stop service Stop service
systemctl list-dependencies List all service that sshd
sshd.service service needs to run
correctly
systemctl mask
sshd.service
systemctl unmask
sshd.services
systemctl enable
sshd.service
systemctl disable Configure service not start
sshd.service at startup

Configure and secure SSH


Public keyes stored in /etc/ssh
[email protected] hostname Run command on the
remote device without a
login
Password Base Every time you need to log
Authentication in to the server will ask you
for the user and pass
Ssh key-based Generate key-pair or public
authentication and private keys for login
ssh -V To show the version for ssh
Ssh -v user@localhost Option used to debug
ssh -v connection issue
user@remotehostname
How to configure SSH key-Based Authentication: -
1- Generate public and private keys on the client
- ssh-keygen
will generate public and private keys in your home directory
under .ssh/ and their name
id_rsa (private key) id_rsa.pub
don’t share private key with anyone
make permission to private 600
make permission to public 644
2- Copy the public key to the server that you need to log on to it
- ssh-copy-id -i .ssh/id_rsa.pub user@remoteserver
3- Try to ssh to the server (it will authenticate using the key)

Ssh main configuration file in /etc/ssh/sshd_config


Recommended to make
PermitRootLogin no #to prevent anyone from accessing your machine
using the root
PasswordAuthentication no # secure way to login using key-pair
authentication

After that, you must reload the ssh configuration file

Sysytemctl reload sshd.service


Installing and Updating Software Packages (RHEL, Ubuntu)
On a Debian-based system: -
Updating Package Lists
 Update the package index:
sudo apt update
Upgrading Installed Packages
 Upgrade all installed packages to the latest version:
sudo apt upgrade
 Upgrade all packages, including potentially breaking changes:
sudo apt full-upgrade
 Upgrade a specific package:
sudo apt upgrade <package_name>

Installing Packages
 Install a specific package:
sudo apt install <package_name>
 Install multiple packages at once:
sudo apt install <package1> <package2>

Removing Packages
 Remove a specific package (but leave its configuration files):
sudo apt remove <package_name>
 Remove a package and its configuration files:
sudo apt purge <package_name>
 Remove unused packages automatically (cleanup after
installing/removing software):
sudo apt autoremove

Searching for Packages


 Search for a package by name or description:
apt search <package_name_or_keyword>
 Display detailed information about a specific package:
apt show <package_name>

Fix Broken Packages


 Fix dependencies and broken packages:
sudo apt --fix-broken install

Cleaning Up
 Remove downloaded package files:
sudo apt clean
 Remove only old package versions from the cache:
sudo apt autoclean
2. apt-get
apt-get is an older package management tool, which is still available and
widely used for scripting and advanced purposes.
Updating Package Lists
 Update the package list:
sudo apt-get update
Upgrading Packages
 Upgrade installed packages:
sudo apt-get upgrade
 Upgrade and handle package dependencies:
sudo apt-get dist-upgrade
Installing and Removing Packages
 Install a package:
sudo apt-get install <package_name>
 Remove a package:
sudo apt-get remove <package_name>
 Purge a package (removes package and configuration files):
sudo apt-get purge <package_name>
 Automatically remove unused packages:
sudo apt-get autoremove

3. dpkg (Debian Package Manager)


dpkg is a lower-level tool used to install, configure, and remove .deb
packages. It doesn’t manage dependencies as apt does.
Installing Packages
 Install a local .deb package:
sudo dpkg -i <package_name.deb>
Removing Packages
 Remove a package (keeps configuration files):
sudo dpkg -r <package_name>
 Purge a package (removes configuration files):
sudo dpkg -P <package_name>
Querying Installed Packages
 List all installed packages:
dpkg -l
 Check if a specific package is installed:
dpkg -l | grep <package_name>
 Show detailed information about an installed package:
dpkg -s <package_name>
4. apt-cache
apt-cache is used to query the local package cache and view package
metadata.
Searching and Querying Packages
 Search for a package:
apt-cache search <package_name>
 Show package details:
apt-cache show <package_name>
 Check package dependencies:
apt-cache depends <package_name>
 Show reverse dependencies (what packages depend on this one):
apt-cache rdepends <package_name>
5. Advanced Package Management Commands
 Hold a package at its current version (prevent it from being upgraded):
sudo apt-mark hold <package_name>
 Unhold a package (allow it to be upgraded):
sudo apt-mark unhold <package_name>
6. Upgrading the Entire System
 Upgrade from one Debian release to another:
sudo apt-get dist-upgrade
7. Sources and Repository Management
 List available software repositories:
cat /etc/apt/sources.list
 Add a new repository (example for adding a PPA):
sudo add-apt-repository ppa:<ppa_name>
sudo apt update
. list all installed packages
apt list --installed

1. dnf (Dandified Yum)


Starting from RHEL 8, dnf replaces yum. It is used for managing packages,
handling dependencies, and maintaining repositories.
Updating Package Lists
 Update the package index:
sudo dnf check-update
Upgrading Installed Packages
 Upgrade all installed packages to the latest version:
sudo dnf upgrade
 Upgrade a specific package:
sudo dnf upgrade <package_name>

Installing Packages
 Install a specific package:
sudo dnf install <package_name>
 Install multiple packages at once:
sudo dnf install <package1> <package2>
Removing Packages
 Remove a package:
sudo dnf remove <package_name>
 Remove a package and its dependencies (those not required by other
packages):
sudo dnf autoremove <package_name>
Searching for Packages
 Search for a package by name or description:
sudo dnf search <package_name_or_keyword>
 List information about a specific package:
sudo dnf info <package_name>
Listing Installed Packages
 List all installed packages:
sudo dnf list installed
 List all available packages:
sudo dnf list available
Group Management
 Install a package group (e.g., Development Tools):
sudo dnf group install "Development Tools"
 List available package groups:
sudo dnf group list
 Remove a package group:
sudo dnf group remove "Development Tools"
Cleaning Up
 Remove cached packages and metadata:
sudo dnf clean all

2. yum (Yellowdog Updater Modified)


For older versions of RHEL (like RHEL 7 and earlier), yum is the primary tool
for package management.
Updating Package Lists
 Update the package index:
sudo yum check-update
Upgrading Installed Packages
 Upgrade all installed packages:
sudo yum upgrade
Installing Packages
 Install a package:
sudo yum install <package_name>
Removing Packages
 Remove a package:
sudo yum remove <package_name>
Searching for Packages
 Search for a package:
sudo yum search <package_name_or_keyword>
Listing Installed Packages
 List installed packages:
sudo yum list installed
Cleaning Up
 Clear out the cache:
sudo yum clean all

3. rpm (Red Hat Package Manager)


rpm is a low-level package management tool used to install, query, and
remove .rpm packages. Unlike dnf or yum, it doesn't resolve dependencies
automatically.
Installing a Local .rpm File
 Install a package from an .rpm file:
sudo rpm -i <package_name.rpm>
 Upgrade an installed package with an .rpm file:
sudo rpm -U <package_name.rpm>
Removing a Package
 Remove a package:
sudo rpm -e <package_name>
Querying Installed Packages
 List all installed packages:
rpm -qa
 Check if a specific package is installed:
rpm -q <package_name>

 Get detailed information about an installed package:


rpm -qi <package_name>
 List all files installed by a package:
rpm -ql <package_name>
 Check the package that provides a file:
rpm -qf <file_name>

4. Repository Management
Enable or Disable Repositories
 Enable a repository temporarily:
sudo dnf --enablerepo=<repo_name> install <package_name>
 Disable a repository temporarily:
sudo dnf --disablerepo=<repo_name> install <package_name>
 List enabled repositories:
sudo dnf repolist
 List all repositories (enabled and disabled):
sudo dnf repolist all
Adding and Removing Repositories
 Add a repository by creating a .repo file in /etc/yum.repos.d/ with
appropriate configuration. For example:
sudo nano /etc/yum.repos.d/example.repo
Inside the file:
ini
[example]
name=Example Repository
baseurl=http://example.com/repo
enabled=1
gpgcheck=1
gpgkey=http://example.com/repo/RPM-GPG-KEY-example
 Remove a repository by deleting its .repo file:
sudo rm /etc/yum.repos.d/example.repo

5. Package Management via Subscription Manager


RHEL uses subscription manager to manage Red Hat subscriptions and
entitlements.
Register System to Red Hat
 Register system:
sudo subscription-manager register --username=<your_username> --
password=<your_password>
Attach Subscriptions
 Attach a subscription to the system:
sudo subscription-manager attach --auto
Check Subscription Status
 Check subscription status:
sudo subscription-manager status

6. Managing Services with systemctl


While not directly related to package management, managing software
services is often a critical aspect.
Starting, Stopping, and Managing Services
 Start a service:
sudo systemctl start <service_name>
 Stop a service:
sudo systemctl stop <service_name>
 Enable a service to start at boot:
sudo systemctl enable <service_name>
 Disable a service:
sudo systemctl disable <service_name>
 Check service status:
sudo systemctl status <service_name>

Basic Network Configuration Commands


These commands help you view or configure basic network settings.
View Network Interface Information
 ifconfig (Older tool, replaced by ip in most distributions):
ifconfig
 ip a (Preferred command for viewing IP addresses and interfaces):
ip address show
ip address show eth0
 Show active network interfaces:
ip link show

Assign or Modify IP Address


 Assign an IP address to an interface:
sudo ip addr add 192.168.1.100/24 dev eth0
 Delete an IP address from an interface:
sudo ip addr del 192.168.1.100/24 dev eth0

Enable or Disable Network Interfaces


 Bring an interface up:
sudo ip link set eth0 up
 Bring an interface down:
sudo ip link set eth0 down

Configure Routing
 Display routing table:
ip route show = ip route = route (apt install net-tools)
 Add a route:
sudo ip route add 192.168.1.0/24 via 192.168.1.1
 Delete a route:
sudo ip route del 192.168.1.0/24

Network Manager Command


 nmcli (Network Manager CLI tool):
o Display the status of network devices:
nmcli device status
o Connect to a network:
nmcli device wifi connect SSID password PASSWORD

2. Testing and Troubleshooting Network


These commands help diagnose network issues.
Ping
 Ping a remote host to test connectivity:
ping google.com
 Ping with a specified count:
ping -c 4 google.com
Traceroute
 Trace the route packets take to a destination:
traceroute google.com
 If traceroute is not installed:
sudo apt install traceroute
MTR
 Real-time network diagnostic tool (combines ping and traceroute):
mtr google.com

 If mtr is not installed:


sudo apt install mtr

Netcat (nc)
 Check if a specific port is open:
nc -zv google.com 80
 Start a simple TCP server:
nc -l 1234
 Connect to a TCP server:
nc 192.168.1.1 1234
Telnet
 Test a TCP connection to a port:
telnet google.com 80

Dig / nslookup
 Query DNS information:
o dig (more powerful, preferred tool):
dig google.com
o nslookup (older tool):
nslookup google.com
 Get DNS records (e.g., MX records):
dig google.com MX

Host
 Find the IP address for a domain name:
host google.com
Nmap
 Network scanning and discovery:
o Basic scan of open ports:
sudo nmap google.com
o Scan a specific port range:
sudo nmap -p 1-100 google.com
o If nmap is not installed:
sudo apt install nmap

3. Monitoring and Analyzing Network


These commands allow you to monitor and analyze network traffic.
Netstat
 Display network connections and listening ports:
netstat -tuln
 Display open ports and established connections:
sudo netstat -anp
 If netstat is not installed:
sudo apt install net-tools

SS (Socket Statistics)
 Display socket and network connection information (replacement for
netstat):
ss -tuln
 -t: Shows TCP connections.
 -u: Shows UDP connections.
 -l: Shows only listening sockets.
 -n: Shows numerical addresses instead of resolving hostnames.

To include the process name (PID):


ss -tulnp
 -p: Displays the process using the socket.

 Show all established TCP connections:


ss -t

Using lsof (List Open Files)


lsof is a versatile tool that can list open files, including network sockets.
Command to show listening TCP and UDP ports:
sudo lsof -i -P -n | grep LISTEN
 -i: Selects files with an Internet address.
 -P: Shows the port number instead of resolving the port name.
 -n: Shows numerical addresses instead of resolving hostnames.
 grep LISTEN: Filters the output to show only listening ports.
TCPdump
 Capture network packets on an interface:
sudo tcpdump -i eth0
 Capture packets on a specific port (e.g., HTTP):
sudo tcpdump -i eth0 port 80
Iperf3
 Measure network bandwidth between two hosts (run as client-server):
o Run server on one machine:
iperf3 -s
o Run client on another machine to test speed:
iperf3 -c <server_IP_address>
 If iperf3 is not installed:
sudo apt install iperf3

Wireshark
 Graphical network protocol analyzer (GUI tool):
wireshark
 If wireshark is not installed:
sudo apt install wireshark
4. Network Interface Configuration
These commands help configure interfaces manually.

DHCP Management
 Renew a DHCP lease:
sudo dhclient eth0
Modify Hostname
 View current hostname:
hostnamectl
 Change hostname:
sudo hostnamectl set-hostname new_hostname
Modify /etc/hosts
 Edit /etc/hosts to map domain names to IP addresses:
sudo nano /etc/hosts

5. Firewall Configuration
These commands manage firewall rules.

UFW (Uncomplicated Firewall)


 Check UFW status:
sudo ufw status
 Allow incoming traffic on a specific port (e.g., 80):
sudo ufw allow 80
 Deny incoming traffic on a specific port:
sudo ufw deny 22
 Enable UFW:
sudo ufw enable
 Disable UFW:
sudo ufw disable

Iptables
 List all current rules:
sudo iptables -L
 Add a rule to allow traffic on port 80:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
 Block traffic from a specific IP:
sudo iptables -A INPUT -s 192.168.1.100 -j DROP
 If iptables is not installed:
sudo apt install iptables

6. SSH (Secure Shell)


These commands help you manage remote connections.
Connecting to a Remote Server
 Connect to a server via SSH:
ssh username@remote_host
 Connect to a server with a specific port:
ssh -p 2222 username@remote_host

File Transfer via SCP


 Copy a file to a remote server:
scp /path/to/local/file username@remote_host:/path/to/remote/file
 Copy a file from a remote server:
scp username@remote_host:/path/to/remote/file /path/to/local/file

16.1. Network Configuration Files


Before delving into the interface configuration files, let us first itemize the
primary configuration files used in network configuration. Understanding the
role these files play in setting up the network stack can be helpful when
customizing a Red Hat Enterprise Linux system.
The primary network configuration files are as follows:
/etc/hosts
The main purpose of this file is to resolve host names that cannot be resolved
any other way. It can also be used to resolve host names on small networks
with no DNS server. Regardless of the type of network the computer is on,
this file should contain a line specifying the IP address of the loopback device
(127.0.0.1) as localhost.localdomain. For more information, refer to
the hosts(5) man page.
/etc/resolv.conf
This file specifies the IP addresses of DNS servers and the search domain.
Unless configured to do otherwise, the network initialization scripts populate
this file. For more information about this file, refer to the resolv.conf(5) man
page.
/etc/sysconfig/network
This file specifies routing and host information for all network interfaces. It is
used to contain directives which are to have global effect and not to be
interface specific. For more information about this file and the directives it
accepts, refer to Section 32.1.22, “/etc/sysconfig/network”.
/etc/sysconfig/network-scripts/ifcfg-<interface-name>
For each network interface, there is a corresponding interface configuration
script. Each of these files provide information specific to a particular network
interface. Refer to Section 16.2, “Interface Configuration Files” for more
information on this type of file and the directives it accepts.

You might also like