Familiarization of basic Linux Commands- history, uname, dmesg
history command
What is the history
Command?
- Purpose: Keeps track of the commands you've executed in the shell session.
- Benefit: Saves time and reduces errors by allowing you to reuse or modify previous commands.
- Default Behavior: Displays a numbered list of the last 500 commands you’ve executed.
history
: Shows the complete list of previously entered commands.[number]
: Limits the output to the last N commands.Viewing Your Command History
1. Display All Commands
To see all the commands saved in your current session:
history
2. Display a Specific Number of Commands
To display only the last N commands:
history 10
Re-Executing Commands
1. Using Command Numbers
Each command in the history list is assigned a number. To re-execute a command by its number, use:
!<number>
2. Re-Executing the Most Recent Command
To quickly repeat the last command:
!!
3. Re-Executing Commands Starting with a Specific String
To run the last command that starts with a specific word or character sequence, use:
!<string>
ls
.Searching Through History
1. Interactive Search
Press Ctrl + r
to perform an interactive reverse search through your history.
- Start typing part of a command:
2. Using grep
to Filter History
To find specific commands in your history, combine history
with the grep
command:
history | grep <keyword>
history | grep mkdir
mkdir
commands you’ve used.Clearing History
1. Clear the Entire History
To erase all saved commands:
history -c
To delete a specific command from history, use its number:
history -d <number>
Example:
Managing the History File
1. Persistent History
Your command history is saved in a file called .bash_history
(for Bash users). You can view or edit it:
cat ~/.bash_history
2. Save the Current Session to the History File
To immediately save your current session’s commands to the history file:
history -w
Customizing History Behavior
You can configure how history works by modifying environment variables.
Set the Maximum Number of Saved Commands
Conclusion
The history
command is a simple but essential tool for Linux users. By learning how to view, search, and reuse commands, you can work more efficiently and avoid unnecessary typing. Practice using it in your terminal to get comfortable with managing your command history!
uname command
uname
command is a simple yet essential tool in Linux that provides basic information about your operating system and hardware. Think of it as your system's "business card," offering key details about the kernel, architecture, and moreWhat is the uname
Command?
- Purpose: Displays information about the system your Linux is running on.
- Default Behavior: Shows the operating system name when run without options.
Using the uname
Command
1. Display the Operating System Name
When you run uname
with no options, it shows the name of the operating system:
uname
Example output
Linux
Options for Detailed System Information
The real power of uname
lies in its options, which allow you to view specific details about your system. Here are the most commonly used options:
1. -a
(All Information)
Displays all available information about the system in one go:
uname -a
Example output
Linux myhostname 5.15.0-76-generic #83~20.04.1-Ubuntu SMP Thu Jun 15 15:25:17 UTC 2023 x86_64 GNU/Linux
Linux
: The operating system name.myhostname
: The system's hostname.5.15.0-76-generic
: The kernel version.#83~20.04.1-Ubuntu
: The build number.x86_64
: The system architecture (64-bit in this case).GNU/Linux
: The underlying OS details.
2. -s
(Kernel Name)
Displays the kernel name, which is the default behavior of uname
:
3. -n (Node Name)
Displays the system’s hostname:
4. -r
(Kernel Release)
Shows the kernel release version. This is useful to know which kernel version your Linux is running on:
uname -r5. -v
(Kernel Version)
Displays the kernel version and build date:
uname -v
6. -m
(Machine Architecture)
Displays the system's hardware architecture (e.g., 32-bit or 64-bit):
uname -m
7. -p
(Processor Type)
Shows the processor type, if available:
uname -p
8. -i
(Hardware Platform)
Displays the hardware platform:
uname -i
9. -o
(Operating System)
Shows the name of the operating system:
uname -o
Real-World Use Cases
- Troubleshooting: Helps identify kernel versions for compatibility with software or drivers.
- System Checks: Useful when documenting system configurations or writing scripts.
- Performance Optimization: Identifying hardware architecture can guide installation of the right software version (e.g., 64-bit vs. 32-bit).
Quick Recap of Common Commands
Command | Description |
---|---|
uname | Displays the kernel name (default). |
uname -a | Displays all system information. |
uname -r | Displays the kernel release version. |
uname -m | Displays the hardware architecture. |
uname -n | Displays the system hostname. |
Conclusion
The uname
command is a fundamental tool for quickly checking your system’s identity. As a beginner, understanding these options will give you confidence in exploring and troubleshooting your Linux environment. Practice using uname
with different options to familiarize yourself with your system's details!
dmesg command
The dmesg
command in Linux is an essential tool that displays messages from the kernel ring buffer. Think of the kernel ring buffer as a diary where the Linux kernel logs events, such as system startup, hardware initialization, and errors.
The dmesg
command allows you to view and analyze these messages, making it invaluable for troubleshooting hardware, drivers, or system issues.
What is the dmesg
Command?
- Purpose: Displays system messages generated by the kernel.
- Usage: Helps diagnose hardware and software issues, especially during boot or runtime.
When to Use the dmesg
Command
- To check what happened during system boot.
- To debug issues related to hardware like USB devices, disks, or network interfaces.
- To troubleshoot driver problems or kernel errors.
How Does the dmesg
Command Work?
When Linux boots, the kernel starts logging system events into a memory buffer. This log includes messages about:
- Detected hardware (e.g., CPU, RAM, disks, network cards).
- Driver initialization.
- Kernel errors or warnings.
- Hardware failures.
The dmesg
command reads and displays these messages, letting you inspect what the kernel has been doing.
Using the dmesg
Command
1. Display All Kernel Messages
To view all messages, simply type:
dmesg
This command outputs a long list of messages. Each message has:
- Timestamps: The time since the system started, in seconds or milliseconds.
- Details: The specific event or issue logged by the kernel.
Example output:
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Linux version 5.15.0-76-generic ...
[ 1.234567] usb 1-1: new high-speed USB device number 2 ...
[ 2.345678] eth0: link is up
Key Options and Examples
1. Clearer Timestamps (--ctime
)
By default, the timestamps are shown as seconds since boot. To convert them into human-readable dates and times:
dmesg --ctime
2. Filter Messages by Keywords
To narrow down the output to specific messages, use the grep
command to search for keywords. For example:
- View USB-related messages
- View network-related messages
If you want to check only the most recent kernel messages, use:
dmesg | tail
4. Follow New Messages
To monitor new messages in real time, combine dmesg
with the -w
option:
dmesg -w
This is especially useful when you're troubleshooting a hardware issue, like plugging in a USB device, as it displays logs as they happen.
5. Limit Output by Level (-l
)
The -l
option lets you filter messages by their severity. Kernel messages are categorized into levels, such as:
- emerg: Critical issues requiring immediate attention.
- alert: Important warnings.
- info: Informational messages.
- debug: Detailed logs for debugging.
Example: To view only warnings and errors:
dmesg -l warn,err
6. Save Output to a File
To save the dmesg
output for further analysis:
dmesg > kernel_logs.txt
kernel_logs.txt
with all the messages.Real-World Use Cases
1. Debugging a USB Device
If a USB device isn’t working, plug it in and use:
dmesg | grep usb
If your system is taking too long to boot, examine the boot logs:
dmesg | less
To check if your network interface is up and running:
dmesg | grep eth
Comments
Post a Comment