DevOps Basic Terminologies: SSH | Linux commands

tudip-logo

Tudip

13 June 2018

SSH:

I am pretty sure you definitely heard about `SSH`, if not then no problem we will learn it together.

What is SSH: SSH allows you to access the remote system securely. You can access the remote system in two ways, first one is using a password and the second one is using a private and public key.

SSH using password: In this method, we just type the username and hostname or system’s IP address. Let’s go and do it in practical.

  • First, open the terminal.
  • Type username and system’s IP:

`ssh username@ipaddress`

For example:

ssh lt-152@192.168.249.152

It will ask you for the password, enter the correct password, and you are there. Now you can access the remote system’s directories and files on your system. You can copy the file from the remote system or vice versa. You can perform any operations as you want to perform. Using password authentication is a bad way and not secure type of authentication. If someone knows the password of the machine then they can access your remote instance. To avoid this type of security risks we have another type of authentication which you will allow to access the system without password.

SSH based on keys: In this method, you will have to generate the public and private keys to access the remote system.

  • A `public key` that you have to copy on the remote system to access the system.
  • A `private key` that you don’t have to share this with anyone.

You can create your key pair using following commands:

`ssh-keygen -t rsa`

After you run this command, you’ll get asked the following questions:

  • Enter file in which to save the key (/home/<your-home>/.ssh/id_rsa)
  • Enter file in which to save the key (/home/<your-home>/.ssh/id_rsa)
  • Write same passphrase again

Now you have to copy this public key on the remote machine. You can do this by running the below command:

`ssh-copy-id root@192.168.249.152`

Now you can access the system without password.

ssh root@192.168.249.152

Now you have done the configuration to communicate systems so now you can perform the operations on the remote system from your system. For Example:

Copy Files Between Computers:

scp /tmp/my_file root@192.168.249.152:/mnt/my_file # From your computer to 192.168.249.152 scp root@192.168.249.152:/mnt/my_file /tmp/my_file # From 192.168.249.152 to your computer.

File Permission in Linux:

It is essential to assign permissions to users for a particular file. By using this Linux security feature you can restrict a user to perform an operation based on the permissions assigned to the particular user.

Basically, there are three basic permissions:

  • Read(r): The Read permission refers; a user can read the content of the file.
  • Write (w): The Write permission refers; a user can modify the file or directory.
  • Execute(x): The Execute permission refers; a user has permission to execute the file.

You can check the permissions of the particular file by using `-l` flag of `ls` command.

For example:
``
jitesh@jitesh-Lenovo-G500s:~$ ls -l
total 120
drwxr-xr-x 2 jitesh jitesh 4096 May 1 05:19 Desktop
drwxrwxr-x 3 jitesh jitesh 4096 Jan 9 13:38 docker_practice
drwxr-xr-x 2 jitesh jitesh 4096 Aug 27 2017 Documents
drwxr-xr-x 3 jitesh jitesh 4096 May 21 01:23 Downloads
-rw-r--r-- 1 jitesh jitesh 8980 Aug 27 2017 examples.desktop
drwxrwxr-x 2 jitesh jitesh 36864 Aug 27 2017 GitHub_Totorial
drwxrwxr-x 3 jitesh jitesh 4096 Apr 9 14:44 hellonode
``

Here you can see permission of the Desktop directory “drwxr-xr-x”

Let’s discuss this in details:

d rwx rwx rwx

The first digit can be changed according to the file and directory.

d -> means you are viewing the document that is a directory.

– -> Means you are viewing the document that is the file.

After that first “rwx” permission belongs to “User”. Second “rwx” permissions belong to “Group”. Third “rwx” permissions belong to “Other people”. You can give the specific permission to the file or directory as per the requirements.

For example:
You can give the permission to the file something like:
 chmod 754 test.js
You can give the permissions to the directory something like:
 chmod -R 754 test

7, 5, 4 represents the individual permissions for (in this order) user, group, other. There are four digits which represent the permission:

4 -> stands for “read”.

2 -> stands for “write”.

1 -> stands for “Execute”

0 -> stands for “no permission”

For example:
 chmod -R 777 test
Where:

7 -> user has rwx permissions.

7 -> Group have rwx permissions.

7 -> other people have rwx permissions.

Basic Linux Commands:

  • cd:

It stands for change directory, by using this command you can jump from current directory to another directory.

For example

  1. cd test
  • pwd:

It stands for the present working directory. It will show you current working directory.

  • locate:

It will show you where the file is located in your system. It works same as “search” command in Windows.

