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

hostname and host configuration in linux

The document provides a comprehensive guide on managing hostnames and configurations in Linux, including setting the hostname, modifying the /etc/hosts file, and using commands like hostnamectl. It also covers text processing tools such as awk and sed, detailing their syntax and common usage examples for tasks like searching, replacing, and sorting text. Additionally, it introduces the vim text editor and the tar command for file archiving, emphasizing their functionalities and basic commands.

Uploaded by

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

hostname and host configuration in linux

The document provides a comprehensive guide on managing hostnames and configurations in Linux, including setting the hostname, modifying the /etc/hosts file, and using commands like hostnamectl. It also covers text processing tools such as awk and sed, detailing their syntax and common usage examples for tasks like searching, replacing, and sorting text. Additionally, it introduces the vim text editor and the tar command for file archiving, emphasizing their functionalities and basic commands.

Uploaded by

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

hostname and host configuration in linux Modify or add a line like this:

ChatGPT said: plaintext


Copy code
ChatGPT 127.0.1.1 new-hostname

In Linux, managing the hostname and host For systems with hostnamectl (like those
configuration involves setting up the running systemd), you can set the
system’s hostname and defining host hostname permanently with:
entries in the /etc/hosts file. Here’s a
guide to configuring both: bash
Copy code
sudo hostnamectl set-hostname new-
1. Setting the Hostname hostname

The hostname is the name of your 2. Configuring the Hosts File


computer on a network. To view, set, or
modify the hostname, you can use various The /etc/hosts file is a local file that
commands. maps hostnames to IP addresses, often
used to define shortnames or resolve hosts
Viewing the Current Hostname that may not be in DNS.
bash To edit the /etc/hosts file:
Copy code
hostname
bash
Copy code
Temporarily Changing the Hostname sudo nano /etc/hosts

To change the hostname temporarily (will Typical Format


reset after a reboot):
Each line in /etc/hosts follows this
bash
format:
Copy code
sudo hostname new-hostname
plaintext
Copy code
Permanently Changing the Hostname IP_address hostname aliases

To make the hostname change permanent, Example Entries


edit the /etc/hostname file:
Here are some common entries:
bash
Copy code plaintext
sudo nano /etc/hostname Copy code
127.0.0.1 localhost
Replace the existing hostname with your 127.0.1.1 new-hostname
new desired hostname, then save and exit. 192.168.1.10 server1.example.com
server1
Additionally, edit /etc/hosts to map the
After configuring these files, your system
hostname to 127.0.1.1:
will recognize the new hostname, and any
bash defined host entries will be available for
Copy code network name resolution.
sudo nano /etc/hosts
4o 3. Specify a Field Separator If the
file uses a delimiter other than
You said: whitespace (e.g., commas in CSV),
use the -F option:
awk command
bash
ChatGPT said: Copy code
awk -F, '{ print $1, $2 }'
ChatGPT filename.csv

The awk command in Linux is a powerful This treats commas as delimiters


text-processing tool used to analyze and and prints the first and second
manipulate text files. It’s especially useful columns.
for extracting and transforming data from
structured files (like CSVs or log files) 4. Print Lines Matching a Pattern
based on specific patterns or conditions.
bash
Here are some common ways to use awk: Copy code
awk '/pattern/ { print }'
Basic Syntax filename

bash This prints lines containing


Copy code "pattern". You can replace
awk 'pattern { action }' filename
"pattern" with any text or regular
expression.
 pattern specifies a condition to
match.
5. Perform Calculations
 { action } specifies what to do if
the pattern is matched. bash
Copy code
Common awk Examples awk '{ print $1, $2, $3, ($2
* $3) }' filename

1. Print All Lines in a File


If the file has columns of numbers,
bash this example prints the first three
Copy code columns and multiplies the 2nd and
awk '{ print }' filename 3rd columns.

This will print all lines in 6. Use if Statements


