Basic Linux Commands – Ubuntu, Centos, and Debian

In this chapter, you will learn

  1. Basic Linux - Ubuntu/Debian and Centos commands

If you are embarking on the journey of learning Linux server management, having a solid understanding of Linux commands is essential. If you're already familiar with Linux commands and have recent experience working on the Linux console, feel free to skip this chapter.

This section introduces some fundamental yet crucial Linux commands that will prove invaluable as you manage your VPS server.

Simply open the Ubuntu/CentOS terminal and begin entering the commands. Learning and executing Linux commands is straightforward and will enhance your server management skills.

1. Essential Console Commands

# Command Description
1 clear This command is used for clearing the console screen.
2 --help This command shows list of all available options along with their usage information for that specific command.
uptime --help
Usage: uptime [options] Options: -p, --pretty show uptime in pretty format -h, --help display this help and exit -s, --since system up since -V, --version output version information and exit For more details see uptime(1).
3 !! It is a shortcut for executing the last commands.
4 Ctrl + c It is used for terminating the current execution.

2. Directory/File Handling

# Command Description
1 ls ls command is used for listing the file and directories in a directory.
Command Description
ls -l List files and directories in long format including the file permission, owner group, and modification time.
ls -a List files and directories including hidden files.
ls -t Sort files according to modification time and shows newest file first.
ls -a -l -t
total 2128 drwxr-xr-x 99 root root 4096 Jan 15 18:56 etc drwxr-xr-x 6 root root 120 Jan 15 18:56 run drwxr-xr-x 19 root root 4096 Jan 15 18:56 . drwxr-xr-x 19 root root 4096 Jan 15 18:56 .. dr-xr-xr-x 192 root root 0 Jan 15 18:56 proc drwxr-xr-x 11 root root 3060 Jan 15 18:56 dev dr-xr-xr-x 11 root root 0 Jan 15 18:56 sys drwxrwxrwt 4 root root 4096 Jan 10 13:53 tmp -rwxrwxrwx 1 root root 2102392 Nov 11 02:59 init
2 cd This command is used for changing the current working directory.
Command Description
cd [path_to_directory] Changing current working directory to path provided.
cd ~ Changing path from current working directory to home directory.
cd - Switch back to previous working directory. It is a best way to toggle between directories.
cd .. Changing path from current working directory to parent working directory.
cd / Changing path from current working directory to root directory.
cd /
$cd home
home$ cd ~
$home$ cd ..
$cd ~
3 pwd pwd (Print Working Directory) command returns the name of current working directory.
pwd
/home/prashant
4 mkdir mkdir (Make Directory) command is used for creating a directory.
mkdir easycodelab
$ls
easycodelab p1
5 rmdir rmdir (Remove Directory) command is used for removing specified directory.
ls
easycodelab p1
rmdir easycodelab $ls
p1
6 touch touch command is used for creating a new empty file.
touch myfile.txt
$ls
myfile.txt p1
7 rm rm (Remove) command is used for removing a file.
ls
myfile.txt p1
rm myfile.txt $ls
p1
8 mv mv (Move) command is used for moving or renaming a file or a directory.

→ Renaming a Directory:

Syntax: mv [old directory name/filename] [new directory name/filename]
mkdir sunday
ls
p1  sunday
mv sunday monday
ls
monday  p1


→ Moving a Directory:

Syntax: mv [current directory] [source directory]
ls
monday  p1
mv monday p1
ls
p1
cd p1
prashant@easycodelab:~/p1$ ls
monday
9 echo echo command in Linux is used for writing something into a file, displaying text, and displaying the variable value on the terminal.

Saving text into a file.

echo "Mic Testing... 1 2 3" > status.txt
cat status.txt
Mic Testing... 1 2 3
Displaying variable value.

name="Jack Martin"
echo "Hello $name. How are you?"
Hello Jack Martin. How are you?
10 printf printf command is also used for storing text on a file, displaying text, and displaying the variable value on the terminal.
name="Jack Martin"
printf "Hello $name, How are you?"
Hello Jack Martin, How are you?
11 cat cat command is used for showing the content of a file.
echo "Hi there! how are you?" > status.txt
cat status.txt
Hi there! how are you?

3. System Information Command


# Command Description
1 uname uname command is used for displaying system information. Use this command with -a option to display all the information.

uname -a
Linux prashant 5.25.124.2-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
2 whoami This command displays the name of current logged in user.

whoami
prashant
3 df df command is used for displaying disk usage information in Linux.

df
Filesystem     1K-blocks      Used Available Use% Mounted on
none             4026356         4   4026352   1% /mnt/wsl
none           365063728 203162076 161901652  56% /usr/lib/wsl/drivers
/dev/sdc       263112772   2070492 247604124   1% /
none             4026356        80   4026276   1% /mnt/wslg
none             4026356         0   4026356   0% /usr/lib/wsl/lib
rootfs           4023060      2056   4021004   1% /init
none             4023060         0   4023060   0% /dev
none             4026356         0   4026356   0% /run
none             4026356         0   4026356   0% /run/lock
none             4026356         0   4026356   0% /run/shm
4 du du commands display the sizes of directories and subdirectories.

