Simon The Pi Man bookstore
Mmmm Raspberry Pi
Simon the Pi Man
To Infinity (probably) and beyond
(A beginners Resource for the Raspberry Pi computer using the Debian distro)
since June 2012
Keep up to date with our RSS feed

If you are looking for info on the Raspberry Pi computer stay on this site
If you are looking for a good cafe in Rye in East Sussex try "Simon the Pie man" near the church.

Beginners Guide to Unix - part 4 -  Editing files with the vi editor

On this page I will give you guidelines as to how you can edit the contents of files using one of the inbuilt unix editors called vi(short for visual editor).

My thanks go to Harry Moyes for his help in improving this tutorial

Please Note: in this guide please press [Return or Enter] at the end of each line with red text.

1. From the previous beginners guide have seen that the /etc folder contains config files so we are going to edit the hosts file and create a local server named "fred" - now to begin with we are going to use a command called ping to see if a machine exists so we are going to ping raspberrypi. pi@raspberrypi:/etc$ ping -c 5 raspberrypi
This means send a test network request to a server called raspberrypi and limit it to 5 times - at 1 per second by default.

PING raspberrypi ( 56(84) bytes of data.
PING raspberrypi ( 56(84) bytes of data.
64 bytes from raspberrypi ( icmp_req=1 ttl=64 time=0.132 ms
64 bytes from raspberrypi ( icmp_req=2 ttl=64 time=0.128 ms
64 bytes from raspberrypi ( icmp_req=3 ttl=64 time=0.132 ms
64 bytes from raspberrypi ( icmp_req=4 ttl=64 time=0.120 ms
64 bytes from raspberrypi ( icmp_req=5 ttl=64 time=0.122 ms

--- raspberrypi ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 0.120/0.126/0.132/0.015 ms

This shows that a machine called raspberrypi has responded on a network address of which is actually the localhost address, meaning me.

2. Now we are going to ping a machine called fred.
pi@raspberrypi:/etc$ ping fred -n 5

ping: unknown host fred

This result is because the local raspberrypi server cannot recognise the name fred, it tries its /etc/hosts file first then it tries its DNS server to identify the I.P. address of the remote server from its name.

3. Right lets edit the file using vi
pi@raspberrypi:/etc$ vi /etc/hosts

::1             raspberrypi localhost6.localdomain6 localhost6       raspberrypi  rpi1-ext  rpi2-ext       localhost
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

"/etc/hosts" [readonly] 12L, 296C                             1,1           All

Firstly look at the bottom line [readonly] - this means that we dont have permission to edit this file so we need to come out of the editor and run the editor as root(god).

So to exit from vi type :q[Return or Enter] the : takes vi into command mode and the q then quits the program.

4. Well why is the file read only to us, I guess we had better look at the file permissions again so pi@raspberrypi:/etc$ ls -l /etc/hosts

-rw-r--r-- 1 root root 296 Jun 10 14:54 /etc/hosts

We can see that the file is read-write as root, but read only for everyone else(for a refresher on the file permissions look a ( 1 Logging in and Files))

5. Therefore we need to edit this file as root, so we need to use a command sudo which means run whatever is after sudo as though it was being run by root(god) - so superuser do
pi@raspberrypi:/etc$ sudo vi /etc/hosts

The vi editor is continued on the next Beginners Guide
Useful commands that you may have missed above
  ping are you there ?
  ls for listing files
  ls -l for detailed listing of files
  sudo run command as root(god)
  vi file editor 2012->2017

hosted by VillageNetThe Raspberry Pi Foundation is a UK registered charity which exists to promote the study of computer science and related topics, especially at school level, and to put the fun back into learning computing.

Please Note:- any mention of the Raspberry Pi computer on these pages refers to the Raspberry Pi Foundation's product, who also have trademark rights to the term 'Raspberry Pi'.