README.md 9.78 KB
Newer Older
Sergio Coronado's avatar
title    
Sergio Coronado committed
1
# Linux Basics 101 and GitLab R3 Workshop
Sergio Coronado's avatar
Sergio Coronado committed
2

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
3
4
5
6
7
8
9
## Quiz (to get to know you)

* Who doesn't know what an IP address is?
* Who doesn't know what a server is?
* Who doesn't know what a port is?


Sergio Coronado's avatar
Sergio Coronado committed
10
11
## How to connect to a remote Session
***
Sergio Coronado's avatar
Sergio Coronado committed
12

Christophe Trefois's avatar
Christophe Trefois committed
13
14
15
16
17
18
19
### 1. My terminal is better than yours?

> Definition: A terminal is an interface in which you can type and execute text based commands.

#### Download your Terminal if you don't have one already.


Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
20
+ **Windows**
Christophe Trefois's avatar
Christophe Trefois committed
21
  + MobaXterm [download](http://mobaxterm.mobatek.net/download-home-edition.html)
Christophe Trefois's avatar
Christophe Trefois committed
22
23

  > Remarks: In Windows, there is a default command line, called command prompt. However, the names of many commands is different and some commands do not exist. For that reason, use another terminal, such as MobaXterm.
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
24

Christophe Trefois's avatar
Christophe Trefois committed
25
+ **Mac**
Christophe Trefois's avatar
Christophe Trefois committed
26
27
  + iTerm2 [download](https://www.iterm2.com/downloads.html)
  + Terminal
Christophe Trefois's avatar
Christophe Trefois committed
28
+ **Linux**
Christophe Trefois's avatar
Christophe Trefois committed
29
30
31
  + Terminal

### 2. Generate your SSH key
Christophe Trefois's avatar
Christophe Trefois committed
32
33
34

#### Background information

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
35
+ **What is SSH?**
Christophe Trefois's avatar
Christophe Trefois committed
36
37
38

<img src="images/screen3.png" width=600>

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
39
* **What is a SSH key pair?**
Christophe Trefois's avatar
Christophe Trefois committed
40

Christophe Trefois's avatar
Christophe Trefois committed
41
42
<img src="images/screen4.jpg" width=400>

Christophe Trefois's avatar
Christophe Trefois committed
43
44
#### Key generation

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
45
+ **Windows (MobaXterm) / Mac / Linux**
Christophe Trefois's avatar
Christophe Trefois committed
46
47
  + ssh-keygen

48
        ```bash
49
50
51
52
53
54
55
56
57
        vagrant@vagrant-ubuntu-trusty-64:~$ ssh-keygen
        Generating public/private rsa key pair.
        Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
        Enter passphrase (empty for no passphrase):
        Enter same passphrase again:
        Your identification has been saved in /home/vagrant/.ssh/id_rsa.
        Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
        The key fingerprint is:
        ac:d7:36:ee:74:f6:34:d1:ff:ee:dd:bc:d5:ee:70:36 vagrant@vagrant-ubuntu-trusty-64
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
58
        The key\'s randomart image is:
59
60
61
62
63
64
65
66
67
68
69
        +--[ RSA 2048]----+
        |                 |
        |                 |
        |                 |
        |       .       . |
        |        S     . .|
        |       . .     .o|
        |      . . = o +E*|
        |       . + + o B*|
        |         .o   .=@|
        +-----------------+
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
70
        ```
Christophe Trefois's avatar
Christophe Trefois committed
71

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
72
73
  > Note, you must **NEVER** share the id_rsa file with anybody

Christophe Trefois's avatar
Christophe Trefois committed
74
75
76
### 3. Create LUMS account

This task has been done already by your awesome R3 team.
Christophe Trefois's avatar
Christophe Trefois committed
77

Christophe Trefois's avatar
Christophe Trefois committed
78
### 4. Add SSH key to LUMS (**L**CSB **U**ser **M**anagement **S**ystem)
Christophe Trefois's avatar
Christophe Trefois committed
79

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
80
81
* Attach your *public* SSH key to your LUMS account
* To get the contents of your public key, type the following command (you will understand it all later today :) )
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
82

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
83
84
85
86
```bash
cd
cat .ssh/id_rsa.pub
```
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
87

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
88
Expected output (your key will be different)
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
89

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
90
Question: Is it OK for me to post my public key publicly?
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
91

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
92
93
94
```
christophe.trefois.TrefexVMPC$ cd && cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRVvaaZejn/CHQsXdb2nMzzPP3WF14H9djaTWmOCabl/q1BJEjWX21P1D+r45mB1EUfUi5w1VrN6I3/9ytZweOa4kLsMJCYU0sEwZq2KKOEKkW7M7W6LGJ+4C2QzkXLCY4r2yToAK7HwvvDUoDF3Ioodw9omTFLaHGzTLEsw9KVrYYwcyAQ5SOQTsL3VuMgyTd+Z8414HPIQrB3abEqHGGvkcrxnKhaEF/R3NNJ523ErRmRZa9cIB6dhXjzEIcvgaDzMKtXbflciVt3u3vM+4aaKhHwO73KUv8wDiPau8ROxmvNkADZB4gpsjbKqVP876QCT5YI5Z0D8Bcj5GoKZ1D christophe.trefois@TrefexVMPC
Christophe Trefois's avatar
Christophe Trefois committed
95

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
96
```
Christophe Trefois's avatar
Christophe Trefois committed
97

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
98
99
* Go to https://lcsb-ipa.uni.lu
* Login with first.lastname and password linuxwork2015
Christophe Trefois's avatar
Christophe Trefois committed
100

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
101
    > (Note: Use your password for existing LUMS account)
Christophe Trefois's avatar
Christophe Trefois committed
102

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
103
104
105
106
* Next to SSH public keys, click on Add and copy paste the key in there, including `ssh-rsa` and the text at the end.

* Don't forget to click on *Update* on the top menu buttons.

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
107
108
109
### 5. Connecting to Virtual Machine

* **Windows / Mac / Linux**
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
110
111
  * Open your terminal
  * Connect to the workshop virtual machine
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
112

Christophe Trefois's avatar
Updates    
Christophe Trefois committed
113
        ```bash
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
114
        ssh first.lastname@10.79.2.191 -p 8022
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
115
        ```
Sergio Coronado's avatar
Sergio Coronado committed
116

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
117
118
119
120
121
122
123
      Explanations:

      * `ssh` is the command we will use to connect to our server
      * `first.lastname` is the username we will use
      * `@10.79.2.191` is the IP address of the server itself
      * `-p 8022` is a parameter. Here it specifies the port to connect to.

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
124
125
126
127
## Before we start

* A word that looks like `this` means that you should replace the word `this` with appropriate content.

Sergio Coronado's avatar
Sergio Coronado committed
128
129
## Discover your surroundings
***
Sergio Coronado's avatar
Sergio Coronado committed
130
131
* In which folder am I? (Hint: pwd)
* What is the contents of the current folder? (Hint: ls)
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
132
133
* Creating my first file called myFile.txt (Hint: touch `filename`)
* Checking that my file exists (Hint: ls)
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
134
* Creating my first folder called "myFirstFolder" (Hint: mkdir `foldername`)
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
* How do I enter my new folder? (Hint: cd `foldername`)
* Copying my first file to the new folder (Hint: cp `source` `target`)

**Exercise 1**

Check if the file is in the correct folder.

    Hint: ls, pwd

What if I want to see a structure like in Windows Explorer?

--> Try this:

```bash
cd
tree
```

You should see something like this:

```bash
.
├── myFile.txt
└── myFirstFolder
    └── myFile.txt

1 directory, 2 files
```

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
164
**Exercise 2**
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
165

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
166
I see `myFile.txt` twice. Maybe I should remove the copy I don't want.
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
167
168
169
170

* Go to where the to-be-deleted file is (Hint: cd)
* Remove the file (Hint: rm -i `myFile.txt`)

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
171
> In Linux, when asked questions you usually type `y` for **Yes** and `n` for **No** unless explicitly written.
Sergio Coronado's avatar
Sergio Coronado committed
172

Christophe Trefois's avatar
Christophe Trefois committed
173
174
175
176
177
## A couple remarks on file / folder names
* Try not to use spaces
* Try to capitalize some words for easier reading
  * for instance: thislongfoldernameisnotveryreadable, but ThisLongFolderNameIsQuiteReadable

Christophe Trefois's avatar
Christophe Trefois committed
178
## Learn more about any *nix command
Sergio Coronado's avatar
Sergio Coronado committed
179
***
Sergio Coronado's avatar
Sergio Coronado committed
180
181
* Go to your home folder (Hint: cd <no arguments>)
* Are there any hidden files in my home folder? (Hint: man ls)
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
182
183
184
185
186

> A hidden fie or folder starts with a dot. Note, on Windows, you normally can't create folders that start with a dot but with mobaXterm you can :)

When you do `ls -al` you see a different view and many hidden files.

Sergio Coronado's avatar
Sergio Coronado committed
187
* How do I get such nice documentation for other unix commands? (Hint: man <mycommand>)
Sergio Coronado's avatar
Sergio Coronado committed
188
189
190

## Copy files to the VM or from the VM (only using console!!)
***
Sergio Coronado's avatar
Sergio Coronado committed
191
* Logout of the VM (Hint: logout)
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
192
193
194
195
196
197
198
199
* Create a local training folder `training` (Hint: mkdir `training`)
* Go to the new folder called `training` (Hint: cd)
* Create a new file on your computer (Hint: touch `file3.txt`)
* Copy file1.txt to “myFirstFolder” on the VM

  Hint:

  ```bash
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
200
  scp –P 8022 file3.txt first.lastname@1079.2.191:myFirstFolder/
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
201
202
203
204
  ```

  Explanations:

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
205
  * `scp` stands for `secure copy` and the basic syntax is `scp source target`
Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
206
207
208
  * `-P 8022` specifies the port on which the server is accpeting our connection
  * `first.lastname@10.79.2.191` specifies how we want to login
  * `:/myFirstFolder` means the file will be put in to that folder on the server
Sergio Coronado's avatar
Sergio Coronado committed
209

Christophe Trefois's avatar
Fixes    
Christophe Trefois committed
210
211
212
213
214
215
216
217
218
219
220

* Connect back to the server and see if your new file is there

    Hint:

    ```bash
    ssh first.lastname@10.79.2.191 -p 8022
    cd myFirstFolder
    ls -al
    ```

Christophe Trefois's avatar
Christophe Trefois committed
221
## Command line tools and parameters
Sergio Coronado's avatar
Sergio Coronado committed
222
***
Christophe Trefois's avatar
Christophe Trefois committed
223
224
225

Let's do something fun!

Christophe Trefois's avatar
Christophe Trefois committed
226
On our server, we will download a zip file and unzip the contents. Ready? First, let's understand the zip command.
Christophe Trefois's avatar
Christophe Trefois committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

* Connect to the server
* Go to folder `myFirstFolder`
* Zip the two files you created

```bash
zip myArchive.zip myFile.txt file3.txt
```

* Check the contents of the zip archive

```bash
unzip -l myArchive.zip
```

Couple of notes:

* `-l` is a parameter. You can see what these parameters mean by doing `man unzip`.
Christophe Trefois's avatar
Christophe Trefois committed
245
246

Let's move on.
Christophe Trefois's avatar
Christophe Trefois committed
247

Christophe Trefois's avatar
Updated    
Christophe Trefois committed
248
### Let us download a zip from the Internet and extract its contents
Christophe Trefois's avatar
Christophe Trefois committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

Let's go:

```bash
cd
wget http://bit.ly/anArchive -O myName.zip
ls
```

* First I check the contents to make sure it's what I wanted:

    ```
    unzip -l myName.zip
    ```

* If I am satisfied, I can safely extract

    ```
    unzip myName.zip
    ```

Christophe Trefois's avatar
Christophe Trefois committed
270
## Text editor
Sergio Coronado's avatar
Sergio Coronado committed
271
***
Sergio Coronado's avatar
Sergio Coronado committed
272
* nano [Tutorial](http://www.howtogeek.com/howto/42980/the-beginners-guide-to-nano-the-linux-command-line-text-editor/)
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
273
274
275
276
277
278
279
280
281
282
283
284

* On the server, open the file `thisfile.txt` that you extracted above.


```bash
nano thisfile.txt
```

* Simply start typing and changing contents...

> Do you realize you are currently changing a file in a remote server and it feels like you are using Notepad? Much awesome!

Christophe Trefois's avatar
Updates    
Christophe Trefois committed
285
286
* When you are done, you can save by hitting `ctrl-o`, and then `Enter`.
* Now you can quit with `ctrl-x`
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
287

Christophe Trefois's avatar
Updates    
Christophe Trefois committed
288
* There are many other text editors for terminal only, such as vi, vim and emacs to name a few.
Sergio Coronado's avatar
Sergio Coronado committed
289
290
291

## File System explore
***
Christophe Trefois's avatar
Christophe Trefois committed
292
### Midnight Commander `mc`
Christophe Trefois's avatar
Updates    
Christophe Trefois committed
293
294
295
296
297
298
299

* Installing
  * Windows (MobaXterm) / Linux  `apt-get install mc`
  * MacOS X `brew install mc`

  > If you don't have brew, install it at: http://brew.sh/

Sarah Diehl's avatar
Sarah Diehl committed
300
301
302
* [Tutorial](http://linuxcommand.org/lc3_adv_mc.php)
* if function keys do not work, use `Esc`+`1` for `F1`, `Esc`+`2` for `F2`, ..., `Esc`+`9` for `F9` and `Esc`+`0` for `F10`
* if you do not have an `Insert` key (e.g. on Apple keyboards) use `Ctrl`+`t` for (un-)selecting files and folders
Sergio Coronado's avatar
Sergio Coronado committed
303

Christophe Trefois's avatar
Updates    
Christophe Trefois committed
304
## Scripts - Running and Writing your own
Sergio Coronado's avatar
Sergio Coronado committed
305
***
Christophe Trefois's avatar
Christophe Trefois committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

Sometimes when you download and extract a zip file, it will contain not only files to look at, but also programs or scripts that you want to run.

Have a look at the contents extracted earlier and you will notice a file called `aprogram.sh`

* Run the program using `./aprogram.sh`

> This is a special syntax which tells the terminal to execute a program named `aprogram.sh` in the current folder rather than in the normal folders where all commands are. On Windows "Program Files" and *nix /usr/bin, /usr/sbin/ etc...

* Run the program again using `./aprogram.sh YourName`
* Look at the contents of the program using `nano`

## Create your first bash script!

* Create a new file called `myScript.sh`

> You can use touch and then nano, or you can use nano directly.

* Type in the following two lines

```bash
#!/bin/bash

echo "Hello World"
```

* Save and exit.
* Run the program using `./myScript.sh`
Christophe Trefois's avatar
Christophe Trefois committed
334
335
336
* ERROR. Why?

    We need to tell linux that a given file can be executed and therefore have to give the correct permissions to that file to transform it into a program that can be launched.
Christophe Trefois's avatar
Christophe Trefois committed
337
338
339
340
* To make our script executable do `chmod u+x myScript.sh`
* Launch our first script again


341
* Simple `bash` scripting [Tutotial](http://linuxconfig.org/bash-scripting-tutorial)
Sergio Coronado's avatar
Sergio Coronado committed
342
343
344

## Basic git (source control)
***
Sergio Coronado's avatar
Sergio Coronado committed
345
346
347
* add
* commit
* push
Christophe Trefois's avatar
Christophe Trefois committed
348
* pull