Thursday, August 14, 2025

Sudo Command Details

sudo -l -U a762311 Output : User a762311 may run the following commands on MACLB390804

Thursday, May 1, 2025

vi commands

Here is the complete **HTML version** of the `vi` commands guide with examples: ```html vi Editor Commands and Examples

๐Ÿ“ 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

CommandDescription
iInsert before cursor
IInsert at beginning of line
aAppend after cursor
AAppend at end of line
oOpen new line below
OOpen new line above
EscExit insert mode

๐Ÿงช Example:

Press i → type your text → press Esc to exit insert mode.

๐Ÿ“‚ File Commands (Command Mode)

CommandDescription
:wSave the file
:qQuit vi
:wqSave and quit
:q!Quit without saving
:w new.txtSave content to new.txt

๐Ÿ” Navigation Commands

CommandDescription
hMove left
lMove right
jMove down
kMove up
0Beginning of line
$End of line
GEnd of file
ggStart of file
:nGo to line number n
Ctrl+dScroll down half page
Ctrl+uScroll up half page

✂️ Editing Commands

CommandDescription
xDelete character under cursor
ddDelete current line
yyCopy (yank) current line
pPaste below
PPaste above
uUndo last change
Ctrl+rRedo
r<char>Replace character under cursor
REnter replace mode

๐Ÿ” Search and Replace

CommandDescription
/wordSearch forward for "word"
?wordSearch backward for "word"
nRepeat last search
:%s/old/new/gReplace all "old" with "new"
:5,10s/foo/bar/gReplace from line 5 to 10

✅ Practical Example

$ vi demo.txt

Then inside vi:

  1. Press i and type:
  2. Hello World
    This is a test file.
  3. Press Esc
  4. Type :wq to save and quit
``` You can save this as `vi-commands.html` and open it in any browser. Would you like a downloadable file version or additional content like visual cheat sheets or keyboard diagrams?

jobs command

Here is the combined HTML version of both responses: the explanation of the `jobs` command and the step-by-step example. ```html Unix jobs Command - Guide and Example

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

CommandDescription
&Run a command in the background
Ctrl+ZPause (suspend) a foreground job
fgResume job in foreground
bgResume job in background
kill %1Kill job with ID 1
jobs -lShow job PID
jobs -pShow 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 background
  • Ctrl+Z to pause
  • bg / fg to resume
  • kill to terminate
``` You can save this as `jobs-command.html` and open it in any web browser. Want me to include downloadable links or CSS framework enhancements?

Unix Top Command

Here's the above explanation in clean, semantic HTML format: ```html Unix top Command - Usage Guide

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 ID
  • USER: Owner
  • %CPU: CPU usage
  • %MEM: Memory usage
  • TIME+: Total CPU time used
  • COMMAND: Command name

๐Ÿ› ️ Common Day-to-Day Usage in Production

  • Find High CPU Usage: Use P to sort by CPU usage.
  • Check Memory Leaks: Use M to 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 %wa indicates disk bottlenecks.
  • Kill a Misbehaving Process: Use k and enter PID.

๐Ÿง  Useful Keybindings in top

Key Action
PSort by CPU usage
MSort by memory usage
kKill a process (enter PID)
RRenice a process
cToggle full command display
1Show all CPU cores
hHelp
qQuit

๐Ÿงช Pro Tips

  • Use top -u <username> to filter by user.
  • Use top -p <PID> to monitor a specific process.
  • Use htop for a more visual alternative (if installed).
``` You can save this as an `.html` file and open it in any browser. Want me to include a live example or a downloadable template?

Thursday, April 10, 2025

Windows Find a PID of a Process Running on a Port

 netstat -aon | findstr :<port>

  TCP    0.0.0.0:8080      0.0.0.0:0      LISTENING       1234


TO kill it 

taskkill /PID 1234 /F

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 jumpserver

That’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/config

Now, 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_key

The 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 ~/.bashrc

3. Testing SSH Auto-Completion:

Now, try typing:

ssh my<tab>
# output
my-server my-webserver

Your 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 dialog

SSH-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
fi

Execute:

chmod +x 
./ssh-ui-script
SSH-UI

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-ssh

Once added you can use the below command to open the SSH-UI.

ui-ssh

Now 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.sh

TL;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 ping localhost or 127.0.0.1 and getting response then that system is using IP4
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