Installing More Tools

In GNU/Linux, you can download and install a software by one command (which is difficult to do in Windows). This is achieved by the package manager. Different GNU/Linux distribution has different package manager. In Debian, the package manager is called apt.

Installing tools from the Debian image

Now you are going to install some tools from the Debian image for convinence. First "insert" the Debian image: Click Devices in the menu bar in the VM window, navigate to CD/DVD Devices, click Choose a virtual CD/DVD disk file..., then select the Debian image file. Then add the sources in the image to the APT's list by the following command:

apt-cdrom add

You will see the following prompt:

Please insert a Disc in the drive and press enter

Since you have already "inserted the Disc" just now, press the Enter key, and you will see some message is output. Read them, you will find an error labeled with "Permission denied". Switch to the root account and execute the above command again, you will find that the command is executed without errors. Now you can install the following tools.

sudo

apt-get install sudo

sudo allows you to execute a command as another user (usually root). This means you do not need to switch to the root account to execute a system administration command or modify a file owned by root. But before you can use sudo, you should add your user account to the sudo group:

addgroup jack sudo

Replace "jack" above with your username. To let the above command go into effect, you should login the system again. Type

exit

to go back to your user account. And exit again to logout, then login again. Now you can use sudo. If you find an operation requires root permission, append sudo before that operation. For example,

username@hostname:~$ sudo poweroff

Note that running sudo may require password. This password is your user account password, not the root one.

Why use "sudo" instead of "su"?

You may consider sudo unnecessary, because you can always perform all operations with the root account. But this may take your system at risk. Can you figure out why?

In fact, all operations related to system changing require root permission. If a malicious program obtains root permission, it can do very bad things, such as deleting system files, to destory your system! Therefore, if an operation can be performed without root permission, perform it without root permission.

vim

apt-get install vim

vim is called 编辑器之神. You will use vim for coding in all PAs and Labs, as well as editing other files. Maybe some of you prefer to other editors requiring GUI environment (such Visual Studio). However, you can not use them in some situations, especially when you are accessing a physically remote server:

  • the remote server does not have GUI installed, or
  • the network condition is so bad that you can not use any GUI tools.

In these situations, vim is still a good choice. If you prefer to emacs, you can download and install emacs from network mirrors after the APT sources file is configured.

ssh

apt-get install openssh-server

ssh is a tool for remote accessing. Using ssh in the experiment can take some advantage of the host system. For ssh configuration, see the Logging in via SSH section.

Installing tools from the network mirrors

Since some tools needed for the PAs can not be found in the Debian image, you will download and install them from the network mirrors. The Debian image will not be used any longer, so "eject" the Debian image: Click Devices in the menu bar in the VM window, navigate to CD/DVD Devices, click Remove disk from virtual drive.

Before using the network mirrors, you should check whether the VM can access the Internet.

Checking network state

By the default network setting of the VM, the VM will share the same network state with your host. That is, if your host is able to access the Internet, so does the VM. To test whether the VM is able to access the Internet, you can try to ping a host outside the university LAN:

ping www.baidu.com -c 4

You should receive reply packets successfully:

PING www.a.shifen.com (220.181.111.188) 56(84) bytes of data.
64 bytes from 220.181.111.188: icmp_seq=1 ttl=51 time=5.81 ms
64 bytes from 220.181.111.188: icmp_seq=2 ttl=51 time=6.11 ms
64 bytes from 220.181.111.188: icmp_seq=3 ttl=51 time=6.88 ms
64 bytes from 220.181.111.188: icmp_seq=4 ttl=51 time=4.92 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 4.925/5.932/6.882/0.706 ms

If you still get an "unreachable" message, please check whether you can access www.baidu.com in the host system, as well as your configuration.

Learning vim

You are going to be asked to modify a file using vim. For most of you, this is the first time to use vim. The operations in vim are quite different from other editors you have ever used. To learn vim, you need a tutorial. There are two ways to get tutorials:

  • Issue the vimtutor command in terminal. This will launch a tutorial for vim. This way is recommended, since you can read the tutorial and practice at the same time.
  • Search the Internet with keyword "vim 教程", and you will find a lot of tutorials about vim. Choose some of them to read, meanwhile you can practice with the a temporary file by
    vim test
    
PRACTICE IS VERY IMPORTANT. You can not learn anything by only reading the tutorials.

Some games operated with vim

Here are some games to help you master some basic operations in vim. Have fun!

The power of vim

You may never consider what can be done in such a "BAD" editor. Let's see two examples.

The first example is to generate the following file:

1
2
3
.....
98
99
100

This file contains 100 lines, and each line contains a number. What will you do? In vim, this is a piece of cake. First change vim into normal state (when vim is just opened, it is in normal state), then press the following keys sequentially:

i1<ESC>q1yyp<C-a>q98@1