filename.
bash
2. Print Specific Columns Copy code
awk '{ if ($3 > 50) print
$1, $3 }' filename
bash
Copy code
awk '{ print $1, $3 }' This example prints the first and
filename third columns only if the value in
the third column is greater than 50.
This prints the 1st and 3rd columns
of each line in filename. $1 refers 7. Count Lines
to the first column, $2 to the
second, and so on. bash
Copy code
awk 'END { print NR }' The sed command in Linux, short for
filename
Stream Editor, is a powerful tool for
performing text transformations on files or
This counts and prints the total input streams. You can use sed to search,
number of lines in filename. NR is find and replace, insert, and delete lines in
a built-in variable that represents a file. Here’s a guide to using some of the
the current record number. most common sed operations.
8. Sum a Column
Basic Syntax
bash
Copy code bash
awk '{ sum += $2 } END { Copy code
print sum }' filename sed 'command' filename

This calculates the sum of values in Or, for multiple commands:


the second column.
bash
Copy code
9. Print Line Numbers sed -e 'command1' -e 'command2'
filename
bash
Copy code
awk '{ print NR, $0 }'
Common sed Commands
filename
1. Printing Lines
This adds line numbers to each line
of output, where NR represents the bash
Copy code
current line number. sed -n 'p' filename

Built-in Variables in awk This prints each line from the file.
By default, sed will print every
 NR: Current record (line) number. line, so the -n option suppresses
 NF: Number of fields in the current this behavior, allowing only
record. specified lines to be printed.
 $0: Entire line.
 $1, $2, etc.: Individual fields. 2. Substitute (Find and Replace)

awk is very versatile and can be used in bash


scripts or on the command line to Copy code
sed 's/old-text/new-text/'
efficiently handle text processing tasks. filename

4o This replaces the first occurrence


of "old-text" with "new-text" in
You said:
each line.
sed cpmmand
Global Replacement (All
ChatGPT said: Occurrences in Line):

ChatGPT bash
Copy code
sed 's/old-text/new-text/g'
filename
Replace in Place (Modify File 7. Change Text on a Specific Line
Directly):
bash
bash Copy code
Copy code sed '3c\This line has been
sed -i 's/old-text/new- changed' filename
text/g' filename
Replaces line 3 with "This line has
3. Replacing Only on Specific Lines been changed".

bash 8. Find and Replace Using Regular


Copy code
Expressions
sed '3s/old-text/new-text/'
filename
bash
Copy code
This replaces "old-text" with "new- sed 's/[0-9]/#/g' filename
text" only on line 3.
This replaces all digits with # in
4. Deleting Lines each line.
bash
Copy code
9. Replacing on Lines Matching a
sed '5d' filename Pattern

bash
Deletes line 5 in the file. Copy code
sed '/pattern/s/old-
Delete a Range of Lines: text/new-text/' filename

bash Finds lines containing "pattern"


Copy code
and replaces "old-text" with "new-
sed '2,4d' filename
text" only in those lines.
This deletes lines 2 through 4.
10. Print Specific Lines
5. Insert Text at a Specific Line bash
Copy code
bash sed -n '3p' filename
Copy code
sed '3i\This is a new line'
filename This prints only line 3. You can
also specify ranges, e.g., sed -n
Inserts "This is a new line" before '2,5p' filename to print lines 2
line 3. to 5.

6. Append Text After a Specific Combining sed Commands


Line
You can combine multiple commands with
bash -e:
Copy code
sed '3a\This is an appended bash
line' filename Copy code
sed -e 's/old/new/g' -e '5d'
Adds "This is an appended line" filename
after line 3.
Additional Options This sorts the file in reverse
alphabetical order.
 -i: Edits files in place (modifies
the original file). 3. Numerical Sort
 -n: Suppresses automatic printing.
