0% found this document useful (0 votes)
226 views17 pages

Vi Editor Lab - 04-04-2022

The document provides instructions for exercises using the Vi text editor. It lists important Vi commands like opening and saving files, switching between insert and command modes, copying/deleting/moving lines, and searching text. The exercises demonstrate how to use these commands through a series of steps like making a copy of the /etc/passwd file, saving it with a different name, deleting and searching lines, and copying, deleting, and moving blocks of text.

Uploaded by

Josue Moreira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
226 views17 pages

Vi Editor Lab - 04-04-2022

The document provides instructions for exercises using the Vi text editor. It lists important Vi commands like opening and saving files, switching between insert and command modes, copying/deleting/moving lines, and searching text. The exercises demonstrate how to use these commands through a series of steps like making a copy of the /etc/passwd file, saving it with a different name, deleting and searching lines, and copying, deleting, and moving blocks of text.

Uploaded by

Josue Moreira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lab#6: Vi editor exercises

Vi editor commands that you must be a very familiar with

 Opening a new or existing file  vi path/filename


 Switching from command mode to Insert mode and vice versa
 Know the key sequence to saving a file in the following situations. The following
commands to the editor is preceded by “ESC +Shift +:” key sequence.
o Quit without saving q!
o Save (write) and quit  wq
o Save the file with a different name ( and possibly in a different location)
w path/newfileName
 Copying and Deleting single lines and multiple consecutive lines of text
 Moving a single or multiple lines from one location in a file to another location in
a file.
 Turning ON/OFF the number line feature
 Performing a Forward./Reverse Search in a text file

DO: Boot a computer with the Kali Linux Virtual Machine

DO: Press CTRL+ALT+F1 ; To switch to virtual console 1 and then login as “root”

DO:Type clear ; clear the screen

DO: Type pwd ; display your current location in the file system tree ( working directory)

What was the system’s response? ______________________

READ: The following command will make a copy (cp) of the /etc/passwd file for us to play with
instead of messing up the real file

DO: Type cp /etc/passwd /root/passwd.bak ; make a copy of the “passwd” file to the
current directory and call it “passwd.bak”
DO: Type ls –l display the content of the directory. Refer to the following screenshot
showing the “passwd.bak” file

DO: Type vi passwd.bak ; open the passwd.bak file in the vi editor

READ: When “vi” opens a file it the insertion point (blinking cursor) is on first line of the file,
also the name of the file and how many lines and characters are displayed at the bottom right
side of the file.

Q: How many lines are in the file? _________________________

Q: What mode are you in (Command or insert)? _____________________


Switching modes
Observation: When you open a document you always start off in “command” mode

DO: press “i” or the “INS/INSERT” key; switch to insert mode

Q: What string of text appears at the bottom of the screen? _______________

DO: press “ESC” key; switch back command mode

OK; let’s learn some basic commands before we do the more complex things

 Know the key sequence for saving a file in the following situations. The following
commands to the editor is preceded by “ESC ;Shift +:” key sequence.
o Quit without saving q!
o Save (write) and quit  wq
o Save the file with a different name ( and possibly in a different location)
w path/newfileName

Let’s learn how to save the file with a different name (save as)
Save the file with a different name ( and possibly in a different location)
w path/newfileName

READ: Saving a file with a different name is useful in situations where you are working on a
document and don’t want to save the changes to the original document, for example you are
working on a “resume” and want to experiment with changes but not save the changes to the
original. This is equivalent to “save as” in a GUI text editor such as “notepad” or “word”
DO: Press 10dd ; this will delete 10 consecutive lines , a message should appear at the bottom
of the file saying “10 fewer lines”

1. Press the ESC key followed by SHIFT + :


2. Type w /root/new-passwd.bak followed by <ENTER> ; saves the changes to a
file named “new-passwd.bak” in the root’s home directory.

We will confirm the existence of the file after the next comamnd

Q: What text appears at the bottom of the screen, what document are you now working in?

________________________________________________________________________

________________________________________________________________________

Let’s learn how to quit a file without saving it


READ: Quitting a file without saving it is very useful in situations where you make big mistakes
and want to start from scratch or you edited a file and decided that you don’t want to save
changes to.

Observation: before you perform the following operation, observe the beginning of the first 10
lines of the file

DO: Press 10dd ; this will delete 10 consecutive lines , a message should appear at the bottom
of the file saying “10 fewer lines”

 Press the ESC key followed by SHIFT + :


 Type q! followed by <ENTER> ; This will exit the file without saving it
DO: clear the screen and type “ls –l /root” you should see the following. Note the
existence of the “new-passwd.bak” file in the following screenshot.

Saving changes to a file


After editing a file you can always save the changes by performing the following two steps

 Press the ESC key followed by SHIFT + :


 Type wq followed by <ENTER> ; This will save the changes and exit the file
Turn on line numbering
DO: Type vi passwd.bak ; open the passwd.bak file in the vi editor

1. Press the “ESC” key followed by SHIFT + :


2. Type “set number” ; turn on line numbering

Q: Do you see numbers on the left side of the screen? Y/N