where <ESC> means the ESC key, and <C-a> means "Ctrl + a" here. You only press no more than 15 keys to generate this file. Is it amazing? What about a file with 1000 lines? What you do is just to press one more key:

i1<ESC>q1yyp<C-a>q998@1

The magic behind this example is recording and replaying. You initial the file with the first line. Then record the generation of the second. After that, you replay the generation for 998 times to obtain the file.

The second example is to modify a file. Suppose you have such a file:

aaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeefffffffffffffffffffffffff
ggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjjj

You want to modify it into:

bbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaa
dddddddddddddddddddddddddccccccccccccccccccccccccc
fffffffffffffffffffffffffeeeeeeeeeeeeeeeeeeeeeeeee
hhhhhhhhhhhhhhhhhhhhhhhhhggggggggggggggggggggggggg
jjjjjjjjjjjjjjjjjjjjjjjjjiiiiiiiiiiiiiiiiiiiiiiiii

What will you do? In vim, this is a piece of cake, too. First locate the cursor to first "a" in the first line. And change vim into normal state, then press the following keys sequentially:

<C-v>24l4jd$p

where <C-v> means "Ctrl + v" here. What about a file with 100 such lines? What you do is just to press one more key:

<C-v>24l99jd$p

Although these two examples are artificial, they display the powerful functionality of vim, comparing with other editors you have used.

Adding APT sources

To use network mirrors, you should configure the source list file. This file lists the sources for apt to obtain software information. The source list file is called sources.list, and it is located under /etc/apt directory. Switch to this directory by cd command:

cd /etc/apt

Then open sources.list using vim:

vim sources.list

you can see the file content:

#

# deb cdrom:[Debian GNU/Linux 8.0.0 _Jessie_ - Official i386 NETINST Binary-1 20150425-11:41]/ jessie main

#deb cdrom:[Debian GNU/Linux 8.0.0 _Jessie_ - Official i386 NETINST Binary-1 20150425-11:41]/ jessie main

deb cdrom:[Debian GNU/Linux 8.0.0 _Jessie_ - Official i386 NETINST Binary-1 20150425-11:41]/ jessie main
deb http://security.debian.org/ jessie/updates main
deb-src http://security.debian.org/ jessie/updates main

After you learn some basic operations in vim (such as moving, inserting text, deleting text), you can try to modify the sources.list file as following:

--- before modification +++ after modification @@ -7,4 +7,5 @@ -deb cdrom:[Debian GNU/Linux 8.0.0 _Jessie_ - Official i386 NETINST Binary-1 20150425-11:41]/ jessie main deb http://security.debian.org/ jessie/updates main deb-src http://security.debian.org/ jessie/updates main +deb http://mirrors.163.com/debian/ jessie main contrib non-free +deb http://ftp.cn.debian.org/debian/ jessie main contrib non-free

We present the modification with GNU diff format. Lines starting with + are to be inserted. Lines starting with - are to be deleted. Other lines are not to be modified. If you do not understand the diff format, please search the Internet for more information.

After you are done, you should save your modification. Type

:w

to save the file. However, you receive an error message:

E45: 'readonly' option is set (add ! to override)

According to the message, the file is read-only, but you may use ! to force saving. Type

:w!

But you receive another error message this time:

"sources.list" E212: Can't open file for writing

It seems that you do not have the permission to write to this file. Type

:q!

to exit vim without saving. Back to shell, type

ls -l

to display detail information of the files. You will see a list like

total 20
drwxr-xr-x 2 root root 4096 May  6 22:30 apt.conf.d
drwxr-xr-x 2 root root 4096 Apr 14 01:26 preferences.d
-rw-r--r-- 1 root root  432 May  6 22:30 sources.list
drwxr-xr-x 2 root root 4096 Apr 14 01:26 sources.list.d
drwxr-xr-x 2 root root 4096 May  6 22:30 trusted.gpg.d

Here are some explanations of what the first column (for example, drwxr-xr-x) of the list means. For more information about what each column means, please search the Internet.

You can see that the sources.list file is owned by root, and you do not have permission to modify it. Therefore, use sudo to open the file:

sudo vim sources.list

Then perform the modification. This time you should save the file successfully.

After saving the modification, you can tell apt to retrieve software information from the sources specified in sources.list:

apt-get update

This command requires root permission, too. And it requires Internet accessing. It costs some time for this command to finish. If some errors are reported, please check

  • whether there are any typos in sources.list, and
  • whether your host is able to access the Internet.

Installing tools for PAs

The following tools are necessary for PAs:

apt-get install build-essential    # build-essential packages, include binary utilities, gcc, make, and so on
apt-get install gcc-doc        # GCC document
apt-get install gdb            # GNU debugger
apt-get install git            # reversion control system
apt-get install time        # we use the GNU time program instead of the build-in one in bash

The usage of these tools is explained later.

results matching ""

    No results matching ""