bash
Copy code
sed is extremely powerful for text sort -n filename
processing and can be scripted to automate
various file-editing tasks. This sorts the file based on
numerical values. This is useful if
4o you’re working with numbers
instead of strings.
You said:
4. Sort by a Specific Column
sort
bash
ChatGPT said:
Copy code
sort -k2 filename
ChatGPT
This sorts the file by the second
The sort command in Linux is used to column. Change 2 to any other
arrange lines of text files in a specific column number as needed.
order (alphabetically, numerically, etc.).
It’s useful for organizing data, making 5. Human-Readable Sort
lists, or cleaning up files. Here’s a guide to
some common sort operations: bash
Copy code
sort -h filename
Basic Syntax
bash Sorts by human-readable numbers
Copy code (e.g., 1K, 2M), which is useful for
sort [options] filename sorting file sizes.

Common sort Options 6. Unique Sort (Remove Duplicates)

1. Alphabetical Sort (Default) bash


Copy code
sort -u filename
bash
Copy code
sort filename This sorts the file and removes
duplicate lines.
This sorts the file alphabetically by
default. 7. Sort by Month

2. Reverse Order bash


Copy code
sort -M filename
bash
Copy code
sort -r filename This sorts based on month names,
assuming the month is listed in the
text (e.g., Jan, Feb).
8. Sort in a Case-Insensitive Example Use Cases
Manner
 Sorting a List of Numbers:
bash
Copy code bash
sort -f filename Copy code
sort -n numbers.txt
Sorts the file in a case-insensitive
way (ignores uppercase/lowercase). Sorts a list of numbers in ascending
order.
9. Sort with a Custom Delimiter
 Sorting by Last Name:
bash
Copy code bash
sort -t ":" -k2 filename Copy code
sort -t " " -k2 names.txt
The -t option lets you specify a
delimiter (in this example, :). This Assuming the names are in "First
command sorts by the second field, Last" format, this command sorts
assuming fields are separated by by last name (second field).
colons.
 Sorting a File with Sizes:
10. Sort and Save to a File
bash
bash Copy code
Copy code sort -h sizes.txt
sort filename >
sorted_filename Useful for sorting files by sizes
written in human-readable form
Redirects the sorted output to a (e.g., 1K, 500M, 2G).
new file called sorted_filename.
The sort command is efficient and useful
11. Sort in Place for arranging data quickly, especially for
large files or structured text data.
bash
Copy code
sort -o filename filename 4o

This sorts the file in place, meaning You said:


it overwrites the original file with
the sorted result. vim
ChatGPT said:
Combining Options
ChatGPT
You can combine multiple options as
needed. For example: vim is a powerful, modal text editor in
Linux and Unix-based systems. It is
bash widely used for editing text, especially in
Copy code
sort -n -r filename programming and configuration files, due
to its speed and versatility. Here’s an
This will sort the file numerically in overview of basic and essential vim
reverse order. commands.
Opening a File  h, j, k, l: Move left, down, up, and
right (in Normal Mode).
bash  0: Move to the beginning of the
Copy code line.
vim filename
 $: Move to the end of the line.
 gg: Go to the start of the file.
This command opens the specified file in
 G: Go to the end of the file.
vim. If the file doesn’t exist, vim will
 :n: Go to line number n (e.g., :10
create it.
to go to line 10).
vim Modes Editing Text
vim has several modes, with the most
 i: Enter Insert Mode before the
commonly used being: cursor.
 a: Enter Insert Mode after the
 Normal Mode: The default mode, cursor.
used for navigation and command
 o: Open a new line below the
execution. current line and enter Insert Mode.
 Insert Mode: Allows you to insert
 O: Open a new line above the
text. Entered by pressing i. current line and enter Insert Mode.
 Visual Mode: Used for selecting
 x: Delete the character under the
text. Entered by pressing v. cursor.
 Command Mode: Allows you to
 dd: Delete the entire line.
execute commands (e.g., save,
 yy: Yank (copy) the current line.
quit). Accessed by typing : from
 p: Paste the yanked (copied) text
Normal Mode.
below the cursor.
 u: Undo the last change.
