Basic Linux Commands

by Tom | Posted on July 2, 2016, 1:01 p.m.

Introduction

In this tutorial we are going to learn about the basic and most useful commands of linux. In the following tutorial I am gona to use a Linux server(Ubuntu 2016 LTS version), But not limited this one only. These are the basic commands of linux, you can use this on any linux machine.

Prerequisites

For testing the linux commands you must have a linux installed computer, Here I am using a ubuntu server 2016 LTS. If you are a windows or mac user you can install VM Ware or Virtual Box to try these commands, Since we are only dealing with the terminal, you can install the minimal version of linux on your Virtual machine. But it does not have Graphical Interface You limetted to use KeyBord Only.

Assumptions


Hope you should have some Intrest on learning new stuff.

Lets Start

Changing Directory( cd )

Changing working directory is a primary need of any computer user. We all are familial with double clicking, drag and drop,,,all that stuff,,, But in terminal you have a two letter command for that.cd

Home directory

cd ~

root directory

   cd /

any other directory

    cd name_of_the_folder

Listing everything in a directory ( ls )

    ls
    ls -l

Make a new directory ( mkdir )

   mdkir folder_name
    sudo mkdir /folder_name

How to know current directory( pwd )

    pwd

How to view all running tasks?

    top

How to take snapshoot of all running tasks?

    ps -ax

How to filter a task from all tasks using grep ?

There a some situations where we want to find a perticular task/Id from above command(ps -ax), here is an example of a node script trigred by rc.local onstartup.

    ps -ax | grep discover_ble_devices.js

How to install an package or software( sudo apt-get install )

    sudo apt-get install django
    sudo apt-get install zip
    sudo apt-get install mysql

Updating packages( pwd )

    sudo apt-get update

How to uninstall an package or software( remove )

content will be updated soon

Start, stop and restart a program

    sudo serviece nginx start
    sudo serviece nginx stop
    sudo serviece nginx restart

Listing partitions( sudo fdisk -l )

    sudo fdisk -l

All physical hard disk information( sudo lshw -C disk )

    sudo lshw -C disk

How to know the disk Space available( df or pydf )

    sudo apt-get install pydf
    pydf
    or
    df
    or df -Th /dev/sda1

How to create a new partition( fdisk )

Use fdisk -l for listing all avilable disks, Then you will get a list of avilable disks and the partitions inside each disk. eg: "/dev/xvda" is the disk and "/dev/xvda1" is the partition. If you want to add new partition in a newly attached disk(Additional storage disk on AWS and Azure are use cases), Let the new disk be /dev/sdb (usualy suffix bfor secod disk or may be difrent in your case, verify using fdisl -l comand). For crating new partition use fdisk command, sudo fdisk /dev/sdb, now you will enter into fdisk utlity. enter m for help, n for new partition. newxt you will be asked for primary or extended paertition. enter p for primary, next is the partition no choose default value 1 (if you need more partitions then it can aso be done once you uderstand the procedure). next is the first and last sectors choose the default value(If you need only one partition). final step is to give write command by entering w. This will execute the new partition table. It can be varified by using the command fdisk -l Eg:

    sudo fdisk -l
    sudo fdisk /dev/sdb
    welcome to fdisk (util-linux 2.27.1).
    Command (m for help): n
    Partition type
    p   primary (0 primary, 0 extended, 4 free)
    e   extended (container for logical partitions)
    Select (default p): p
    Partition number (1-4, default 1): 1
    Command (m for help): w
    The partition table has been altered.
 

 

How to format a partition( fdisk )

First step is to find the exact partition name using list command then use mkfs command for formating

    sudo fdisk -l
    mkfs -t ntfs /dev/sdb1

How to mount a partition or disk( mount )

First step is to create a directory on the root to which the partition to be mount using sudo mkdircommand.

    mkdir /mydata
    mount /dev/sdb1 /mydata

Compressing files in linux( zip )

    sudo apt-get install zip
    zip -r name.zip /directory

Decompressing files in linux( unzip )

    sudo apt-get install unzip
    unzip file.zip -d destination_folder

content will be updated soon

Edicting files in linux( nano )

    nano file.cof
    nano file.txt

Browse on internet in linux( curl )

    curl www.google.com

Downloading files from internet( curl )

    wget http://www.buildfromzero.com/media/3/TM4C123.jpg

Deleting files( rm )

    rm buildfromzero.png

Deleting folder( rm )

    rm -r -f folder_name

How to stop and restart mysql server?

    sudo /etc/init.d/mysql stop
    sudo /etc/init.d/mysql restart

for changing MySQL data folder try this tutorial

Listing SSH Keys on your computer

The following command will list all the SSH keys saved on your default directory (~/.ssh)

ls -al ~/.ssh

more details

How to copy contents of a file into clipboard?

The following command will copy the contents of the file github_rsa.pub, This is a very useful command while playing with ssh keys, an alternative for this is manual copying using cat command.

pbcopy < github_rsa.pub

Generating a new SSH key

    ssh-keygen -t rsa -b 4096 -C "mail@example.com"
                    or
    ssh-keygen -t rsa -b 2048 -C "mail@example.com"

Now you will be asked for the location to save the file. now enter your location

    Enter file in which to save the key (/Users/Tomvictor/.ssh/id_rsa): /Users/Tomvictor/.ssh/mysshkey

Then you will be asked for entering a phrase, enter your phrase or simply press enter. 

Adding new SSH key to SSH Agent

    ssh-add -K ~/.ssh/GitHub

The above command will add the newly generated SSH key(GitHub and GitHub.pub) to the ssh-agent, this step is must inorder to establish connection over SSH

Login to remote server using SSH

SSH stands for secure shell. It's running on port 22. It is commonly used for managing a remote server. SFTP protocol is also running on the same port 22. SSH will add an extra level of security while communicating to the remote server. The communication can be achieved either using a password for authentication or authentication via SSH key. The SSH key method is more secure and safer way. SSH key is two files one is private and other public. The public key will be stored on the remote server while the private key should be kept on the local computer. Since the SSH key is the only way to authenticate it should be kept in a secure and secret location. Use the above method to generate SSH keys. the public key will have a .pub extension.

    ssh username@Remote_IP
            or
    shh username@Remote_domain_name
            or
    ssh -i location_of_private_key/privatekey username@Remote_IP_or_Domain_name

Conclusion

So we discussed the commonly used Linux commands, its usage, examples... If you have any specific doubt please mention on the comment section below. Hope This tutorial is helpful for you...