Vi Editor, Networking commands essentials for Devops
What you will Learn:
Vi Editor Introduction
Vi modes
Switch from read-only to insert mode in Vi
Save the file in Vi
Quit the file without saving
Switch from insert mode to read-only mode
Move the cursor to beginning of the file (gg)
Move the cursor to end of the file (Shift + g)
Move the cursor to end of line ($ symbol)
Move the cursor to beginning of line (0)
See line numbers in Vi editor
Disable line numbers in Vi editor
Go to a specific line number
Copy a line (yy)
Paste a line (p)
Delete a line (dd)
Delete a single character (x)
Ctrl + R to search for previous commands
netcat command (nc)
nslookup (to find public ip address of a website)
curl (to find website corresponding to an ip address)
Vi Editor Introduction
Vi editor is a very famous text editor in linux. If you want to create/edit any file in linux environment, you should know fundamentals of Vi editor. We have already used it a little bit, let us see slightly more of it.
Vi modes
The 2 modes that we commonly use are:
Read-only mode or normal mode
Edit mode or write mode or insert mode
Read-only mode is the default mode of Vi text editor. To understand this better, let us create a new file
Hit Enter. Try to type something, example, abc329, you would notice that nothing would be typed, see below. The reason being, default mode is readonly
Switch from read-only to insert mode in Vi
If you want to type something, switch to ‘insert’ mode by pressing the ‘i’ key. Now you can begin typing anything you want, just the way you type in notepad or any other editor
Save the file in Vi
Save the file by performing below-
Esc key followed by colon : followed by wq and Hit Enter
‘w’ stands for write.
You can ‘cat’ the file, see that the contents got saved
Quit the file without saving
Open the same file, press ‘i’ key and type something, see below
To quit the file without saving-
Esc key followed by colon : followed by q! and Hit Enter
You can ‘cat’ the file, notice that the contents did not change
Switch from insert mode to read-only mode
Open the same file, press ‘i’ key and type something, see below
To switch back to the read-only mode, press ‘Esc’ key
Now try to type something, you will not be able to type anything
Quit the file by typing colon : followed by q! and Hit Enter
Move the cursor to beginning of the file (gg)
Open the same file
In read-only mode, press the arrow-down key to move the cursor to 2nd line
Now, to go back to the file beginning, simply press gg
Notice above that cursor has moved to first line. So if you are at, lets say 10th line, press gg in read-only mode to move the cursor to the beginning of file
Move the cursor to end of the file (Shift + g)
To move the cursor to the end of file, press Shift + g (read-only mode)
Move the cursor to end of line ($ symbol)
Suppose we are at line beginning
Shift + 4 (which is $) will move cursor to end of line
Remember that we are executing all these commands in read-only mode.
Move the cursor to beginning of line (0)
The number 0 will move cursor to the beginning of the line
See line numbers in Vi editor
Escape colon :set number, Hit Enter to see line numbers
Disbale line numbers in Vi editor
Escape colon :set nonu, Hit Enter to disbale line numbers
Go to a specific line number
Let us enable line numbers
If you want to go to line number 3, Escape colon :3, Hit Enter
Copy a line (yy)
To copy line number 3, press yy in read only mode
Paste a line (p)
To paste line number 3, first copy it and than press p in read only mode
Delete a line (dd)
To delete line number 4, press dd in read only mode
Delete a single character (x)
To delete a single character, press x in read only mode. Notice below that ‘5’ is deleted
Ctrl + R to search for previous commands
If you remember some portion of any of the previous commands that you have executed, you can simply search it. You don’t have to remember the entire command.
So let’s say you want to search for the command that restarts nginx
Simply Ctrl+R
The below prompt comes up
Start typing resta
Notice below that the entire command comes up
Simply hit Enter to execute command
netcat command (nc)
The netcat command can be used to check if a port is open or not. The syntax is nc -zv <website> <port number>
As you can see below, the port number 80 is open for both the websites
So as we saw earlier, the below command shows that nginx is running on all the n/w interfaces
So, as we can see below, the port 80 is open on both the n/w interfaces
However, as expected, the port 81 is not open, see below. That means none of the service is running on port number 81
So whenever you are trying to access a website and if the website does not load, the first thing you need to check if the port 80 is open or not
nslookup (to find public ip address of a website)
We can find out the public ip address of a website using nslookup, the syntax is nslookup <website>
As you can see below, we do get the ip address of google.com
Just copy/paste this address on a browser
Hit Enter
Notice you are redirected to google home page
curl (to find website corresponding to an ip address)
Let us first install curl
We can now use curl command to find website corresponding to an ip address. As we know, www.google.com is the domain name corresponding to below ip address
Also, as expected, 127.0.0.1 corresponds to nginx service, see below
Similarly for ethernet port
Thank you for reading!