Linux-All Commands With Meaning.
What is Linux in simple word?
Linux is an operating system, just like Windows. The main difference is that Windows is user-friendly and UI-based, which means you can use the mouse to click and do your work. Linux, on the other hand, is mostly command-line based, which means you have to type commands to interact with the OS and perform tasks. There are some Linux flavors that have a graphical user interface, but the command line is still very powerful and useful.
For example, if you want to see the contents of a folder named “vinayak” in Windows, you can double-click on it and open it. In Linux, you can use the command “cd vinayak” to change your current directory to “vinayak”, and then use the command “ls” to list the files and folders inside it.
Basic Linux Commands
Linux command | Meaning |
ls | Lists all files and directories in the present working directory |
ll | ll= long list To show all files with details. |
ls – R | Lists files in sub-directories as well |
ls – a | Lists hidden files as well |
ls – al | Lists files and directories with detailed information like permissions, size, owner, etc. |
ls -ltr | latest file will show at last in list |
ls -lrtha | a>> all files (including hidden files) l>> long list. r>> reverse order. T> h> human redable |
ls li | l>> long. list i>> inode |
mkdir madhu | mk= Make. dir=directory. To create new directory (folder) with directory name madhu. |
rmdir madhu | rm=remove. dir=directory. To remove directory (folder) madhu. |
rm -vrf filename | v >> verbose (to see which file is deleting first) f >>. forcefully. Deletes a file |
cd | c=change. d=directory. change to directory. |
touch | create empty files. |
cat > filename | create new file and writes the content and ctrl+D to save content into the file. |
cat >> filename | >> means append. To add more lines in to the same file without deleting the previous contents. |
cat filename | display content of file. |
cat file1 file2 > file3 | Joins two files (file1, file2) and stores the output in a new file (file3) |
pwd | present working directory. To show present working directory. |
cp | copy a file or directory. |
mv file “new file path” | moves a file or directory. Moves the files to the new location. |
mv filename new_file_name | Renames the file to a new filename |
head | display first 10 lines of a files. |
tail | display last 10 lines of a file. |
tac | display file content lines in reverse. |
more | similar to cat and here we can display large content by using ENTER, SPACEBAR. |
id | display id of user/group. |
clear | clear the screen. |
vi | text editor to write programs of text. |
grep | filter to search given pattern in the file content. |
diff | compares the content of two different files |
ping | check the connectivity status of server. |
sudo | Allows regular users to run programs with the security privileges of the superuser or root |
man | Gives help information on a command |
history | Gives a list of all past basic Linux commands list typed in the current terminal session |
clear | Clears the terminal. You can press Ctrl+L. |
pr -x | Divides the file into x columns |
pr -h | Assigns a header to the file |
pr -n | Denotes the file with Line Numbers |
lp -nc | Prints “c” copies of the File |
lp -dlpr -P | Specifies name of the printer |
apt-get | Command used to install and update packages |
mail -s ‘subject’ -c ‘cc-address’ -b ‘bcc-address’ ‘to-address’ | Command to send email |
mail -s “Subject” to-address < Filename | Command to send email with attachment |
date | to display the date |
touch vin- | To create file with date appended in the name |
cal | To show the calendar |
who | to show who logged in and from how long with time |
last | to show all users who logged in succesfully and from how long with time |
lastb | b >> bad log in's means it will show who tried to log but not loged in. |
w | who is loged in and what they are eecuting or doing. |
audit.log | file will show what has done and who has done. |
Some more Commands
SCP to copy file from aws's ec2 server to > local server's current directory. | ||
Vinayak@vinayak-desktop:~/Downloads$ scp -i "terra-ubuntu-server-key.pem" ubuntu@ec2-54-89-77-131.compute-1.amazonaws.com:/home/ubuntu//terraform-practice/ . | scp= secure copy, | : = for mentioning the path on the server where file is located which we need to copy. |
SCP to copy file from > local server to >> aws's ec2 server | Note:> Make sure you have the write permission (o+w) on the directory on ec-2 server | |
scp -i "terra-ubuntu-server-key.pem" ./vin.txt ubuntu@ec2-54-89-77-131.compute-1.amazonaws.com:/home/ubuntu/vin/ | Here >> For directry vin located at > /home/ubuntu/vin/you must have write permissin to others. (chmod o+w /home/ubuntu/vin) |
Only Important commands | ||
Linux commands | Meaning | Details |
sudo tail -f /var/log/nginx/access.log | To check nginx live logs | |
sudo tail -100 /var/log/nginx/access-new.log | grep executeapis | To grep executeapis from last 100 lines of the log file | If we specify the -n option, grep will prefix each matching line with the line number. -i option to perform a case-insensitive match. Refer>>> https://www.computerhope.com/unix/ugrep.htm |
sudo tail -f /var/log/nginx/access.log | grep -v "notifications" | -v >> exclude | To grep live logs by excluding the word notifications. |
tail -n 4 long.txt | Last 4 lines | To view only the last four lines of file long.txt |
free | Free memory. (RAM and SWAP) | The free command gives information about used and unused memory usage and swap memory of a system. By default, it displays memory in kb (kilobytes). Memory mainly consists of RAM (random access memory) and swap memory. Options -b to get outputs in bit. -k = show in Kb. -m=MB. -g==GB. -w= Buffer and cache shows separate. -lm = (l)-list down the lowest memory utilization in (m)Mb. |
df -Th | disk free | To show the disc partition details. -h=human readable format. -T=Filesystem type. |
du -h | disk usage | Disk usage of current diretory -h=human readable format. |
du -sh | sort -rn | head | >>> To print the highst disc using files at top | |
aide | Advanced Intrusion Detection Environment (aide) | To show who modified the file, what is modified and when. |
grep "root" /etc/passwd | Global Regular Expression Print (grep) | To find the string of characters from the particular file. |
chattr +i filename | Change attributes | To protect any file from accidental delete. |
chmod +rwx | Change mode | To change file permissions. +rwx to add the read write and execute permissions. |
./ file name | Run script | To run a script file |
apt, yum & pacman | Package managers | Used to install, update, and remove the softwares. |
yum command will replace by dnf command | ||
apt-get is a command line tool in Debian and Debian-based Linux distros that you use to install and manage packages. | ||
apt install httpd | To install Apache server (httpd) on Ubuntu OS. | Used for Debian-based (Ubuntu, Linux Mint) OS to install packages/softwares |
apt-get install Nginx | To install Nginx server. | |
yum install httpd | To install Apache server (https) on Redhat based OS. | For Red Hat-based (Fedora, CentOS) |
yum info tree | To find out package for any commands | |
sudo apt-get update | To check packages that can be updated. | It'll figure out what the latest version of each package and dependency is, but will not actually download or install any of those updates. |
https://www.freecodecamp.org/news/sudo-apt-get-update-vs-upgrade-what-is-the-difference/ | ||
apt list --upgradable | To see which packages can be upgraded | |
sudo apt-get upgrade | Only upgrades what it can without removing anything. | |
sudo apt autoremove | To remove unnecessary packages. | |
pacman -S httpd | For Arch-based (Manjaro, Arco Linux) | |
service nginx start or systemctl start nginx | To start the Nginx. systemctl is for Redhat 8+ & service strat is for older versions. | Note:- When we install packages using yum install we need to start the service using systemctl cammand. When we use apt in ubuntu to install packages, the service will start by default. |
chkconfig nginx on or | To enable the nginx service. | chkconfig= For older versions and systemctl for latest versions of OS. |
ps | Process show | To view the running process in the current shell session. |
ps fax | grep nginx | To find the process running related to Nginx. | |
lsof -i -P | List Of Open File. -i >> select internet files | It will list all open files with port number, node name and PID details |
kill pid | kill with process ID | To kill the processes by entering either the PID (processes ID) or the program’s binary name. |
head -n 5 long.txt | First 5 ines | To view only the first five lines of file long.txt |
vim file-name | To open and edit the file. | Press ESC and navigate to the lines you want to comment out. >> Press CTRL + V to enable Visual Mode. >> Using the up and down arrow key, highlight the lines you wish to comment out. >> Once you have the lines selected, press the SHIFT + I keys to enter insert mode. >> Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines. |
In vim press esc and type | 1) :%s/^#/ | Navigate to the line you wish to comment out and press CTRL + V to enter Visual mode. >> Use the up and down arrow keys to highlight the lines you wish to uncomment. Once selected, press x to remove the comments. |
vim editor's Short cuts | Working >>>> | For indentation try >> Press "SHIFT + v" to enter VISUAL LINE mode. |
whoami | Print current user name | To print the Current user name |
whatis command name | To prints a single-line description of any other command | |
which command name | To show the full path of the shell of commands. | |
wc | word count | To show the “word count,”. It returns the number of words in a text file. |
uname -a | Unix name | Prints the operating system's all information |
netstat | network statistics https://www.supportsages.com/understanding-the-netstat-command/ | netstat command to list all open ports, including those of TCP, UDP, which are the most common protocols for packet transmission in the network layer. |
To find out which perticular process running on which perticular port >> We need to use netstat -anp command and then we need to use ps -ef | grep 457 (457> is PID) | ||
netstat -tulnp | network statistics | Options== -l= tells netstat to only show listening sockets.. -n= instructs it to show numerical addresses. (-n). -t= tells it to display tcp connections.. -u= UDP ports. -p= enables showing of the process ID and the process name. |
ss -tulnp | show socket / socket statistics | Just to ensure that we are getting consistent outputs, let’s verify this using the ss command to list listening sockets with an open port. |
ss -punta | grep "51903" | wc -l | ss is used to dump socket statistics. It allows showing information similar to netstat. It can display more TCP and state information than other tools. | |
curl ifconfig.me | to get IP | To get our own machine's public IP address. |
cp -i source destination | copy interactive mode | -i for interactive mode= it will check file is already present or not. If file is present then it will ask whether to overwrite it or not?. |
Network troubleshooting Commands | Linux troubleshooting Commands Video duration 01 H :40 min :41 sec | |
ping | to check the network connectivity. | to check the network connectivity. |
yum install bind-utils | Package for network troubleshooting Commands | bind-utils package has many commands which are used for network troubleshooting. Such as dig, nslookup, host etc commands will get installed with this package. |
host www.google.com | To print ip address | To print the ipv4 & ipv6 address for google.com |
host -a www.google.com | print all details with ip address | |
ssh admin@ip | to check connection | |
nmap -A ip address | To find out port open on the remote server. | It is used for hacking purpose. |
Fingerprints are used to provide authenticity between two servers | To provide authorizaton that what the server is and which server is trying to coneect. | |
mtr ip address of ec2/vm | To show details of packets sent and latency % lost packets | To check which firewall is blocking connection. yum install mtr= to install the package for the mtr command. |
nslookup google.com | To check latency | To check latency between our machin and google server. |
dig google.com | More details with header | To check more details of network connections with server ip, time etc. |
tracepath www.google.com | To find out where our trafic went down. | tracepath=for linux 7+ versions and traceroot=for older versions. Tracepath is used to find out at exact what point our trafic went down. Whichever is the last hope that ip address should be our own ip. or if its other third party ip then that third party vendor blocking the connection. no reply in result means that perticular isp or router hiding their identity. |
Process to copy or replace key file | ||
ssh-keygen -t rsa | ssh key generation | To generate ssh keys, -t is for type and type is rsa. The result will show where identification and key has been saved in with location. The public key will be saved to /root/.ssh/id_rsa.pub |
cat /root/.ssh/id_rsa.pub >> /home/ec2-user/.ssh/authorized_keys | To append keys into ec2-user's authorized keys. | |
cat .ssh/id_rsa | Open file select the entire key and then go to cd Downloads/ then create one file. | |
vi vin.pem | Paste the entire copied content and save it. | |
chmod 400 vin.pem | To change the permissions. | We can import keys in aws. Go to key pairs >> action >> import key >> give name to key and then go to the server and open file from location cat /home/ec2-user/.ssh/authorized_keys >> copy the appended contents starting from ssh-rsa till the end and paste it in to blank box and >> click on import key. We can use this key for creating new ec2 machine. |
ssh -i "vin.pem" ec2-user@all copied command from aws | We have successfully replaced the key file with new one and logged in to the server using new .pem file. | |
OS troubleshooting Commands | We need to understand the linux bootup process in order to troubleshoot the OS. | Select the ec2 instance >> Actions >> Instance settings >> Get system logs. >> We can check what happen when linux/ OS boots up. |
up to 1H 22 Min | ||
sudo dmesg > dmesg.log | To show the contents of the boot log | Use the command dmesg to show the contents of the boot log. This is a good way to find errors at startup |
cat dmesg.log | grep error | To show the error word in dmesg.log file | |
lspci | Lists all PCI devices. | lspci |
lsusb | Lists all USB devices. | lsusb |
lsmod | Shows the status of modules in the Linux kernel. | |
ufw | Uncomplicated Firewall | Uncomplicated Firewall (UFW) is a program for managing a netfilter firewall designed to be easy to use. It uses a command-line interface consisting of a small number of simple commands, and uses iptables for configuration. UFW is available by default in all Ubuntu installations since 8.04 LTS. |
ufw status | >> To check status of the firewall. | |
ufw enable | >> To enable the firewall. | |
date | to display the date | |
touch vin- | To create file with date appended in the name | |
Port opening Link | https://www.digitalocean.com/community/tutorials/opening-a-port-on-linux | |
User management Lokendra | ||
rpm | Redhat package manager | |
rpm -ql | ||
less filename | To show file page by page. To exit need to press q to quit. | |
more filename | Work same like less To show file page by page but no need to quite. | |
vim /etc/default/useradd | To change default user's parameters like home directory. SHELL etc. Whenever we add new user the default SHELL and Directory will be defined by this (/etc/default/useradd) file | |
cd /etc/skel | If we add any file or folder in this directory then that all files and directory will get copied to user's home directory whenever we create new user after modifying this directory. | |
history | To Show command history | |
export HISTTIMEFORMAT='%F %T ' | temp >> history format | to set HISTTIMEFORMAT variable temporarily to current user. |
echo 'HISTTIMEFORMAT="%F %T "' >> ~/.bashrc | permanent >> history format | Append this HISTTIMEFORMAT variable to .bashrc or .bash_profile file to make it permanent for every user. |
echo 'HISTTIMEFORMAT="%F %T "' >> ~/.bash_profile | ||
history -i | To show history with dat in zsh shell. | |
iptables -I INPUT ! -s 192.168.1.2 -p tcp --dport ssh -m state --state NEW,ESTABLISHED,RELATED -j REJECT | https://www.golinuxhub.com/2014/03/how-to-allowblock-ssh-connection-from/ | Block ssh connection from all the host EXCEPT 192.168.1.2 |
Linux command you must know:--- | ||
uname -o | To know the OS type: | |
uname -m | To know the CPU architecture: | |
uname -r | To check the kernel version: | |
cat /etc/os-release | To get the OS name, release, version: | |
lshw | To list the system hardware: | |
lscpu | To get the CPU details: | |
free -h or free -m | To check system memory: | |
vmstat -S m | To check the virtual memory stats: | |
echo 3 > /proc/sys/vm/drop_caches | Free memory cache, dentries and inode (with root): | |
ps aux --sort=-%mem | To print the process specific memory utilizations: | |
apt search <package name> | To search packages for installation: | |
e.g.: apt search python-boto | ||
sudo apt-get install <package name> | To installed package: | |
sudo apt-get remove <package name> | To uninstall package: | |
df -kh | To list the mounted disk drives: | |
mkdir -p <directory path e..g /mount-vol> | To mount the volume: | |
sudo mount <src path> <above created dir path> | (create the directory first to mount volume) | |
sudo du -a /dir/ | sort -n -r | head -n 5 | To list biggest files from directory (biggest 5): | |
find <dir path> -name <filename> -print | Find the file (search for a file): | |
find /var -name app.log –print` | e.g. to find app.log in /var directory | |
find /var -type f -print | xargs grep <search test> | Search the text string in a directory and print filename containing that string: | |
grep -rIn <search text> <directory path> | File the text string from a given directory: |