Basic Commands  Ctrl + r: Redo the last undone
change.
Switching Between Modes
Visual Mode (Selecting Text)
 Enter Insert Mode: Press i to
start typing text at the cursor's  v: Start Visual Mode for character-
position. wise selection.
 Return to Normal Mode: Press
 V: Start Visual Mode for line-wise
Esc to return to Normal Mode from
selection.
Insert Mode.
 Ctrl + v: Start Visual Block
 Enter Visual Mode: Press v to Mode (useful for column
select text. selections).
 Once selected, use commands like
Saving and Exiting d (delete) or y (copy) on the
selection.
 Save changes: :w (write changes to
the file) Search and Replace
 Quit: :q (quit if no unsaved
changes)  Search: /pattern (e.g., /error to
 Save and quit: :wq or :x search for "error")
 Quit without saving: :q! o Press n to go to the next
match.
Basic Navigation
o
Press N to go to the The tar command in Linux is used to
previous match. create, extract, and manipulate archive
 Find and Replace in Entire File: files. It's commonly used to combine
multiple files into a single archive, usually
vim with compression, making it easier to store
Copy code and transfer data.
:%s/old-text/new-text/g

This replaces all occurrences of Basic Syntax


"old-text" with "new-text" in the
bash
entire file. Copy code
tar [options] archive-name.tar
 Find and Replace in Current file(s)/directory
Line:
Common tar Options
vim
Copy code
:s/old-text/new-text/g Option Description
-c Create a new archive.
Indentation -x Extract files from an archive.
-v
Verbose output (lists files being
 >>: Indent the current line.
processed).
 <<: Unindent the current line.
Specifies the filename of the
 Indent Multiple Lines: Select -f
archive.
lines in Visual Mode and use > or
<. Compresses the archive with
-z
gzip.
Useful Tips Compresses the archive with
-j
bzip2.
 View line numbers: :set number -J Compresses the archive with xz.
 Disable line numbers: :set -t Lists the contents of an archive.
nonumber
 Syntax highlighting: :syntax on
 Undo changes: Press u multiple Common tar Examples
times to undo multiple steps.
1. Create an Archive
vim has a learning curve, but it’s
bash
extremely powerful once you get the hang Copy code
of it, making text editing very efficient. tar -cvf archive-name.tar
directory/
4o
This creates an uncompressed tar
You said: archive of the specified directory.

tar 2. Create a Gzipped Archive


ChatGPT said: bash
Copy code
ChatGPT tar -cvzf archive-
name.tar.gz directory/
This creates a gzip-compressed This extracts a bzip2-compressed
archive (.tar.gz) of the specified archive.
directory.
8. Extract an Xz-Compressed
3. Create a Bzipped Archive Archive

bash bash
Copy code Copy code
tar -cvjf archive- tar -xvJf archive-
name.tar.bz2 directory/ name.tar.xz

This creates a bzip2-compressed This extracts an xz-compressed


archive (.tar.bz2) of the specified archive.
directory.
9. List Contents of an Archive
4. Create an Xz-Compressed
Archive bash
Copy code
tar -tvf archive-name.tar
bash
Copy code
tar -cvJf archive- This displays the contents of an
name.tar.xz directory/ archive without extracting it.

This creates an xz-compressed 10. Extract Specific Files from an


archive (.tar.xz) of the specified Archive
directory.
bash
5. Extract an Archive Copy code
tar -xvf archive-name.tar
file1 file2
bash
Copy code
tar -xvf archive-name.tar This extracts only file1 and
file2 from the archive.
This extracts an uncompressed tar
archive. 11. Add Files to an Existing Archive
(Without Compression)
6. Extract a Gzipped Archive
bash
bash Copy code
Copy code tar -rvf archive-name.tar
tar -xvzf archive- newfile
name.tar.gz
This appends newfile to an
This extracts a gzip-compressed existing uncompressed archive.
archive.
Practical Tips
7. Extract a Bzipped Archive
 Keep original files: When creating