du
4       ./.landscape
4       ./.config/procps
4       ./.config/htop
12      ./.config
8       ./jack
8       ./.ssh
76      ./.dotnet/corefx/cryptography/crls
80      ./.dotnet/corefx/cryptography
5 free This command shows the system memory information.

free
      total    used    free    shared buff/cache available
Mem:  8052716  273204  7630448  2380  149064     7572432
Swap: 2097152  0       2097152
6 uptime The uptime command provides information about the current time, the duration the system has been running, the number of users currently logged in, and the system load averages over the last 1, 5, and 15 minutes.

uptime
14:51:43 up 2 min,  0 users,  load average: 0.01, 0.02, 0.00
7 lscpu This command shows detailed cpu information.

lscpu
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Byte Order:                         Little Endian
Address sizes:                      39 bits physical, 48 bits virtual
CPU(s):                             8
On-line CPU(s) list:                0-7
Thread(s) per core:                 2
Core(s) per socket:                 4
    
8 lspci This command shows PCI information along with connected device.

lspci
32c9:00:00.0 SCSI storage controller: Red Hat, Inc. Virtio filesystem (rev 01)
35ca:00:00.0 3D controller: Microsoft Corporation Device 008e
6f08:00:00.0 3D controller: Microsoft Corporation Device 008e
8ddd:00:00.0 SCSI storage controller: Red Hat, Inc. Virtio filesystem (rev 01)
9f0f:00:00.0 System peripheral: Red Hat, Inc. Device 105a (rev 01)
    
9 lsusb This command shows information about USB buses and the device connected to them.

lspci
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

4. Network Commands


# Command Description
1 ip ip command is used for configuring and displaying network interfaces and routing information.
Command Description
ip a Show Information about Network Interfaces:
ip a
1: inet 127.0.0.1/8 scope host lo
   valid_lft forever preferred_lft forever
ip route Show Information about Routing Tables
ip route
default via 122.35.122.11 dev eth0 proto kernel
122.35.122.10/20 dev eth0 proto kernel scope link src 112.15.12.101
ip -s link Show Detailed Link Information
ip -s link
1: lo:  mtu 65536 qdisc n
    link/loopback 00:00:00:00:00:00
    RX: bytes  packets  errors  
    0          0        0       
    TX: bytes  packets  errors  
    0          0        0
2 ping ping command is used to test the reachability of a host on an internet.
ping easycodelab.com
PING easycodelab.com (172.67.129.198) 56(84) bytes of data.
64 bytes from 172.67.129.198: icmp_seq=1 ttl=48 time=193 ms
64 bytes from 172.67.129.198: icmp_seq=2 ttl=48 time=157 ms
64 bytes from 172.67.129.198: icmp_seq=3 ttl=48 time=209 ms
64 bytes from 172.67.129.198: icmp_seq=4 ttl=48 time=175 ms
64 bytes from 172.67.129.198: icmp_seq=5 ttl=48 time=197 ms
^C
--- easycodelab.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4204ms
rtt min/avg/max/mdev = 156.683/185.994/209.154/18.332 ms
3 netstat The netstat command in Linux is used to display various network-related information such as network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
netstat
Active Internet connections (w/o servers)
Active UNIX domain sockets (w/o servers)
Proto RefCnt Type       State         I-Node   Path
unix  3      STREAM     CONNECTED     19483    /tmp/.X11-unix/X0
unix  3      STREAM     CONNECTED     18474
4 ssh It is used to establish a secure shell (SSH) connection to a remote server or device.

a. To connect a remote server using default port
ssh username@remote_server

b. Connect to a Remote Server on a Specific Port:
ssh -p 2222 username@remote_server

5 scp The scp command is used to securely copy files between host over a network.

a. Copy a File from Local to Remote:
scp local_file.txt username@remote_server:/path/to/destination/

b. Copy a File from Remote to Local:
scp username@remote_server:/path/to/remote_file.txt /local/destination/

c. Copy a Directory Recursively:
scp -r local_directory/ username@remote_server:/path/to/destination/

6 wget This command is used for downloading files from the internet.

a. Download a File
wget http://example.com/file.zip

b. Save with a Different Name:
wget -O new_name.zip http://example.com/file.zip

c. Recursive Download (Download Entire Website):
wget -r http://example.com/

7 curl The curl command is widely used for making HTTP requests from the command line and is a powerful tool for various tasks, including testing APIs, downloading files, and interacting with web services.

Download a File:
curl http://example.com/file.zip -o file.zip