For example: locate filename

  • ls:

List command is used to check the files in the directory. This command has two option “-l” and “-a”.

For example:

  1. ls -l :It will show you the file permissions.
  2. ls -a : It will list all the file in the current directory(hidden files also).
  • mkdir:

It will create the new directory.

For example:

  1. mkdir test
  • rm

You can use the rm command to remove the file or directory from your system.

For example:

rm filename

  • cp:

This command is used to copy the data from one location to another location.

cp source dest
  • ps aux:

It will show you all the processes that currently running in your system.

  • touch:

This command is used to create an empty file. It can be anything, from an empty “.txt” file to an empty js file.

For example

  1. touch test.txt
  •  man & –help:

To know more about command and how to use it, use the man command. It shows the manual pages of the command.

For example:

  1. “man cd” shows the manual pages of the cd command.
  • mv:

This command is used to move the files through the command line. You can also use this command to rename the file.

For example:

Let’s say you have a file name “demo.txt” and you want to rename this file to “test.txt” then you can do something like:

“mv demo.txt test.txt”

  • echo:

The “echo” command is used to print the data on the terminal. Also, you use this command to move the data usually text into a file.

For example:

If you want to create a new text file or add to an already made text file, you just have to type in,

“echo hello, my name is Jitesh >> test.txt”.

  • cat:

You can use this command to display the content of this file on the terminal without open that file.

“cat filename”
  • nano, vi:

You have seen mostly users use nano and vi text editors in the Linux to open the file. This editor comes as pre-installed in the Linux. Vi editor is good and simple than nano editor.

For example:

If you want to create a file named “test.txt”, you can create it by doing something like:

“nano test.txt”

“vi test.txt”

  • sudo:

Sudo is a mostly used command in the Linux command line, sudo stands for “SuperUser Do”. If you want to run any file in the Linux with administrative or root privileges, you can use this command.

For example:

If you want to modify any file which requires root permission, you can use sudo to open the file as a root

“sudo vi nginx.conf”

  • df:

By using this command you can check the available space in each of the partitions in your system

For example:
``
lt-152@lt152-Vostro-3559:~$ df

Filesystem 1K-blocks Used Available Use% Mounted on udev 3905456 0 3905456 0% /dev tmpfs 786356 2852 783504 1% /run /dev/sda1 953348996 23639172 881259420 3% / tmpfs 3931772 201168 3730604 6% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 3931772 0 3931772 0% /sys/fs/cgroup tmpfs 786356 64 786292 1% /run/user/1000 “

  • du:

If you want to check the disk usage of a file in your system then you do it by using this command.

“du hello.py”

For example:
``
lt-152@lt152-Vostro-3559:~/jitesh$ du hello.py
4 hello.py
``
You can also check the disk usage of a directory by using du.
``
lt-152@lt152-Vostro-3559:~/jitesh$ du test

16 test “

  •  tar:

The tar command is used to compress and uncompress different types of tar archives like .tar, tar.gz, tar.bz3, etc.

For example:

You can create the .tar archive:

“tar -cvf myfile.tar test”

To untar a tar archive:

“tar -xvf redis-4.0.9.tar.gz”

  • zip, unzip:

You can use this utility to compress files into a zip archive, and unzip to extract files from a zip archive.

For example:

You can create the .zip archive:

zip myfile.zip test”

To unzip a zip archive:

“unzip myfile.zip”

  • uname:

You can check the name of the Linux distro you are running. By using the command “uname -a” you can check the most of the information of the system. The output of this command shows the kernel release date, version, processor type, etc.

  • chmod:

The “chmod” stands for change mode. It is generally used to change the permission of the files or directories. Also, it is used to make a file executable.

For example:

To give the permissions:

“chmod 777 demo.sh”

To make a file executable:

“chmod +x test.sh”

  • hostname:

You can check the name of the system by using this command. You have to just type in, “hostname” on your terminal. Basically, it displays your hostname and IP address.

For example:

To check hostname you can do something like: “ lt-152@lt152-Vostro-3559:~$ hostname lt152-Vostro-3559 “ To check the IP of the system on your network, you can do something like: “ lt-152@lt152-Vostro-3559:~$ hostname -I 192.168.249.152 172.17.0.1 172.18.0.1 “

  • ping:

You can use the ping command to check your connection to the server. According to the Wikipedia, “Ping is a computer network administration software utility used to test the reach ability of a host on an Internet Protocol (IP) network”.

For example:

  1. ping google.com

You can also use this utility to check the internet connection is active.

Request a quote