bash an archive, tar does not delete the
Copy code
tar -xvjf archive-
original files.
name.tar.bz2  Compressed formats: Use
.tar.gz, .tar.bz2, or .tar.xz
for compression based on needs to the end of the file if it
(gzip is faster; xz offers higher already exists.
compression). o Example:

The tar command is versatile and often bash


Copy code
used in Linux for packaging files, backups, echo "New entry" >>
and transferring data efficiently. filelist.txt

4o This appends "New entry"


to filelist.txt without
You said:
overwriting the existing
content.
output redirection

ChatGPT said:
3. 2> (Redirect Standard Error)
o Syntax: command 2>
ChatGPT filename
o Redirects the standard error
In Linux, output redirection is a way to (error messages) of a
direct the output of a command to a file or command to a file.
another command, rather than displaying it o Example:
on the screen. Here are the basics and
bash
some common uses for output redirection. Copy code
ls
Basic Redirection Operators /nonexistent_directory
2> error.log
1. > (Redirect Output)
o Syntax: command >
If ls fails because the
filename directory doesn’t exist, the
o Redirects the standard error message will be saved
output of a command to a to error.log.
file. If the file already
exists, it will be 4. 2>> (Append Standard Error)
overwritten. o Syntax: command 2>>
o Example: filename
o Redirects standard error and
bash appends it to the file if it
Copy code exists.
ls > filelist.txt o Example:

This saves the output of ls bash


to filelist.txt, Copy code
overwriting if it already ls
/nonexistent_directory
exists. 2>> error.log

2. >> (Append Output) This appends any error


o Syntax: command >> messages to error.log
filename
without overwriting the file.
o Redirects the standard
output to a file, appending
5. &> (Redirect Both Standard
Output and Standard Error)
o Syntax: command &> This sends the output of ls as input
filename to grep, which then filters the
o Redirects both standard results for .txt files.
output and standard error to
a file. Examples of Output Redirection
o Example:
1. Save Command Output to a File
bash
Copy code
ls bash
/nonexistent_directory Copy code
&> output.log df -h > disk_usage.txt

This redirects both output This saves the output of df -h


and error messages to (disk space usage) to
output.log. disk_usage.txt.

6. &>> (Append Both Standard 2. Redirect Error Messages


Output and Standard Error) Separately
o Syntax: command &>>
filename bash
Copy code
o Appends both standard find / -name "myfile" >
output and standard error to results.txt 2> errors.txt
a file.
o Example: This command saves the results of
find to results.txt and any
bash
Copy code
errors (like permission errors) to
ls errors.txt.
/nonexistent_directory
&>> output.log 3. Redirect Both Output and Error
to the Same File
This appends both output
and error messages to bash
output.log without Copy code
find / -name "myfile" &>
overwriting it. all_output.txt

Using | for Piping Output This command saves both the


output and any errors to
The pipe operator (|) redirects the output all_output.txt.
of one command as the input to another
command. Using /dev/null for Output
 Syntax: command1 | command2
Suppression
 Example:
/dev/null is a special file that discards all
bash data written to it. This is useful if you want
Copy code to suppress output or error messages.
ls | grep ".txt"
 Suppress Output:

bash
Copy code
command > /dev/null bash
Copy code
env
 Suppress Errors:

bash These commands list all


Copy code environment variables currently set
command 2> /dev/null in the shell session.

 Suppress Both Output and 2. Display a Specific Variable


Errors:
bash
bash Copy code
Copy code echo $VARIABLE_NAME
command &> /dev/null
For example:
Output redirection is essential in Linux for
managing and controlling output, enabling bash
flexible file management, logging, and Copy code
scripting. echo $HOME

4o This shows the home directory of


the current user.
You said:
Common Environment Variables
exploring environment variables and path
 HOME: Path to the user’s home
ChatGPT said:
directory (e.g., /home/username).
 USER: The username of the logged-
ChatGPT in user.
 SHELL: The default shell for the
