To learn something we should start from the basics. In this article, we are going to learn the most and useful basic Linux commands or Unix commands. I would suggest you should read this article if you are going to start learning Linux or interested in Linux. This article will give approximately all the basic Linux commands which will make Linux easier to understand. Not only basic you will get much more than that, but I mean to say this article is useful also for experienced Linux admins.
So Let’s have a look at the below Linux commands or Unix commands.
1. Operating System Related Linux Commands
If someone asks which operating system you are currently using or proves that you are using a Linux Operating System then you can use uname command to prove the same.
[root@localhost ~]# uname # To check the Current Operating System Linux
uname command with option -a will give you some more useful information like Computer Name, Kernel Version, Operating System architecture, etc.
[root@localhost ~]# uname -a # To check Linux Kernel Verison and OS details Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Sat Aug 22 16:42:41 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
2. List files and directories we can use ls command.
ls Linux Commands are used to list files and directories in Linux.
[root@localhost ~]# ls # List Files and Directories anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Desktop Downloads Music Public Videos
ls with option -l will give some more details about files and directories like Permissions, Owner of the files and dir’s, created and modifies date and time, etc.
[root@localhost ~]# ls -l # List Files and Directories with more Advance Information total 8 -rw-------. 1 root root 1530 Feb 2 17:39 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 Feb 2 22:05 Desktop drwxr-xr-x. 2 root root 6 Feb 2 22:05 Documents drwxr-xr-x. 2 root root 6 Feb 2 22:05 Downloads -rw-r--r--. 1 root root 1578 Feb 2 21:56 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 Feb 2 22:05 Music drwxr-xr-x. 2 root root 6 Feb 2 22:05 Pictures drwxr-xr-x. 2 root root 6 Feb 2 22:05 Public drwxr-xr-x. 2 root root 6 Feb 2 22:05 Templates drwxr-xr-x. 2 root root 6 Feb 2 22:05 Videos
To check the Hidden files and directories you can use ls command with option -a
[root@localhost ~]# ls -a # To check hidden Files and Directories . .bash_profile .dbus .ICEauthority Public .. .bashrc Desktop initial-setup-ks.cfg .tcshrc anaconda-ks.cfg .cache Documents .local Templates .bash_history .config Downloads Music Videos .bash_logout .cshrc .esd_auth Pictures
Also Read – How to Increase Existing Software Raid 5 Storage Capacity In Linux
3. Directory Related Linux Commands
Directory Related Linux commands are like create, delete, change directories, etc.
To create a directory in Linux you can use mkdir (Referred as Make Directory) command and Remove/Delete a directory use rmdir (Referred as Remove Directory). To go into the directory use cd (Referred as Change Directory) command. To check you are currently in which directory or current directory path we can use the pwd command. Refer to the output below for directory related Linux commands.
[root@localhost ~]# mkdir test # Create a Directory [root@localhost ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Videos Desktop Downloads Music Public test [root@localhost ~]# rmdir test # Delete a Directory [root@localhost ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Desktop Downloads Music Public Videos [root@localhost ~]# mkdir test [root@localhost ~]# cd test/ # Change Directory [root@localhost test]# pwd /root/test [root@localhost test]# cd .. [root@localhost ~]# pwd # Check the current Path /root [root@localhost ~]#
4. Files Operation Linux Commands
File operation Linux commands are used to create, delete, list, display and edit the files.
To create a file we can use the touch command, Follow the output below.
[root@localhost ~]# touch test.txt # Create a File [root@localhost ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates test.txt Desktop Downloads Music Public test Videos [root@localhost ~]# touch test/test.txt # Create a File under Directory [root@localhost ~]# ls test test.txt
One more useful command is there in Linux is a cat, We can use the cat command to create a new file and also to display the content of the file. Follow the output for cat Linux commands.
[root@localhost ~]# cat > test.txt # Create a File using cat Welcome to itsmarttricks.com [root@localhost ~]# cat test.txt # Display the content of the file Welcome to itsmarttricks.com [root@localhost ~]# cat >> test.txt # Append/edit a File Here you can get quality Linux Tutorials [root@localhost ~]# cat test.txt # Confirm the edited text Welcome to itsmarttricks.com Here you can get quality Linux Tutorials
less Linux commands help us to display long files in a scrolling way. you can use up and down the error to forward and backward the file I mean to say scroll the file.
[root@localhost ~]# less /etc/passwd # Display file using less command root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:997:User for polkitd:/:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin unbound:x:997:996:Unbound DNS resolver:/etc/unbound:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin libstoragemgmt:x:996:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin colord:x:995:994:User for colord:/var/lib/colord:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin /etc/passwd
We can see the content of the file using head command. the head command will display first 10 Lines of the file.
[root@localhost ~]# head /etc/passwd # Display file using head command root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
Also Read – How To Create Symlink (Symbolic Link) And Hardlink In Linux
Like head command, We can see the content of the file using the tail command. the tail command will display the last 10 Lines of the file.
[root@localhost ~]# tail /etc/passwd # Display file using tail Command setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:990:985::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin itsmarttricks:x:1000:1000:itsmarttricks:/home/itsmarttricks:/bin/bash
To check the manual page or help page of any command we can use man command.
[root@localhost ~]# man ls # check command help page LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file
To check the already executed command use the history command.
[root@localhost ~]# history # Checking Already executed Commands 1 clear 2 fdisk -l 3 ifconfig 4 fdisk -l 5 init 0 6 uname 7 uname -a 8 ls 9 ls -l 10 ls -a
locate command is used to find the path of any file.
[root@localhost ~]# locate chmod # Find the File Path using locate command /usr/bin/chmod /usr/share/man/man1/chmod.1.gz /usr/share/man/man1p/chmod.1p.gz /usr/share/man/man2/chmod.2.gz /usr/share/man/man2/fchmod.2.gz /usr/share/man/man2/fchmodat.2.gz /usr/share/man/man3p/chmod.3p.gz /usr/share/man/man3p/fchmod.3p.gz
To check the date, time, month, and year of the Linux system use the date command.
[root@localhost ~]# date # Check Date and Time Sat Aug 24 02:40:58 IST 2019
To close the current terminal you can use the exit command.
exit # Exit a Terminal
To clear the terminal screen using the clear command.
clear # Clear the Terminal Screen
To run any command as root we can use the Sudo command. For example, you are logged in as a normal user, and you want to install some package. In that case, you have installed it using the Sudo command.
sudo yum -y install vsftpd # Run a Command as root using sudo
vi command is used as a text editor in Linux Systems.
[root@localhost ~]# vi /etc/httpd/conf/httpd.conf # Edit a file using vi
Also, you can use the nano command as an editor in Linux.
[root@localhost ~]# nano /etc/httpd/conf/httpd.conf # Edit a File using nano
grep command is used as a search command in Linux. For example, here I am searching for word itsmarttricks in file /etc/passwd.
[root@localhost ~]# cat /etc/passwd | grep itsmarttricks # Search for Word itsmarttricks:x:500:500:itsmarttricks:/home/itsmarttricks:/bin/bash
grep command with option -i will allow searching case insensitive words.
[root@localhost data]# cat file1.txt | grep -i mangesh # Search case insensitive Word MangeshDhulap
Network Related Basic Linux Commands
To check the IP Address in the Linux system we can use the command ifconfig. This will list all connected NIC (Network Interface Card) OR LAN Card with loopback LAN.
[root@localhost ~]# ifconfig # Check IP Address ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.1 ether 00:0c:29:36:fd:a7 txqueuelen 1000 (Ethernet) RX packets 511 bytes 31797 (31.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1 (Local Loopback) RX packets 388 bytes 33748 (32.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 388 bytes 33748 (32.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:7d:e7:fe txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
To check the IP Address of a particular NIC execute the command ifconfig <NIC Name>. Refer to the sample output below.
[root@localhost ~]# ifconfig ens33 # Check IP Address of a Particular LAN ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:36:fd:a7 txqueuelen 1000 (Ethernet) RX packets 559 bytes 34677 (33.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
To check the hostname of the Linux system uses the below command.
[root@localhost ~]# nano /etc/hostname # Check Hostname itsmarttricks # OR you can use just hostname command to check the Hostname of the system. [root@localhost ~]# hostname itsmarttricks
Also Read – TAR Command Examples in Linux
To set IP Address OR configure the NIC follow the below steps.
[root@localhost ~]# nano /etc/sysconfig/network-scripts/ifcfg-ens33 # Configure IP Address TYPE=Ethernet BOOTPROTO=none IPADDR=192.168.1.100 # IP Address NETMASK=255.255.255.0 # Subnet Mask GATEWAY=192.168.1.1 # Gateway DOMAIN="itsmarttricks.com" # Domain DNS1=192.168.1.100 # DNS1 IP ADDRESS DNS2=192.168.1.101 # DNS2 IP ADDRESS DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=fbaafa25-25cd-461d-af14-c436b35c5d7e BOOTPROTO=dhcp
To Enable and Disable the NIC use ifup and ifdown command. Refer to the sample output below.
[root@localhost ~]# ifdown ens33 # Disable a LAN Device Device 'ens33' successfully disconnected. [root@localhost ~]# ifup ens33 # Enable a LAN Device Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
To check the network connectivity we can use the command ping. For example, if some website is not working then to diagnose the issue your first step would be to ping to that website. Follow the output below.
[root@localhost ~]# ping itsmarttricks.com # Check Network Connivicity using ping PING itsmarttricks.com (111.118.215.222) 56(84) bytes of data. 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=1 ttl=58 time=8.19 ms 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=2 ttl=58 time=7.68 ms 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=3 ttl=58 time=7.47 ms 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=4 ttl=58 time=8.04 ms 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=5 ttl=58 time=8.74 ms
ping with option -c will ping three times to a particular website.
[root@localhost ~]# ping -c 3 itsmarttricks.com # ping 3 times PING itsmarttricks.com (111.118.215.222) 56(84) bytes of data. 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=1 ttl=58 time=7.84 ms 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=2 ttl=58 time=7.88 ms 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=3 ttl=58 time=7.65 ms --- itsmarttricks.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2011ms rtt min/avg/max/mdev = 7.657/7.794/7.880/0.097 ms
ping with option -i will take an interval between two pings. Refer to the below output where we have three pings which took 3 seconds between each ping.
[root@localhost ~]# ping -c 3 -i 3 itsmarttricks.com # ping 3 time with 3 seconds of interval between each ping PING itsmarttricks.com (111.118.215.222) 56(84) bytes of data. 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=1 ttl=58 time=6.22 ms 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=2 ttl=58 time=7.90 ms 64 bytes from cp-in-9.webhostbox.net (111.118.215.222): icmp_seq=3 ttl=58 time=7.76 ms --- itsmarttricks.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 6016ms rtt min/avg/max/mdev = 6.224/7.297/7.907/0.767 ms
To monitor inbound and outbound networks there is a nice tool available in Linux is netstat.
[root@localhost ~]# netstat # Monitor Network using netstat
To list all TCP and UDP connections we can use the netstat command with option -a.
[root@localhost ~]# netstat -a
There is more network-related Linux commands are available for troubleshooting but will write a separate post for that.
System Utility Related Linux Commands
To Halt OR Shutdown the Linux system we can use the init command with run level 0, Refer to the command below.
init 0 # Shutdown the System
To restart the Linux system use command init with run level 6 OR you can use the reboot command to do the same.
init 6 # Restart the System reboot
To check the Process Status we can use the command ps.
[root@localhost ~]# ps # Check Processing Status PID TTY TIME CMD 3219 pts/0 00:00:00 bash 3488 pts/0 00:00:00 cat 3559 pts/0 00:00:00 cat 4582 pts/0 00:00:00 ps
ps command with option -e and -f will display some more advanced reports the process used by the user i.e. UID, Process ID i.e. PID, Parent Process ID i.e PPID, Time, Command i.e. CMD, etc.
[root@localhost ~]# ps -ef # To check the Processing Status completely page by page you can use below command. [root@localhost ~]# ps -ef | more UID PID PPID C STIME TTY TIME CMD root 1 0 0 09:35 ? 00:00:02 /usr/lib/systemd/systemd --switched-r oot --system --deserialize 21 root 2 0 0 09:35 ? 00:00:00 [kthreadd] root 3 2 0 09:35 ? 00:00:00 [ksoftirqd/0] root 7 2 0 09:35 ? 00:00:00 [migration/0] root 8 2 0 09:35 ? 00:00:00 [rcu_bh] root 9 2 0 09:35 ? 00:00:00 [rcu_sched] root 10 2 0 09:35 ? 00:00:00 [watchdog/0] root 12 2 0 09:35 ? 00:00:00 [khelper] root 13 2 0 09:35 ? 00:00:00 [kdevtmpfs] root 14 2 0 09:35 ? 00:00:00 [netns] root 15 2 0 09:35 ? 00:00:00 [khungtaskd] root 16 2 0 09:35 ? 00:00:00 [writeback] root 17 2 0 09:35 ? 00:00:00 [kintegrityd] root 18 2 0 09:35 ? 00:00:00 [bioset] root 19 2 0 09:35 ? 00:00:00 [kblockd] root 20 2 0 09:35 ? 00:00:00 [md] root 26 2 0 09:35 ? 00:00:00 [kswapd0] root 27 2 0 09:35 ? 00:00:00 [ksmd] root 28 2 0 09:35 ? 00:00:00 [khugepaged] root 29 2 0 09:35 ? 00:00:00 [fsnotify_mark] root 30 2 0 09:35 ? 00:00:00 [crypto] root 38 2 0 09:35 ? 00:00:00 [kthrotld] root 40 2 0 09:35 ? 00:00:00 [kmpath_rdacd] --More--
To kill or stop a process use the kill
Syntax : kill <process ID>
[root@localhost ~]# kill 40 # End a Process
Other Useful ps commands are :
ps -e
ps -eF
ps -ely
Linux Commands to Manage Users & Groups
To create a New User in Linux we can use the command useradd command and set a password for the user to use passwd command.
Syntax :
To add a New User :
useradd <Username>
Set/Reset Password of any User
passwd <Username>
[root@localhost ~]# useradd helpdesk # Create a New User [root@localhost ~]# passwd helpdesk # Set Password for User Changing password for user helpdesk. New password: Retype new password: passwd: all authentication tokens updated successfully.
To confirm if the user created it successfully or not we can check the /etc/passwd file. Refer to the output below.
[root@localhost ~]# cat /etc/passwd | grep helpdesk # Confirm the Newly created User helpdesk:x:1001:1001::/home/helpdesk:/bin/bash
To add a new group use groupadd command.
Syntax : groupadd <GROUP NAME>
[root@localhost ~]# groupadd admins # Create a New Group [root@localhost ~]# cat /etc/group | grep admins # Confirm the Newly Created Group admins:x:1002:
Create a New User and add to a Group. Here I am creating a user named test2 and adding to group admins.
Syntax : useradd -g <GROUP NAME> <USER NAME>
[root@localhost ~]# useradd -g admins test2 # Create a New User and Add in to a Group [root@localhost ~]# cat /etc/passwd | grep test2 test2:x:1003:1002::/home/test2:/bin/bash
To add a Supplementary group to an existing user you have to use usermod command with options -a and -G, Refer to the command below.
Note: Supplementary Group is also referred to as Secondary Group.
[root@localhost ~]# usermod -a -G admins itsmarttricks# Add a Secondary group to a existing User [root@localhost ~]# cat /etc/group | grep admins admins:x:1002:itsmarttricks To add already existing user to multiple groups use below command, Here I am add user test to Multiple groups i.e.admins and coders
[root@localhost ~]# usermod -a -G admins,coders test # Add user to multiple Groups
To confirm the if users are added to respective groups or not we can use the below command.
[root@localhost ~]# cat /etc/group admins:x:1002:itsmarttricks,test coders:x:1003:test
As we can see above admins group has two users i.e. itsmarttricks and test and the coders group has one i.e. test that we have added now.
Also Read – Best Yum Command With Examples A Package Manager In Rhel/Centos/Fedora
Disk Related Linux Commands
To List Available disks and partitions we can use fdisk command with option -l
[root@localhost ~]# fdisk -l # List the disks and partitions Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000c0d0d Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 41943039 19921920 8e Linux LVM
To check all mounted partitions or devices we can use df command with option -h, We can check File System Name, Total Size of the Disk, Used Disk Space, Mount Point by using df -h command. Refer to the output below.
[root@localhost ~]# df -h # List mounted disks & partitions Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 17G 3.3G 14G 20% / devtmpfs 473M 0 473M 0% /dev tmpfs 489M 144K 489M 1% /dev/shm tmpfs 489M 7.1M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 1014M 173M 842M 18% /boot tmpfs 98M 12K 98M 1% /run/user/0 /dev/sr0 4.1G 4.1G 0 100% /run/media/root/CentOS 7 x86_64
Also, you can use the only df command to get disk and partition information.
[root@localhost ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 18208184 3007176 14269424 18% / tmpfs 506040 228 505812 1% /dev/shm /dev/sda1 289293 34673 239260 13% /boot
To check all mounted devices you can use the mount command. Refer to the output below.
[root@localhost ~]# mount sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=484112k,nr_inodes=121028,mode=755) securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
Compression and Archiving related Linux Commands
Compress a file using gzip command with option -c. The extension of the gzip-compressed file is .gz.
Where :
c – To create a gzip file
[root@localhost ~]# gzip -c test.txt > test.txt.gz # Create gzip file
UnCompress/Extract .gz file using command gunzip.
[root@localhost ~]# gunzip test.txt.gz # Extract a gzip file [root@localhost ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Videos Desktop Downloads Music Public test.txt
Compress a file using the bzip2 command with option -c. The extension of the bzip2 compressed file is .bz2.
[root@localhost ~]# bzip2 -c test.txt > test.txt.bz2 # Create a bzip2 file [root@localhost ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates test.txt.bz2 Desktop Downloads Music Public test.txt Videos
UnCompress/Extract .bz2 file using command gunzip.
[root@localhost ~]# bunzip2 test.txt.bz2 # Extract a bzip2 file [root@localhost ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Videos Desktop Downloads Music Public test.txt
Compress a file using the zip command. The extension of the bzip2 compressed file is .zip.
[root@localhost ~]# zip test.txt.zip test.txt # create zip file adding: test.txt (stored 0%) [root@localhost ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates test.txt.zip Desktop Downloads Music Public test.txt Videos
Compress a Directory using the zip command.
[root@localhost ~]# zip data.zip data/ # Compress a Directory using zip command adding: data/ (stored 0%) [root@localhost ~]# ls anaconda-ks.cfg data.zip Documents initial-setup-ks.cfg Pictures Templates data Desktop Downloads Music Public Videos
Extract/Unzip a zip compressed file.
[root@localhost ~]# unzip test.txt.zip # Extract a zip file Archive: test.txt.zip extracting: test.txt [root@localhost ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates test.txt.zip Desktop Downloads Music Public test.txt Videos
tar is used as a backup tool in Linux Systems. The extension of the tar file is .tar. By using the tar command we can archive both files and directories. So follow the below command to archive a directory.
Where :
c – To create a tar file
v – For Verbose
f – For file
[root@localhost ~]# tar -cvf data.tar data/ # Create a tar archive File data/ data/file1.txt data/file2.txt data/file3.txt data/file4.txt data/file5.txt [root@localhost ~]# ls anaconda-ks.cfg data.zip Downloads Pictures Videos data Desktop initial-setup-ks.cfg Public data.tar Documents Music Templates
To extract a tar file.
Where :
x – to extract a file
[root@localhost ~]# tar -xvf data.tar # Extract a tar archive file data/ data/file1.txt data/file2.txt data/file3.txt data/file4.txt data/file5.txt [root@localhost ~]# ls anaconda-ks.cfg data.tar Documents initial-setup-ks.cfg Pictures Templates data Desktop Downloads Music Public Videos
Create a tar archive with gzip compression.
Where :
z – for gzip compression
[root@localhost ~]# tar -czvf data.tar.gz data/ # Create a tar archive with gzip compression data/ data/file1.txt data/file2.txt data/file3.txt data/file4.txt data/file5.txt [root@localhost ~]# ls anaconda-ks.cfg data.tar.gz Documents initial-setup-ks.cfg Pictures Templates data Desktop Downloads Music Public Videos
Extract a tar archived with gzip compressed file.
[root@localhost ~]# tar -xzvf data.tar.gz # Extract a tar archived with gzip compressed file data/ data/file1.txt data/file2.txt data/file3.txt data/file4.txt data/file5.txt [root@localhost ~]# ls anaconda-ks.cfg data.tar.gz Documents initial-setup-ks.cfg Pictures Templates data Desktop Downloads Music Public Videos
Create a tar archive with bzip2 compression.
Where :
j – for bzip2 compression
[root@localhost ~]# tar -cjvf data.tar.bz2 data/ # Create a tar archive with bzip2 compression data/ data/file1.txt data/file2.txt data/file3.txt data/file4.txt data/file5.txt [root@localhost ~]# ls anaconda-ks.cfg data.tar.bz2 Documents initial-setup-ks.cfg Pictures Templates data Desktop Downloads Music Public Videos
Extract a tar archive with bzip2 compressed file
[root@localhost ~]# tar -xjvf data.tar.bz2 # Extract a tar archived with bzip2 compressed file data/ data/file1.txt data/file2.txt data/file3.txt data/file4.txt data/file5.txt [root@localhost ~]# ls anaconda-ks.cfg data.tar.bz2 Documents initial-setup-ks.cfg Pictures Templates data Desktop Downloads Music Public Videos
System Information Related Linux Commands
To check memory-related information we can check /proc/meminfo file.
[root@localhost proc]# cat /proc/meminfo # Check Memory Information
To check disk statistics use the below command.
[root@localhost proc]# cat /proc/diskstats # Check Disk Information
To know the RAID device status you can use the below command.
[root@localhost proc]# cat /proc/mdstat # Check RAID disks information
To check the CPU Information use the below command.
[root@localhost proc]# cat /proc/cpuinfo # Check CPU Information
To check your partition status you can use the below command, the same result you can get by using command fdisk -l command.
[root@localhost proc]# cat /proc/partitions # Check Partition Information major minor #blocks name 8 0 20971520 sda 8 1 307200 sda1 8 2 18631680 sda2 8 3 2031616 sda3
/proc/mounts file will show you all mounted disks or devices. the same result you can get by using the mount command.
[root@localhost proc]# cat /proc/mounts # Check mounted disks information
To check your system’s uptime use the below command.
[root@localhost proc]# cat /proc/uptime # Check System UP Time 2163.72 1783.93
To check the Server version. The same output you will get by uname -a command.
[root@localhost proc]# cat /proc/version # Check OS Information Linux version 2.6.32-642.el6.x86_64 (mockbuild@worker1.bsys.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) ) #1 SMP Tue May 10 17:27:01 UTC 2016
/proc/swaps will show you the swap partition information.
[root@localhost proc]# cat /proc/swaps # Check swap Partition Info. Filename Type Size Used Priority /dev/sda3 partition 2031612 8 -1
you can pull complete system information like disk report, raid device report..etc by using sosreport command.
[root@localhost ~]# sosreport # Pull System Information Report
After creating a new partition you have to mount that partition in /etc/fstab file, then only you can use that partition to store data. fstab referred to as Filesystem Table. Refer to the sample /etc/fstab file below.
[root@localhost ~]# cat /etc/fstab # Overview on Filesystem Table # # /etc/fstab # Created by anaconda on Sun Dec 25 03:05:56 2016 # # 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 # UUID=25f0e1e0-0f54-42f3-b609-d95ce77c783f / ext4 defau$ UUID=51ea032d-f7af-4bb3-8aa9-8fa4428bc362 /boot ext4 defau$ UUID=e9cf7e2b-b945-4074-a63f-b114a3477f42 swap swap defau$ tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
Permissions Related Linux Commands
There are two ways by which we can apply permission to files and directories in Linux. One is the Alphabetical way and the second is the Numerical Way. Below explained Permission related Linux commands with examples.
Alphabetical Permission
Permission in Linux is looks something like this (Divided into 10 Bits) :
– – – – – – – – – –
For our understanding let’s convert all 10 bit’s in Numbers.
– – – – – – – – – –
1 2 3 4 5 6 7 8 9 10
- Out of which First Bit (i.e 1 ) is for File or Folder Identification (–) Where if the first bit is “–” then it’s a file and if it’s “d” then it’s a directory.
- The next three-bit (i.e 2 3 4) is for Owner (– – –) and Owner is Identified as alphabet “u“
- The next three-bit (i.e 5 6 7) is for Group (– – –) and Group is Identified as alphabet “g“
- & Last three-bit (i.e 8 9 10)is for Other (– – –) and Other is Identified as alphabet “o“
First, have look at alphabetical permission :
r – Read
w – Write
e – Execute
To Add permission we have to use the “+” Symbol and for Remove Permission we have to use the “–” Symbol.
We can check the permission of any file or directory by the ls -l command.
Example: 1
In our first example the file named test.txt having permission “-rw-r–r–“, so let’s divide the permission bits of file test.txt.
The first bit is “–” which means it’s a File.
“r w –” – Means Owner having Read & Write Permission because 2nd bit contains r and 3rd bit contains w.
“r – –” – Means Group having Read Access.
“r – –” – Means Others having Read Access.
Now I am going to give full access to Everyone i.e. User, Group & Others by using the chmod command.
[root@localhost data]# ls -l total 0 -rw-r--r--. 1 root root 0 Mar 8 09:56 test.txt # Now File having Permission i.e. -rw-w--w-- [root@localhost data]# chmod ugo+rwx test.txt # Giving Full Access to Everyone [root@localhost data]# ls -l total 0 -rwxrwxrwx. 1 root root 0 Mar 8 09:56 test.txt # Now File having Permission i.e. -rwxrwxrwx
Example: 2
Remove Write and Execute Permission from Group & Others
[root@localhost data]# chmod go-wx test.txt # Change Permission in Alphabatical Way [root@localhost data]# ls -l total 0 -rwxr--r--. 1 root root 0 Mar 8 09:56 test.txt
Example : 3
Add Write Permission to Group & Others
[root@localhost data]# chmod go+w test.txt [root@localhost data]# ls -l total 0 -rwxrw-rw-. 1 root root 0 Mar 8 09:56 test.txt
Numerical Permission
Numerical Permission in Linux as shown below.
4 – Read
2 – Write
1 – Execute
Steps to apply permission to User, Group & Others
Suppose you want to give Full Permission to Owner i.e. Read+Write+Execute to the file text.txt then in Numerical way it’s like
4+2+1 = 7 Read+Write+Execute
& If you would like to give only Read access to Group and Others then it’s like
For Groups – 4
For Others – 4
So the overall command would be chmod 744 test.txt.
Now let’s take some examples so that you can understand properly.
Example: 1
Giving Full access to File test.txt
[root@localhost data]# ls -l total 0 -rwxrw-rw-. 1 root root 0 Mar 8 09:56 test.txt [root@localhost data]# chmod 777 test.txt # Change Permission in Numerical Way [root@localhost data]# ls -l total 0 -rwxrwxrwx. 1 root root 0 Mar 8 09:56 test.txt
Example: 2
Giving Read & Write Permission to all (User, Group & Others).
[root@localhost data]# chmod 666 test.txt [root@localhost data]# ls -l total 0 -rw-rw-rw-. 1 root root 0 Mar 8 09:56 test.txt
Example : 3
Giving Full Access to Owner and Read Access to Group & Others.
[root@localhost data]# chmod 744 test.txt [root@localhost data]# ls -l total 0 -rwxr--r--. 1 root root 0 Mar 8 09:56 test.txt [root@localhost data]# chmod 755 test.txt
We can check the Owner of the file by the ls -l command, Currently, the owner of the test.txt file is the root (Highlighted in Red Color on the output below). We can change the ownership of the file by using the chown command.
Ownership Format :
-rwxr-xr-x. 1 User Group 0 Mar 8 09:56 test.txt
[root@localhost data]# ls -l total 0 -rwxr-xr-x. 1 root root 0 Mar 8 09:56 test.txt
Here I am changing the Ownership of the file from user root to user itsmarttricks.
[root@localhost data]# chown itsmarttricks:eitsmarttricks test.txt # Change Ownership of a File [root@localhost data]# ls -l total 0 -rwxr-xr-x. 1 itsmarttricks itsmarttricks 0 Mar 8 09:56 test.txt
Now let’s change the ownership of the directory with all its content Recursively. To do so we have to use chown command with option -R.
[root@localhost data]# ls database/ file1.txt file2.txt file3.txt file4.txt file5.txt [root@localhost data]# ls -l database/ total 0 -rw-r--r--. 1 root root 0 Mar 8 10:01 file1.txt -rw-r--r--. 1 root root 0 Mar 8 10:01 file2.txt -rw-r--r--. 1 root root 0 Mar 8 10:01 file3.txt -rw-r--r--. 1 root root 0 Mar 8 10:01 file4.txt -rw-r--r--. 1 root root 0 Mar 8 10:01 file5.txt [root@localhost data]# chown -R itsmarttricks:itsmarttricks database/ # Change Ownership of Directory Recursively [root@localhost data]# ls -l total 4 drwxr-xr-x. 2 itsmarttricks itsmarttricks 4096 Mar 8 10:01 database -rwxr-xr-x. 1 itsmarttricks itsmarttricks 0 Mar 8 09:56 test.txt [root@localhost data]# ls -l database/ total 0 -rw-r--r--. 1 itsmarttricks itsmarttricks 0 Mar 8 10:01 file1.txt -rw-r--r--. 1 itsmarttricks itsmarttricks 0 Mar 8 10:01 file2.txt -rw-r--r--. 1 itsmarttricks itsmarttricks 0 Mar 8 10:01 file3.txt -rw-r--r--. 1 itsmarttricks itsmarttricks 0 Mar 8 10:01 file4.txt -rw-r--r--. 1 itsmarttricks itsmarttricks 0 Mar 8 10:01 file5.txt
Package installation and Service-Related Linux Commands
Install a package using rpm commands.
Where :
i – Install a Package
v – For Verbose
h – for # output
# rpm -ivh dhcp-3.0.5-23.el5.x86_64.rpm # Install Package using rpm command warning: dhcp-3.0.5-23.el5.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 37017186 Preparing... ########################################### [100%] 1:dhcp ########################################### [100%]
Install a Package using yum command. In the case of yum when we try to install a package, it retrieves the package information from the yum repository package manager server and makes it available to us.
[root@localhost ~]# yum -y install vsftpd # Install a Package using yum command Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirror.nbrc.ac.in * extras: mirrors.vinahost.vn * updates: mirrors.vonline.vn Resolving Dependencies --> Running transaction check ---> Package vsftpd.x86_64 0:2.2.2-21.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: vsftpd x86_64 2.2.2-21.el6 base 155 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 155 k Installed size: 340 k Downloading Packages: vsftpd-2.2.2-21.el6.x86_64.rpm | 155 kB 00:02 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : vsftpd-2.2.2-21.el6.x86_64 1/1 Verifying : vsftpd-2.2.2-21.el6.x86_64 1/1 Installed: vsftpd.x86_64 0:2.2.2-21.el6 Complete!
Start/Restart/Stop a Service a Service.
[root@localhost ~]# /etc/init.d/vsftpd start # To start a Service [root@localhost ~]# service vsftpd status # Check the Service status vsftpd (pid 3168) is running... [root@localhost ~]# /etc/init.d/vsftpd restart # To Restart a Service [root@localhost ~]# /etc/init.d/vsftpd stop # To Stop a Service
Start a Service at startup.
chkconfig --level 35 vsftpd on # Start a Service on Startup
We tried to include all possible basic Linux commands. That’s all, In this article, we have explained Complete Unix Commands And Basic Linux Commands. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.