Top Linux Commands for Seamless Operations

Linux Commands for Seamless Operations

Top Linux Commands for Linux Administrators

The essential tools for interacting with and controlling an operating system based on Linux are Linux commands.

Having command proficiency with these commands enables users to effectively interact with their systems, from file manipulation to system setup and network administration.

This thorough tutorial will explore the most important Linux commands, including tips, examples, and useful apps to help you become more proficient with Linux system operations and make the most of your command-line expertise.

Here are some useful Linux commands that may help you to do day-to-day work.

Change Directory

cd /path/to/folder

List files and directories in the path

ls -lh

Check current directory

PWD

Delete the folder

rm -rf /path/to/directory

Delete the file

rm filename

Create a symlink (shortcut)

ln -s /path/to/file /path/to/symlink

Read the file

cat /path/to/file
tail /path/to/file

Edit the file

vi /path/to/file
nano /path/to/file

Copy file

cp filename /path/to/destination

Move

mv -v /path/of/source /path/to/destination

Rename the file

mv [options] source_file destination_file

To find the path

For ex:- to find the nginx directory location

which nginx

To search the files or directory

find ./ -name "long.txt"

Find Files on the System by name

mount

Enables to mount even remote file system

locate

Change the ownership of the file or directories

sudo chown user:group /path/to/directory

Use -R to recursively change the owner and groups for all underneath files and directories

sudo chown -R user:group /path/to/directory

Check the permission of files and directories

ls -l

Change permissions

sudo chmod 755 /path/to/file

for more information on octal values refer to https://www.thegeekdiary.com/linux-file-directory-permissions-cheat-sheet/

If you want to change the user owning this file or directory (folder), you will have to use the command chown. For instance, if you run

sudo chown username: myfolder

the user owning myfolder will remain. Then you can execute below command to add the write permission to the username user.

sudo chmod u+w myfolder

if you want to add this user to the group associated with “myfolder”, you can run

sudo usermod -a -G groupname username

and then execute to add the write permission to the group

sudo chmod g+w myfolder

To check the running processes sorted by memory

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem

The other command is Top which gives some brief information about memory on the system and locations of the process

top -c -b -o +%MEM | head -n 100 | tail -100
htop

Simple Memory Check

free -m

To show the memory map of the Process

pmap {PID}

List down the services

systemctl --type=service

to filter the running services only

systemctl --type=service --state=running 

Restart the Service

systemctl restart servicename 

Enable Service

systemctl enable service.name

Disable Service

systemctl disable service.name

To clear the Service cache due to changes

systemctl daemon-reload

To clear the failed Services from the list

systemctl reset-failed

To know the Ports being used by running processes with PID

sudo ss -plunt
sudo lsof -i -P -n | grep LISTEN

Check the space on Linux

df -h

To thoroughly check the space used by which path

du -h -d1 /path/to/folder

Delete large amount of files by date

If the files are not modified after initial creation, you could delete them if they have not been modified in over 90 days:

find /path/to/folder ! -type d -mtime +90 -exec rm -f {} +

Show directory space usage.

du

To Increase the Disk Space

Please check this Article

Root Admin

To Enter the Sudo mode

sudo -i

App or Package Installation and Removal

To Install the app

CentOS Command

yum install app name

*make sure the current repo is updated or the file is downloaded in case of manual installation

Ubuntu Command

apt-get install app name

To remove the app

CentOS

yum remove app name

Ubuntu

apt-get remove app name

Update or Patching the Linux

CentOS

yum update -y

Ubuntu

apt-get update
apt-get upgrade

Reboot

sudo reboot

Clear screen

clear

Compress or Extract files or directories

ZIP/Compress the file or directories

tar -czvf archive.tar.gz /path/to/file or folder

Extract file or folder

tar -xzvf filename.tar.gz -C /path/to/folder

Check the Private IP Address of the system

ifconfig -a 
ifconfig eth0

Change the IP address

check this article on How to Change IP Address in Ubuntu: A Comprehensive Guide

To find out the public IP (Gateway outbound IP)

curl ifconfig.me

Check IP route

ip r

Check the traceroute

pathping www.google.com

Track network performance

mtr www.google.com

to Check the opened connections

To display the program

netstat -p

To display the ports

netstat -s

To display route

netstat -r

Add Local Host record

sudo nano /etc/hosts

CentOS

sudo nano

Update Local CA Certificate

upload the local ca file pem or crt to your home directory via WinSCP

CentOS

cp mycert.crt /etc/pki/ca-trust/source/anchors/

update-ca-trust extract

Ubuntu

cp mycert.crt /usr/local/share/ca-certificates/

sudo update-ca-certificates

Check timezone

ls -l /etc/localtime

list timezones

timedatectl list-timezones

set timezone

sudo timedatectl set-timezone name of the zone 

Firewall

Allow firewall port

firewall-cmd --permanent --zone=public --add-port=80/tcp

firewall-cmd --list-ports

firewall-cmd --reload

Create a new user with password

sudo adduser "username"
su - "username"
*enter the password twice

Create a new user with ssh key and without a password

Ubuntu

sudo adduser "username" --disabled-password

Cent OS

sudo adduser "username"

Run below commands further in Linux (works on both centos or ubuntu)

su - "username"
mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys 

Options to use PPK files

  1. Create a new SSH public key directly in the server, run the below commands (if you already have the key then skip to step 2.)
ssh-keygen -t rsa
cat >> .ssh/authorized_keys

Copy the Public and Private keys to a .ppk file to your desktop, that will be used at a later stage to log in from Putty or WinSCP.

OR

2. if you need to use an existing SSH user public key or a key generated from puttygen software, run the below command and paste the public key

nano .ssh/authorized_keys 

Press Ctrl+X, Enter Y to save the file, and exit. Now, bypass the below steps and continue with next step to add user to sudo group

Exit from new user

exit

allow user to wheel or sudo group to have no password access

Ubuntu

sudo usermod -aG sudo username

Cent OS

sudo usermod -aG wheel username

Edit VIsudo

sudo visudo

Ubuntu

In the editor, add the following line at the very bottom, but before the #includedir statement.

username ALL=(ALL) NOPASSWD: ALL

Cent OS

Look for ##Allow root to run any commands anywhere, add the below line.

username ALL=(ALL) NOPASSWD:ALL

Make sure you have added PermitRootLogin without-password in sshd_config if not then please add it to the below location

sudo vi /etc/ssh/sshd_config

Change the password of the existing user

passwd

Get the UID and GID for the users

cat /etc/passwd

to get to know about a specific user

cat /etc/passwd | grep username

List existing Users

cat /etc/passwd
less /etc/passwd
more /etc/passwd

List existing Groups

cat /etc/group
less /etc/group
more /etc/group

Add Existing User to the Group

sudo usermod -a -G groupName userName

To download the content

wget https://website url

To test the website

curl https://website url

Modules

To list the enabled modules in Linux

for example:- list the PHP modules installed

 sudo dnf module php

System Information:

Display system information.

uname

Monitor system resources.

top or htop

CPU information.

lscpu
cat /proc/cpuinfo

List block devices.

lsblk 
fdisk -l

Display the system date and time.

date

Conclusion

I hope the above Linux commands will be useful for your day-to-day operations managing the Linux server.

Ravi Chopra
Scroll to Top