Unix
Thursday, August 14, 2025
Sudo Command Details
Thursday, May 1, 2025
vi commands
๐ vi Editor Commands with Examples
๐น Starting vi
vi filename
This opens filename in vi. If it doesn't exist, vi will create it when saved.
๐ Modes in vi
- Normal Mode: Default mode for navigation and commands.
- Insert Mode: For editing text.
- Command Mode: For file operations (triggered by
:).
✍️ Insert Mode Commands
| Command | Description |
|---|---|
i | Insert before cursor |
I | Insert at beginning of line |
a | Append after cursor |
A | Append at end of line |
o | Open new line below |
O | Open new line above |
Esc | Exit insert mode |
๐งช Example:
Press i → type your text → press Esc to exit insert mode.
๐ File Commands (Command Mode)
| Command | Description |
|---|---|
:w | Save the file |
:q | Quit vi |
:wq | Save and quit |
:q! | Quit without saving |
:w new.txt | Save content to new.txt |
๐ Navigation Commands
| Command | Description |
|---|---|
h | Move left |
l | Move right |
j | Move down |
k | Move up |
0 | Beginning of line |
$ | End of line |
G | End of file |
gg | Start of file |
:n | Go to line number n |
Ctrl+d | Scroll down half page |
Ctrl+u | Scroll up half page |
✂️ Editing Commands
| Command | Description |
|---|---|
x | Delete character under cursor |
dd | Delete current line |
yy | Copy (yank) current line |
p | Paste below |
P | Paste above |
u | Undo last change |
Ctrl+r | Redo |
r<char> | Replace character under cursor |
R | Enter replace mode |
๐ Search and Replace
| Command | Description |
|---|---|
/word | Search forward for "word" |
?word | Search backward for "word" |
n | Repeat last search |
:%s/old/new/g | Replace all "old" with "new" |
:5,10s/foo/bar/g | Replace from line 5 to 10 |
✅ Practical Example
$ vi demo.txt
Then inside vi:
- Press
iand type: - Press
Esc - Type
:wqto save and quit
Hello World
This is a test file.
jobs command
Unix jobs Command - Usage Guide
๐ง What Is a "Job" in Unix?
A job refers to a command or process started from a shell. Jobs can be:
- Foreground: Active and attached to the terminal.
- Background: Running behind the scenes.
- Stopped: Suspended with
Ctrl+Z.
๐ Usage of jobs Command
jobs
This displays all jobs started from the current shell, including their ID, state, and command.
๐ Example Output
$ sleep 100 &
[1] 12345
$ jobs
[1]+ Running sleep 100 &
⌨️ Related Job Control Commands
| Command | Description |
|---|---|
& | Run a command in the background |
Ctrl+Z | Pause (suspend) a foreground job |
fg | Resume job in foreground |
bg | Resume job in background |
kill %1 | Kill job with ID 1 |
jobs -l | Show job PID |
jobs -p | Show job all processes |
✅ When It's Useful
- Managing long-running processes during an active shell session
- Suspending/resuming tasks without killing them
- Debugging shell-based automation or accidental background tasks
๐งช Step-by-Step Example: Managing Jobs
๐น Step 1: Start a Long-Running Command
$ sleep 300
๐น Step 2: Suspend with Ctrl + Z
^Z
[1]+ Stopped sleep 300
๐น Step 3: Check Jobs
$ jobs
[1]+ Stopped sleep 300
๐น Step 4: Resume in Background
$ bg %1
[1]+ sleep 300 &
๐น Step 5: Bring Back to Foreground
$ fg %1
sleep 300
๐น Step 6: Kill the Job
$ jobs
[1]+ Running sleep 300 &
$ kill %1
$ jobs
[1]+ Terminated sleep 300
✅ Summary
&to run in backgroundCtrl+Zto pausebg/fgto resumekillto terminate
Unix Top Command
Unix top Command - Usage Guide
๐ What top Shows
When you run top, it displays:
1. System Summary
- uptime: How long the system has been running.
- users: Number of logged-in users.
- load average: 1, 5, and 15 minute load.
- Tasks: Total, running, sleeping processes, etc.
- CPU usage:
%us: user processes%sy: system/kernel processes%id: idle time%wa: I/O wait%hi: hardware interrupts%si: software interrupts%st: steal time (VMs)
- Memory and Swap: Total, used, free, buffers, etc.
2. Process List
Displays information per process:
PID: Process IDUSER: Owner%CPU: CPU usage%MEM: Memory usageTIME+: Total CPU time usedCOMMAND: Command name
๐ ️ Common Day-to-Day Usage in Production
- Find High CPU Usage: Use
Pto sort by CPU usage. - Check Memory Leaks: Use
Mto sort by memory usage. - Identify Stuck or Zombie Processes: Look for state
Z. - Check Load Averages: High load may indicate system stress.
- Check I/O Wait: High
%waindicates disk bottlenecks. - Kill a Misbehaving Process: Use
kand enter PID.
๐ง Useful Keybindings in top
| Key | Action |
|---|---|
P | Sort by CPU usage |
M | Sort by memory usage |
k | Kill a process (enter PID) |
R | Renice a process |
c | Toggle full command display |
1 | Show all CPU cores |
h | Help |
q | Quit |
๐งช Pro Tips
- Use
top -u <username>to filter by user. - Use
top -p <PID>to monitor a specific process. - Use
htopfor a more visual alternative (if installed).
Thursday, April 10, 2025
Windows Find a PID of a Process Running on a Port
netstat -aon | findstr :<port>
Saturday, April 5, 2025
SSH Like A Pro
Hey Mate! Welcome to another blog. Here we will discuss about SSH automation.
Tired of manually typing long SSH commands every time? Want to auto-complete your SSH like a pro? Well, you’re in the right place!
Ever find yourself typing out long SSH commands, only to realize you’ve misspelled the hostname? Or worse — forgotten the exact host? ๐ซ If you’re tired of manually looking up hostnames, it’s time to level up your SSH game with auto-completion using ~/.ssh/config! ๐
With just a bit of setup, you can:
✅ Auto-complete SSH hostnames with Tab
✅ Avoid remembering long hostnames
✅ Save time and effort while connecting
✅SSH UI for Login
Let’s dive in and make SSH work for you, not against you! ๐
1️⃣ Without Use SSH Auto-Completion?
The Problem
Without SSH auto-completion, you might find yourself doing things like:
ssh 192.168.1.104 -i ~/.ssh/id_rsa -p 2222 -J jumpserverThat’s a lot of typing, and let’s be real — you’re going to mess it up at some point.
Solution:
๐ Create or Edit Your ~/.ssh/config File
Run this command to open (or create) your SSH config file:
nano ~/.ssh/configNow, add your SSH hosts in this format:
Host my-server
HostName 192.168.1.104
User myuser
IdentityFile ~/.ssh/id_rsa
Port 2222
ProxyJump jumpserver
Host my-webserver
HostName 10.0.0.5
User ubuntu
IdentityFile ~/.ssh/webserver_key
Host jumpserver
HostName 10.11.0.5
User ubuntu
IdentityFile ~/.ssh/webserver_keyThe real problem is here:
But wait if you have 50+ hostname, how can you remember for all the hostnames ? it’s pretty annoying right.
How It feel like, if you can complete with “TAB” while type SSHhostnames ?
Like linux navigation ๐๐
Let’s do SSH Auto Completion…
๐ 2 : Enable Auto-Completion in Bash
Now, let’s make sure your terminal can auto-complete SSH hostnames:
complete -W "$(grep "Host" ~/.ssh/config | grep -vE "HostName|Hostname|no" | awk '{print $2}' | sed -E "/\*/d")" ssh๐ก Tip: Add this line to ~/.bashrc or ~/.zshrc to make it permanent:
echo 'complete -W "$(grep "Host" ~/.ssh/config | grep -vE "HostName|Hostname|no" | awk '{print $2}' | sed -E "/\*/d")"' ssh >> ~/.bashrc
source ~/.bashrc3. Testing SSH Auto-Completion:
Now, try typing:
ssh my<tab>
# output
my-server my-webserverYour terminal should auto-complete to myserver. ๐
4. Let’s Connect via SSH UI:
But wait, yes you heard right it’s SSH UI.
Execute the below script, and you navigate and log in the hosts.
Pre-requisites:
apt install dialogSSH-UI Script:
#!/bin/bash
# Extract hosts from SSH config
HOSTS=$(grep "Host" ~/.ssh/config | grep -vE "HostName|Hostname|no" | awk '{print $2}' | sort -h | sed -E "/\*/d")
# Build dialog menu options dynamically
MENU_OPTIONS=""
COUNT=1
declare -A HOST_MAP # Map for storing numbered options
while read -r HOST; do
MENU_OPTIONS+="$COUNT \"$HOST\" "
HOST_MAP["$COUNT"]="$HOST"
((COUNT++))
done <<< "$HOSTS"
# Show dialog menu
MACHINE=$(dialog --menu "Welcome! Karthick-Dkk\nSelect Machine for Login:" 0 0 0 $MENU_OPTIONS 3>&1 1>&2 2>&3 3>&-)
clear
# If a valid selection was made, SSH into the selected host
if [[ -n "$MACHINE" ]]; then
ssh "${HOST_MAP[$MACHINE]}"
exit
else
echo "No machine selected. Exiting..."
exit 1
fiExecute:
chmod +x
./ssh-ui-script
Add to system-level command: (Optional)
If tired of executing manually, we can make our life easier, will add system command for SSH-UI.
command: ui-ssh
# ln -s >location of script> <command-name>
ln -s ssh-ui-script /usr/bin/ui-sshOnce added you can use the below command to open the SSH-UI.
ui-sshNow you can select your SSH host from a menu instead of typing! ๐ฅ️
️5. Bonus: Convert Salt Roster (/etc/salt/roster) to ~/.ssh/config (Automation)
If you manage many hosts using SaltStack, manually adding entries is a pain. Here’s a script to convert /etc/salt/roster to ~/.ssh/config automatically:
๐ง Roster to SSH Config Script
#!/bin/bash
ROSTER_FILE="/etc/salt/roster"
SSH_CONFIG="$HOME/.ssh/config"
# Backup existing SSH config
cp "$SSH_CONFIG" "$SSH_CONFIG.bak"
> "$SSH_CONFIG"
while read -r line; do
[[ -z "$line" || "$line" =~ ^#.*$ ]] && continue
if [[ "$line" =~ ^[a-zA-Z0-9_-]+: ]]; then
HOST_ALIAS="${line%:}"
elif [[ "$line" =~ "host:" ]]; then
HOST_IP=$(echo "$line" | awk '{print $2}')
elif [[ "$line" =~ "user:" ]]; then
SSH_USER=$(echo "$line" | awk '{print $2}')
elif [[ "$line" =~ "port:" ]]; then
SSH_PORT=$(echo "$line" | awk '{print $2}')
elif [[ "$line" =~ "priv:" ]]; then
SSH_KEY=$(echo "$line" | awk '{print $2}')
elif [[ "$line" =~ "proxy:" ]]; then
PROXY_JUMP=$(echo "$line" | awk '{print $2}')
fi
if [[ -n "$HOST_ALIAS" && -n "$HOST_IP" && -n "$SSH_USER" && -n "$SSH_KEY" ]]; then
{
echo "Host ${HOST_ALIAS}"
echo " HostName $HOST_IP"
echo " User $SSH_USER"
echo " Port ${SSH_PORT:-22}"
echo " IdentityFile $SSH_KEY"
[[ -n "$PROXY_JUMP" ]] && echo " ProxyJump $PROXY_JUMP"
echo ""
} >> "$SSH_CONFIG"
unset HOST_ALIAS HOST_IP SSH_USER SSH_PORT SSH_KEY PROXY_JUMP
fi
done < "$ROSTER_FILE"
echo "SSH config updated!"Run It:
chmod +x roster_to_ssh_config.sh
sudo ./roster_to_ssh_config.shTL;DR — Quick Setup
1️⃣ Edit ~/.ssh/config and add your hosts
2️⃣ Enable auto-completion in Bash/Zsh
3️⃣ Use ssh <tab> to autocomplete hostnames
4️⃣ (Bonus) Convert /etc/salt/roster to ~/.ssh/config
5️⃣ (Bonus) SSH menu selection script
Enjoy your new and improved SSH workflow! ๐
Why This Rocks?
✅ Saves Time: No more typing long SSH commands
✅ Auto-Completion: Just press TAB and autocomplete!
✅ No More IP Headaches: Human-friendly SSH hostnames
✅ Portable & Reusable: Works across multiple machines
Final Thoughts:
๐น Before: Manually typing long SSH commands ๐ฉ
๐น After: Tab-completion, menu selection, and automation! ๐
This setup saves time, reduces errors, and makes SSH easier. No more “What was that server’s IP again?” moments! ๐
If you found this useful, share it with your fellow sysadmins! ๐ก๐ป
Try it out and thank me later! ๐ Happy SSH-ing! ๐๐ง
Sunday, March 16, 2025
Localhost
if ::1 and getting response then that system is using IPV6
Sudo Command Details
sudo -l -U a762311 Output : User a762311 may run the following commands on MACLB390804
-
Here is the complete **HTML version** of the `vi` commands guide with examples: ```html vi Editor Commands and Examples ๐...
-
Hey Mate! Welcome to another blog. Here we will discuss about SSH automation. Tired of manually typing long SSH commands every time? Want to...
-
Here is the combined HTML version of both responses: the explanation of the `jobs` command and the step-by-step example. ```html Un...