Vi Editor Lab - 04-04-2022
Vi Editor Lab - 04-04-2022
DO: Press CTRL+ALT+F1 ; To switch to virtual console 1 and then login as “root”
DO: Type pwd ; display your current location in the file system tree ( working directory)
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
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.
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”
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?
________________________________________________________________________
________________________________________________________________________
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”
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.
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
_____________________________________________________________
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.
Press “ESC”
Type ?nolog ;followed by <Enter>
Press “n” 5 times to look for the next 5 instances
Copying a single line
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
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: What text appeared at the bottom of the screen when “p” was pressed?
________________________________________________________________
Q: What text appeared at the bottom of the screen when “5dd” was pressed?
________________________________________________________________
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
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
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 cat /etc/hosts ; you should see something like the following
DO: Type cat /etc/hostname ; you should see something like the following
DO : Type hostname
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
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
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.
In Linux:
Note: Your computer probably does not have and IP address so , you should see something
like the following
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
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
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
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 .
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
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
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
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.
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