Frequently used commands
Published on: 22nd Apr 2014
Updated on: 20th Oct 2023
Overview
This page contains the frequently used commands in normal day operations.
Operations
-
To view the processes that is running,
top
Or
htop
Where
htop
has to be installed with the following command,sudo apt install htop
-
To view memory usage
free -h
Where the "-h" argument means human readable numbers.
-
To find the location of a program (where
7z
is a compression utility program),whereis 7z
-
To search text in a text file, we will first call
cat
to load the contents into memory and then pipes (|) the contents of thegrep
command to do the searching. In the following case, it matches for 'some-search-text' that appears in the 'auth.log' file.sudo cat /var/log/auth.log | grep some-search-text
-
If a file has been gzip (file extension is .gz), then, use
zcat
to load and decompress the contents and the pipes the contents togrep
:sudo zcat /var/log/auth.log.1.gz | grep some-search-text
-
Matching text1 and excluding text2, in this case, you need two
grep
commands. The first one is matching text1 and after that excluding text2.sudo zcat /var/log/auth.log.1.gz | grep text1 | grep -Ev text2
-
Find out current directory,
pwd
-
To search file in sub-directory,
find . -name myFileName*
-
To cancel a command, press CONTROL+C.
Server management
-
To view current server name,
hostname
-
To change the host name,
sudo hostnamectl set-hostname myserver01
Before rebooting the server (this requires to do it for one time only),
sudo nano /etc/cloud/cloud.cfg
Then, change the following line from "false" to "true".
preseve_hostname: true
Save it and reboot the server.
-
To reboot the server,
sudo reboot
Or
sudo shutdown -r
-
To shut down the server,
sudo shutdown
-
Viewing the server OS version
cat /etc/lsb-release
-
To check if the server requires rebooting after apt update
cat /var/run/reboot-required
-
18.Sep.2022: How to find out if the server is listening to port 21.
Run this command:
netstat -peanut
And you will see something like this:
tester@ubuntu11:/$ netstat -peanut (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 24969 - tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 0 28595 - tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 101 22735 - tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 24780 - tcp 0 240 192.168.101.11:22 192.168.101.1:62289 ESTABLISHED 0 27000 - tcp6 0 0 :::8080 :::* LISTEN 1000 26678 955/node /home/test tcp6 0 0 :::80 :::* LISTEN 0 24970 - tcp6 0 0 :::8084 :::* LISTEN 1000 26679 984/node /home/test tcp6 0 0 :::22 :::* LISTEN 0 24791 - udp 0 0 127.0.0.53:53 0.0.0.0:* 101 22647 - udp 0 0 10.0.2.15:68 0.0.0.0:* 100 17443 -
From the result, we confirm that the server is listening on port 21.
-
20.Oct.2023: view the current time setting in the server
timedatectl
To view all available time zones.
timedatectl list-timezones
To change the time zone to UTC+0,
timedatectl set-timezone UTC
Daemon/service management
-
To start, stop, restart or check status of a daemon
sudo systemctl [start|stop|restart|status] service_name
For example, check the fail2ban service
sudo systemctl status fail2ban
OR equivalent command:
sudo service fail2ban status
-
To view all services
sudo systemctl -l
OR
sudo service --status-all
-
To view failed services
sudo systemctl --failed
-
Search for 'nginx' service
sudo service --status-all | grep nginx
apt command
-
To view installed packages
sudo apt list --installed sudo apt list -i
-
View the list of upgradable packages,
apt list --upgradeable
-
To upgrade the installed components,
sudo apt update sudo apt full-upgrade
-
To see if you need to reboot the server after full upgrade,
sudo cat /var/run/reboot-required
-
To cleanup the unused components and recover some spaces,
sudo apt --purge autoremove
-
Finally, upgrade the OS if you have it.
sudo do-release-upgrade
Personalize your console command by using alias
.
-
Edit your console profile,
sudo nano ~/.bashrc
Then, add the following (my frequently used commands) to the bottom of the file,
alias f2b='sudo iptables -S | grep f2b' alias f2b-log='sudo tail -n 30 /var/log/fail2ban.log' alias auth-log='sudo tail /var/log/auth.log -n 100' alias check-reboot='cat /var/run/reboot-required'
After saving the changes, you will be able to use the alias. For example, to view the IP address that blocked by fail2ban.
sudo f2b
Related posts
Jump to #UBUNTU blog
Author
Lau Hon Wan, software developer.