In Linux, environment variables are user (e.g., /bin/bash).
dynamic variables that affect the processes  PATH: Directories that the shell
or programs on a system. These variables searches for executable files.
hold values like the paths of executable
 PWD: The current working
files, default editors, usernames, and more,
directory.
influencing the environment for user
 LANG: The system’s language
sessions and system operations. One of the
setting.
most crucial environment variables is the
 EDITOR: The default text editor.
PATH, which specifies the directories
where executable files are located.
Modifying Environment Variables
Viewing Environment Variables
1. Setting a Variable Temporarily
1. Display All Environment bash
Variables Copy code
VARIABLE_NAME=value
bash
Copy code For example:
printenv
bash
or Copy code
MY_VAR="Hello, World!"
echo $MY_VAR bash
Copy code
export PATH=$PATH:/new/directory
This sets MY_VAR temporarily in the
current session.
This adds /new/directory to the PATH for
2. Setting a Variable for Child the current session only.
Processes (Exporting)
Adding a Directory to PATH
bash Permanently
Copy code
export VARIABLE_NAME=value 1. Open the ~/.bashrc or
~/.profile file in a text editor:
For example:
bash
bash Copy code
Copy code nano ~/.bashrc
export MY_VAR="Hello,
World!"
2. Add the following line:
export makes the variable bash
available to child processes Copy code
spawned from the current shell. export
PATH=$PATH:/new/directory
3. Setting Variables Permanently
o Add export 3. Save the file, then apply changes:
VARIABLE_NAME=value to
bash
shell configuration files Copy code
such as ~/.bashrc, source ~/.bashrc
~/.bash_profile, or
/etc/profile (for system- Practical Examples
wide variables).
o After editing, apply 1. Setting a Default Editor
changes using:
bash
bash Copy code
Copy code export EDITOR=nano
source ~/.bashrc
This sets nano as the default text
The PATH Variable editor for the session. Adding this
to ~/.bashrc makes it permanent.
The PATH variable is a list of directories
separated by colons (:) where the shell 2. Adding a Custom Script
looks for executable files. Directory to PATH If you have a
directory for custom scripts (e.g.,
Viewing the PATH Variable ~/my_scripts):

bash bash
Copy code Copy code
echo $PATH export
PATH=$PATH:~/my_scripts
Adding a Directory to PATH
Temporarily
Now you can run any executable Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
script from ~/my_scripts directly Release: 20.04
from the command line. Codename: focal

3. Using Environment Variables in b. Checking /etc/os-release or


