Terminal Mastery: Custom Linux Shortcuts for Fast Port Management
January 6, 2026
mrbeandev
2 min read
First how do you check the port's status in linux?
Before automating, it's essential to know how to do it manually. The most efficient way to check which process is occupying a specific port is using the lsof (List Open Files) command.
Simply run:
sudo lsof -i :8080
(Replace 8080 with your desired port number)
This command will show you the COMMAND, PID, and USER associated with that port. If nothing appears, the port is free!
How to make this process easier?
Repeating the same command with sudo and colons can be tedious. we can create an alias for these commands in our .zshrc or .bashrc file to make it a single-word command.
1. Add to your configuration file
Depending on your shell, add these lines to either ~/.zshrc (for Zsh) or ~/.bashrc (for Bash):
# Check what is running on a specific port
alias portcheck='f() { sudo lsof -i :"$1"; }; f'
# Force kill whatever is running on a specific port
alias portkill='f() { sudo lsof -t -i:"$1" | xargs -r sudo kill -9; echo "Port $1 cleared."; }; f'
2. How to apply the changes?
- Open your configuration file:
- For Zsh:
nano ~/.zshrc - For Bash:
nano ~/.bashrc
- For Zsh:
- Paste the aliases at the bottom.
- Save (Ctrl+O, Enter) and Exit (Ctrl+X).
- Refresh your terminal session by running:
- For Zsh:
source ~/.zshrc - For Bash:
source ~/.bashrc
- For Zsh:
Now you can use these two aliases anywhere:
portcheck <port>: to check what is running on a specific portportkill <port>: to force kill whatever is running on a specific port
Example usage:
portcheck 3000
portkill 3000