DO: What’s written on line #9?: ____________________________________________

Q: How many lines are in the file ? ________________________________________

Jumping to specific lines of a file

DO: Press ESC ; go to command mode

DO: Type 1G ; go to the first line of the file

DO: Type G ; goes to last line

DO: Type 23G ; jump to line 23

Turn off line numbering


1. Press the “ESC” key followed by SHIFT + :
2. Type “set nonumber” ; turn on line numbering

Q: Did the numbers disappear from the left side of the screen? Y/N

Observation: The line number feature is just for convenience if you have to locate a line quickly
for editing etc.

DO:Press 10G

Q: Did you jump to line 10 ? look at the text at the bottom right of the screen
Searching the file for string
Forward search …Searching a document from top to bottom

The steps
 Navigate to the top of the document ( UP/DOWN arrow key)
 Press “ESC” key to go to command mode
 Type /<string> followed by <ENTER>, where <string> is the string you want to find.

DO: Navigate to the top of the document ( UP/DOWN arrow key)

 Press “ESC”
 Type /ssh ;followed by <Enter>
 press “n” to look for the next instances until there are no more “ssh” lines to be found

Q: What text message was displayed at the bottom of the screen?

_____________________________________________________________

Backward search ..Searching a document from bottom to top

The steps
 Navigate to the bottom of the document ( UP/DOWN arrow key)
 Press “ESC” key to go to command mode
 Type ?<string> followed by <ENTER>, where <string> is the string you want to find.

DO: Navigate to the bottom of the document ( UP/DOWN arrow key)

 Press “ESC”
 Type ?nolog ;followed by <Enter>
 Press “n” 5 times to look for the next 5 instances
Copying a single line

Copying is a two-step process

1. copy ; yanking
2. paste

1. Press “ESC”
2. Navigate to the line that begins with “root”
3. Press “yy” ; copy the line to the memory buffer ..COPY
4. Navigate to the line that begins with “sshd” and press “p” to paste the line..PASTE

Q: Was the line pasted? Y/N

5. Press “p” again to paste another copy


6. Press “u” twice to undo the paste

Copying multiple consecutive lines


1. Press “ESC”
2. Navigate to the first line to be copied …The line that begins with “mail”

Observe the “mail” line and the 4 lines below it

3. Press “5yy” ; copy 5 consecutive lines beginning with the first line (mail) to the memory
buffer
4. Navigate to the line that begins with “sshd” and press “p” to paste the lines

Q: Were the line pasted? Y/N

Q: What text appeared at the bottom of the screen when “p” was pressed?

________________________________________________________________

5. Press “u” to undo the paste


Deleting a single line
1. Press “ESC”
2. Navigate to the line that begins with “mail”
3. Press “dd” ; delete the line (move the line to the memory buffer)

Q: Was the line deleted? Y/N

Q: Are you still in command mode? Y/N

4. Press “u” to undo deletion

Deleting multiple consecutive lines


1. Press “ESC”
2. Navigate to the first line to be deleted …The line that begins with “sshd”
3. Press “5dd” ; delete 5 consecutive lines from the first line to the memory buffer

Q: Were the line deleted? Y/N

Q: What text appeared at the bottom of the screen when “5dd” was pressed?

________________________________________________________________

4. Press “u” to undo the deletion


Moving a single line

Steps in moving

3. cut
4. paste

1. Press “ESC”
2. Navigate to the line that begins with “mail”
3. Press “dd” ; delete the line (move the line to the memory buffer) …CUT

Q: Was the line deleted? Y/N

Q: Are you still in command mode? Y/N

1. Navigate to the line that begins with “ssh”


2. Press “p” …PASTE
3. Press “u” twice to undo the paste and cut

Moving multiple consecutive lines

Steps in moving

5. cut
6. paste

1. Press “ESC”
2. Navigate to the first line to be moved …The line that begins with “mail”
3. Press “5dd” ; delete the line (move the line to the memory buffer) …CUT

Q: Was the 5 lines deleted(CUT)? Y/N

Q: Are you still in command mode? Y/N

4. Navigate to the line that begins with “ssh”


5. Press “p” …PASTE

Q: Were the 5 lines pasted? Y/N

6. Press “u” twice to undo the paste and cut


Let’s do some useful things with our new text editing skills
 Changing the hostname
 Configuring IP Related information for the machine
 Creating additional root accounts
 Giving a user “sudo” powers

Changing the computer’s name


DO: Type hostame ; displays the current name of the computer

What is the current name of your computer? _____________________________________

READ: The hostname is stored in two files: /etc/hosts and /etc/hostname file.

 The /etc/hosts file contains IP address to machine name mappings, this file is used for
resolving names to IP address in the absence of a DNS server
 The /etc/hostname file contains the actual computer’s name

DO: Type clear ; clear the screen

DO: Type cat /etc/hosts ; you should see something like the following

DO: Type cat /etc/hostname ; you should see something like the following

Q: Do you see the hostname in both files? Y/N


READ: The next command will temporarily change the computer’s name , this means no
changes will be made to the /etc/hosts and /etc/hostname files and if the computer reboots the
computer’s hostname will revert back to the one in the /etc/hostname file.