Scripts Environment variables can /etc/*release file
be used within scripts to make
them more flexible. For instance: Most Linux distributions have the
/etc/os-release file, which contains
bash
Copy code details about the operating system.
#!/bin/bash
echo "Welcome, $USER!" bash
echo "Your home directory is Copy code
$HOME" cat /etc/os-release

Environment variables are essential for Example output:


configuring the Linux environment to meet
specific requirements, allowing you to makefile
Copy code
control and optimize how programs NAME="Ubuntu"
execute. VERSION="20.04.3 LTS (Focal
Fossa)"
4o ID=ubuntu
ID_LIKE=debian
VERSION_ID="20.04"
You said:

find distro and kernel information Alternatively, you can use:

ChatGPT said:
bash
Copy code
cat /etc/*release
ChatGPT
c. Using hostnamectl (if available)
You can find your Linux distribution and
kernel version information using a few
The hostnamectl command shows OS
simple commands:
and kernel information, along with
hostname details.
1. Find Distribution Information
bash
a. Using lsb_release (if available) Copy code
hostnamectl
The lsb_release command provides
information about the distribution and Output example:
version.
yaml
Copy code
bash
Operating System: Ubuntu
Copy code
20.04.3 LTS
lsb_release -a
Kernel: Linux 5.4.0-
89-generic
Output might look like this: Architecture: x86-64

yaml 2. Find Kernel Version Information


Copy code
a. Using uname to find details on CPU, memory, disk
usage, and more.
The uname command provides kernel-
related information. 1. CPU Information
bash a. Using lscpu
Copy code
uname -r
The lscpu command provides detailed
Output example: information about the CPU architecture.

Copy code bash


5.4.0-89-generic Copy code
lscpu

b. Detailed Kernel Information Example output includes details such as


CPU model, cores, threads, and
For more detailed kernel information, use:
architecture.
bash
Copy code b. Checking /proc/cpuinfo
uname -a
You can also view CPU information by
Output might include the kernel version, examining the /proc/cpuinfo file.
hostname, architecture, and more:
bash
graphql Copy code
Copy code cat /proc/cpuinfo
Linux hostname 5.4.0-89-generic
#100-Ubuntu SMP Mon Aug 23 This lists details about each core, such as
10:00:00 UTC 2021 x86_64 x86_64
x86_64 GNU/Linux
model name, speed, and cache size.

Summary of Commands 2. Memory Information

 Distribution Information: a. Using free


lsb_release -a, cat /etc/os-
release, hostnamectl
The free command shows total, used, and
 Kernel Information: uname -r available memory (RAM).
(or uname -a for more details) bash
Copy code
4o free -h

You said: The -h option displays the values in a


human-readable format (e.g., GB, MB).
find system hardware and disk information
b. Using /proc/meminfo
ChatGPT said:

ChatGPT For detailed memory statistics, you can


check the /proc/meminfo file.
In Linux, you can gather system hardware
bash
and disk information using a variety of Copy code
commands. Here’s a comprehensive guide
cat /proc/meminfo The -s flag shows only the total size, and
the -h flag displays it in a human-readable
This provides information on total and free format.
memory, as well as buffer, cached, and
swap memory. 4. Hardware Information
3. Disk Information a. Using lshw

a. Using df The lshw (list hardware) command


provides detailed information about the
The df command shows disk space usage hardware. You might need root
on all mounted filesystems. permissions to get all details.
bash bash
Copy code Copy code
df -h sudo lshw

The -h option makes it human-readable, For a summary, you can use:


showing values in GB and MB.
bash
b. Using lsblk Copy code
sudo lshw -short
The lsblk (list block devices) command
provides an overview of all storage b. Using lspci
devices, their partitions, and their mount
points. The lspci command lists all PCI
(Peripheral Component Interconnect)
bash devices, including network and graphics
Copy code cards.
lsblk
bash
This is especially useful for viewing disk Copy code
partitions and their sizes. lspci

c. Using fdisk c. Using lsusb

The fdisk command can display partition The lsusb command lists all USB devices
tables for specified devices. You may need connected to the system.
root permissions.
bash
Copy code
bash lsusb
Copy code
sudo fdisk -l
d. Using dmidecode
d. Using du for Directory Disk Usage
The dmidecode command displays
The du command shows disk usage of hardware information retrieved from the
directories and files. system’s DMI (Desktop Management
Interface) table, including system
bash manufacturer, model, and BIOS version.
Copy code
du -sh /path/to/directory bash
Copy code
sudo dmidecode

5. System Summary with inxi

If you have the inxi tool installed, it


provides a quick summary of CPU,
memory, disk, network, and other
hardware information.

bash
Copy code
inxi -Fxz

This gives a comprehensive overview of


system hardware in a readable format. If
inxi isn’t installed, you can usually add it
with:

bash
Copy code
sudo apt install inxi # For
Debian/Ubuntu
sudo yum install inxi # For Red
Hat/CentOS
sudo dnf install inxi # For
Fedora

Summary of Commands

Information Command
lscpu, cat
CPU /proc/cpuinfo
free -h, cat
Memory /proc/meminfo
df -h, lsblk, sudo fdisk
Disk Space -l
Directory du -sh
Usage /path/to/directory
sudo lshw, lspci, lsusb,
Hardware sudo dmidecode
System
inxi -Fxz (if available)
Summary

These commands provide a solid


foundation for exploring hardware and
disk information on a Linux system.

You might also like