Linux basic commands
Updated: Oct 3, 2021
Document Id: RHEL002
In my previous post, I've shown how to install Red Hat Enterprise Linux, in this post I'll explain some basic commands every Linux administrator should know to operate Linux operating system.
hostname command, to check server hostname
[root@RHEL82 html]# hostname
RHEL82.localdomain
whoami command, to check login user
[root@RHEL82 html]# whoami
root
uname –a command, to get system identification, mainly kernel version
[root@RHEL82 html]# uname -a
Linux RHEL82.localdomain 4.18.0-193.el8.x86_64 #1 SMP Fri Mar 27 14:35:58 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
To get installed Red Hat Linux version details
[root@RHEL82 html]# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.2 (Ootpa)
ifconfig command, to check server network interface details
[root@RHEL82 html]# ifconfig
ping command, to check connectivity to the server
[root@RHEL82 ~]# ping 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
64 bytes from 192.168.10.10: icmp_seq=1 ttl=128 time=20.8 ms
pwd command
Present working directory, when we first login to terminal we are in user home directory, pwd command shows absolute path of the current directory
[root@RHEL82 ~]# pwd
mkdir command
To create an empty directory
[root@RHEL82 ~]# mkdir newfolder
Output: It will create a directory with name newfolder in the current working directory
To create a directory in the specified location
[root@RHEL82 ~]# mkdir /tmp/newfolder
Output: It will create a directory with name newfolder in the /tmp directory
ls command
List of files and directory
[root@RHEL82 ~]# ls
Output: anaconda-ks.cfginitial-setup-ks.cfgnewfolder
To check list of files and directory in the specified location
[root@RHEL82 ~]# ls /tmp/
To check hidden files and folders
[root@RHEL82 ~]# ls –a
To check all files and folder with associated permission
[root@RHEL82 ~]# ls –la
cd command
Change directory- we use this command to change the working directory location.
Example:
[root@RHEL82 ~]# pwd
/root
[root@RHEL82 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg newfolder
[root@RHEL82 ~]# cd newfolder/
[root@RHEL82 newfolder]# pwd
/root/newfolder
cd.. (double dot) to go parent directory
Example:
[root@RHEL82 html]# pwd
/var/www/html
[root@RHEL82 html]# cd ..
[root@RHEL82 www]# pwd
/var/www
cd (only cd or cd~) to go home directory from anywhere
Example:
[root@RHEL82 ~]# cd /var/www/html/
[root@RHEL82 html]# pwd
/var/www/html
[root@RHEL82 html]# cd
[root@RHEL82 ~]# pwd
/root
cd – (hyphen) to come back previous working directory
Example:
[root@RHEL82 html]# pwd
/var/www/html
[root@RHEL82 html]# cd
[root@RHEL82 ~]# pwd
/root
[root@RHEL82 ~]# cd -
/var/www/html
[root@RHEL82 html]# pwd
/var/www/html
touch command
To create new blank file
[root@RHEL82 ~]# touch newfile
[root@RHEL82 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg newfile newfolder
echo command
There are various use of echo command, here we will use echo command to create file and to append string to already created file
[root@RHEL82 ~]# echo This is RedHat Enterprise Linux >> newfile
[root@RHEL82 ~]# echo Current version 8.2 >> newfile
[root@RHEL82 ~]# cat newfile
This is RedHat Enterprise Linux
Current version 8.2
In echo command “>>” use to append content into the file
In echo command “>” use to add content to the file by deleting existing content
[root@RHEL82 ~]# echo delete all content > newfile
[do not use this command to system configuration files]
[root@RHEL82 ~]# cat newfile
delete all content
cat command
cat is short of concatenate, there are various use of cat command
To see file content
[root@RHEL82 ~]# echo Create a new file > file01
[root@RHEL82 ~]# cat file01
Create a new file
[root@RHEL82 ~]# echo This is the second file content > file02
[root@RHEL82 ~]# cat file02
This is the second file content
To add two file contents to a single file “file03” and to see the content of “file03”
[root@RHEL82 ~]# cat file01 file02 >> file03
[root@RHEL82 ~]# cat file03
Create a new file
This is the second file content
adduser command
To add new user
[root@RHEL82 ~]# useradd user01
passwd command
To set user password
[root@RHEL82 ~]# passwd user01
Changing password for user user01.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
su -
Switch to new user
[root@RHEL82 ~]# su - user01
[switch user from root user to another user password is not required, from any other user to any other user password is required]
[user01@RHEL82 ~]$ whoami
user01
exit command, to exit from login shell
[user01@RHEL82 ~]$ exit
Logout
cp command, to copy files or folder
Exampel:
To copy file01 to /var/www/html
[root@RHEL82 ~]# pwd
/root
[root@RHEL82 ~]# ls
anaconda-ks.cfg file01 file02 file03 initial-setup-ks.cfg newfile newfolder
[root@RHEL82 ~]# cp file01 /var/www/html/
[root@RHEL82 ~]# ls /var/www/html/
file01
To copy /var/ww/html/file01 to /mnt
[root@RHEL82 ~]# cp /var/www/html/file01 /mnt/
[root@RHEL82 ~]# ls /mnt/
file01
To copy directory/folder we have to use cp with (–R) option
To copy /root /newfolder to /mnt/
[root@RHEL82 ~]# cp -R /root/newfolder /mnt/
[root@RHEL82 ~]# cd /mnt/
[root@RHEL82 mnt]# ls
file01 newfolder
mv command, to move files
To move /root/file03 to /var/www/html/
[root@RHEL82 ~]# pwd
/root
[root@RHEL82 ~]# ls
anaconda-ks.cfg file01 file02 file03 initial-setup-ks.cfg newfile newfolder
[root@RHEL82 ~]# mv /root/file03 /var/www/html/
[root@RHEL82 ~]# ls
anaconda-ks.cfg file01 file02 initial-setup-ks.cfg newfile newfolder
[root@RHEL82 ~]# ls /var/www/html/
file01 file03
To move folder
[root@RHEL82 ~]# mv /mnt/newfolder /var/www/html/
[root@RHEL82 ~]# ls /var/www/html/
file01 file03 newfolder
rmdir command, to remove an empty directory
Example:
[root@RHEL82 ~]# mkdir emptydirectory
[root@RHEL82 ~]# ls
anaconda-ks.cfg emptydirectory file01 file02 initial-setup-ks.cfg newfile newfolder
[root@RHEL82 ~]# rmdir emptydirectory
[root@RHEL82 ~]# ls
anaconda-ks.cfg file01 file02 initial-setup-ks.cfg newfile newfolder
rm command, to delete files or folder
Example:
[root@RHEL82 ~]# ls
anaconda-ks.cfg file01 file02 initial-setup-ks.cfg newfile newfolder
[root@RHEL82 ~]# rm file01
rm: remove regular file 'file01'? y
[root@RHEL82 ~]# ls
anaconda-ks.cfg file02 initial-setup-ks.cfg newfile newfolder
rm –r command, to delete directory
Example:
[root@RHEL82 ~]# rm -r newfolder/
rm: descend into directory 'newfolder/'? y
rm: remove regular file 'newfolder/newfile'? y
rm: remove directory 'newfolder/'? y
[root@RHEL82 ~]# ls
anaconda-ks.cfg file02 initial-setup-ks.cfg newfile
rm –rf command, to delete entire directory without prompt
Exmaple:
[root@RHEL82 html]# pwd
/var/www/html
[root@RHEL82 html]# ls
file01 file03 newfolder
[root@RHEL82 html]# rm -rf newfolder
[use this command with caution , it will delete all data of the folder]
[root@RHEL82 html]# ls
file01 file03
grep command, to search full text of a file and display the full line of "matching word"
below command will display lines that contain word “NetowrkManager”
[root@RHEL82 ~]# grep NetworkManager /var/log/messages
Aug 31 22:12:56 RHEL82 NetworkManager[1430]: <info> [1598897576.2262] dhcp4 (eth0): option dhcp_lease_time => '86400'
Aug 31 22:12:56 RHEL82 NetworkManager[1430]: <info> [1598897576.2269] dhcp4 (eth0): option domain_name => 'mshome.net'
Aug 31 22:12:56 RHEL82 NetworkManager[1430]: <info> [1598897576.2270] dhcp4 (eth0): option domain_name_servers => '172.18.35.17'
Aug 31 22:12:56 RHEL82 NetworkManager[1430]: <info> [1598897576.2270] dhcp4 (eth0): option expiry => '1598983976'
Aug 31 22:12:56 RHEL82 NetworkManager[1430]: <info> [1598897576.2270] dhcp4 (eth0): option ip_address => '172.18.35.29'
ps command, to check system running process
To see every process on the system using standard syntax:
[root@RHEL82 ~]# ps –ef
Output: It will show all current services
To search for a particular service
[root@RHEL82 ~]# ps –ef |grep service_name
[root@RHEL82 ~]# ps -ef |grep nfs
root 2258 2156 0 20:57 ? 00:00:00 python3 /var/lib/pcp/pmdas/nfsclient/pmdanfsclient.python
root 23758 9955 0 23:36 pts/0 00:00:00 grep --color=auto nfs
free –h command, to see system memory usages
[root@RHEL82 html]# free -h
total used free shared buff/cache available
Mem: 2.5Gi 524Mi 1.5Gi 21Mi 498Mi 2.0Gi
Swap: 2.0Gi 0B 2.0Gi
df –h command , to see system disk usages
[root@RHEL82 html]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 854M 0 854M 0% /dev
tmpfs 882M 0 882M 0% /dev/shm
tmpfs 882M 18M 865M 2% /run
tmpfs 882M 0 882M 0% /sys/fs/cgroup
/dev/mapper/rootvg-root 20G 5.3G 15G 27% /
/dev/sda1 1014M 231M 784M 23% /boot
/dev/mapper/rootvg-home 9.8G 112M 9.7G 2% /home
/dev/mapper/rootvg-var 9.8G 488M 9.3G 5% /var
tmpfs 177M 16K 177M 1% /run/user/42
tmpfs 259M 4.0K 259M 1% /run/user/0
head command, to see first lines of a file, normally it shows first 10 lines.
Example:
[root@RHEL82 ~]# head /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 27 22:50:21 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
To see number of lines from the top of the file we can use
#head –n number file_name
[root@RHEL82 ~]# head -n 15 /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 27 22:50:21 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rootvg-root / xfs defaults 0 0
UUID=aa8807bd-6213-4159-a6ec-ff606a4cf3f0 /boot xfs defaults 0 0
/dev/mapper/rootvg-home /home xfs defaults 0 0
/dev/mapper/rootvg-var /var xfs defaults 0 0
tail command, to see last lines of a file, normally it shows last 10 lines
[root@RHEL82 ~]# tail /etc/fstab
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rootvg-root / xfs defaults 0 0
UUID=aa8807bd-6213-4159-a6ec-ff606a4cf3f0 /boot xfs defaults 0 0
/dev/mapper/rootvg-home /home xfs defaults 0 0
/dev/mapper/rootvg-var /var xfs defaults 0 0
UUID=be0efc87-f61b-4b50-abef-ebbe26ae19f3 swap swap defaults 0 0
To see number of lines form the last of the file we can use
#tail –n number files_name
[root@RHEL82 ~]# tail -n 15 /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 27 22:50:21 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rootvg-root / xfs defaults 0 0
UUID=aa8807bd-6213-4159-a6ec-ff606a4cf3f0 /boot xfs defaults 0 0
/dev/mapper/rootvg-home /home xfs defaults 0 0
/dev/mapper/rootvg-var /var xfs defaults 0 0
UUID=be0efc87-f61b-4b50-abef-ebbe26ae19f3 swap swap defaults 0 0
tail –f command , f=follow , output appended data as the file grows, very important command to check logs in progress
[root@RHEL82 ~]# tail -f /var/log/messages
Aug 31 22:07:56 RHEL82 NetworkManager[1430]: <info> [1598897276.2174] dhcp4 (eth0): option requested_subnet_mask => '1'
Aug 31 22:07:56 RHEL82 NetworkManager[1430]: <info> [1598897276.2174] dhcp4 (eth0): option requested_time_offset => '1'
Aug 31 22:07:56 RHEL82 NetworkManager[1430]: <info> [1598897276.2175] dhcp4 (eth0): option requested_wpad => '1'
Aug 31 22:07:56 RHEL82 NetworkManager[1430]: <info> [1598897276.2175] dhcp4 (eth0): option routers => '172.18.35.17'
Aug 31 22:07:56 RHEL82 NetworkManager[1430]: <info> [1598897276.2175] dhcp4 (eth0): option subnet_mask => '255.255.255.240'
Aug 31 22:07:56 RHEL82 NetworkManager[1430]: <info> [1598897276.2175] dhcp4 (eth0): state changed extended -> extended
Aug 31 22:07:56 RHEL82 dbus-daemon[891]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service' requested by ':1.23' (uid=0 pid=1430 comm="/usr/sbin/NetworkManager --no-daemon " label="system_u:system_r:NetworkManager_t:s0")
Aug 31 22:07:56 RHEL82 systemd[1]: Starting Network Manager Script Dispatcher Service...
Aug 31 22:07:56 RHEL82 dbus-daemon[891]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Aug 31 22:07:56 RHEL82 systemd[1]: Started Network Manager Script Dispatcher Service.
man command, an interface to the online reference manual, to see command manual
[root@RHEL82 ~]# man ls
Output will show the details of ls command and syntax
[root@RHEL82 ~]# man adduser
Output will show details of adduser command and syntax
That's the end of this post, thank you!
Comments