DO: Type hostname KaliBox

DO : Type hostname

Q: What is the name of the computer? ________________________

DO: Type cat /etc/hosts

Q: Did the hostname change in the /etc/hosts file? Y/N

DO: Type cat /etc/hostname

Q: Did the hostname change in the /etc/hostname file? Y/N

DO: Type reboot ; reboots the system

DO: Re-login as root to terminal 1

DO: Type hostname

Q: What is the current name of your computer? ________________________________


let’s permanently change the name using the vi editor
DO: Type vi /etc/hosts ; open the file in the vi editor

 go to insert mode
 change the line that says “127.0.1.1 <Hostname>” to “127.0.1.1 YourLastName”
 save and quit the file

DO:Type cat /etc/hosts ; verify that the changes were correctly made

DO: Type vi /etc/hostname ; open the file in the vi editor

 go to insert mode
 change the line that says <Hostname> to YourLastname
 save and quit the file

READ: The names in the two files must be the same, if not re-edit the file(s)

DO: Type cat /etc/hostname ; verify that the changes were correctly made

DO: Type hostname

Q: Was the computer name changed to your lastname? Y/N

OK: Let’s reboot the system and see what happens

DO: Type reboot ; reboots the system

DO: Re-login as root to terminal 1

DO: Type hostname

Q: What is the hostname of the computer? ________________________________

Screenshot
 Type clear ; clear the screen
 Type hostname ; display the computer’s name
 Take a picture of your computer’s name and paste it below
Changing the computer’s IP address
READ: The IP address information is stored in the /etc/network/interfaces file.

DO: Type ifconfig or ip a or ip addr to display the IP address of the computer

In Linux:

 Network card interfaces are named “eth0, eth1 etc”


 IP address is displayed as “inet”
 Subnet mask is “netmask”

Note: Your computer probably does not have and IP address so , you should see something
like the following

DO: Type vi /etc/network/interfaces

DO: Add the lines with the red dots and when finished save and exit the file
(ESC,shift+:wq)
OK: Let’s reboot the system and see what happens

DO: Type reboot ; reboots the system

DO: Re-login as root to terminal 1

Screenshot
 Type clear ; clear the screen
 Type ifconfig ; display the IP address information
 Type route -n ; display the routing table
 Take a screenshot and paste it below

Creating additional roots by editing the /etc/passwd file


Steps

1. Create a user
2. Open the /etc/passwd file in a text editor and change the target user’s
UID to zero
3. Save and exit the file
4. Test the users power

The following will create a new user named “pinky” with a UID of 2000 and a blank
password

DO: Type useradd -m -u 2000 –p “” pinky

DO: Type tail –n 1 /etc/passwd ; confirm that user was created

DO: Type tail –n 1 /etc/shadow ; confirm that user has no password

DO: Type vi /etc/passwd; open the /etc/passwd file in the vi editor

 Go to insert mode
 Navigate to “pinky’s” account and change the UID to zero
 Save and quit the file
DO: Type grep pink /etc/passwd .

You should see “pinky:x:0:2000::/home/pinky:/bin/sh”, if you don’t see that you


made a misteak …re-edit the file and make the necessary changes.

DO: Press “Ctrl+ALT+F3” ; go to virtual console 3

 login as “pinky”
 Does your prompt end with a “$” or a “#” sign?
 Type whoami ..Does it say root? Y/N
 Type useradd –m bwayne
 Type tail -n 1 /etc/passwd ; is the account there? Y/N
 Type exit ; logoff the terminal
 Press CTRL+ALT+F1 ; go back to console 1

Creating additional sudo users by editing the /etc/group file


Steps

1. Create a user
2. edit the /etc/group file and add the user to the end of the sudo group
3. save and exit the file
4. test the users power
The following will create a new user named “brain” with a UID of 3000 and a blank
password

DO: Type useradd -m -u 3000 –p “” brain

DO: Type tail –n 1 /etc/passwd ; confirm that user was created

DO: Type tail –n 1 /etc/shadow ; confirm that user has no password


DO: Type vi /etc/group ; open the /etc/group file in the vi editor

 go to insert mode
 navigate to “sudo” group and add “brain” to the end of the line ( use a comma to
separate the members if necessary)
 Save and quit the file

DO: Type groups brain ; list all groups that brain is a member of

Q: which groups is brain a member of? _____________________________

DO: Type grep sudo /etc/group

You should see “sudo:x:<GUID>:sholmes,brain”, if you don’t see that you made a
misteak …re-edit the file and make the necessary changes.

DO: Press Ctrl+ALT+F3 ; go to virtual console 3

 login as “brain”
 Does your prompt end with a “$” or a “#” sign?
 Type whoami ..What does it say?__________________________
 Type useradd –m dgrayson ; error? Y/N
 Type sudo useradd –m dgrayson
 Type tail -n 1 /etc/passwd ; is the account there? Y/N
 Type exit ; logoff the terminal
 Press CTRL+ALT+F1 ; go back to console 1
 shutdown the system

You might also like