aboutsummaryrefslogtreecommitdiff
path: root/guide.tex
blob: 2a880f18f11b9d841b301674691854806c4b1565 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
\documentclass{book}

\usepackage{manfnt}
\usepackage{hyperref}

\title{Bruh}
\author{Paul Sutton (\texttt{zleap}), Andrew Yu, and Ron Nazarov}

\setlength\parindent{2.3\parindent}

\makeatletter
% Dangerous Bend - Please only use this when there are at least two lines of text in the dangerous paragraph!
\def\hang{\hangindent \parindent}
\def\d@nger{\medbreak\begingroup\clubpenalty=10000
  \def\par{\endgraf\endgroup\medbreak} \noindent\hang\hangafter=-2
    \hbox to0pt{\hskip-\hangindent\dbend\hfill}\normalsize}
\outer\def\danger{\d@nger}
\def\dd@nger{\medbreak\begingroup\clubpenalty=10000
  \def\par{\endgraf\endgroup\medbreak} \noindent\hang\hangafter=-2
    \hbox to0pt{\hskip-\hangindent\dbend\kern1pt\dbend\hfill}\normalsize}
\outer\def\ddanger{\dd@nger}
\def\enddanger{\endgraf\endgroup}

\newcommand\ph[1]{\texttt{\textit{#1}}}
\newcommand\libera[1]{\href{https://web.libera.chat/?channel=\##1}{\texttt{\##1} on \texttt{irc.libera.chat}}}
\newcommand\youtube[2]{\href{https://invidious.snopyta.org/watch?v=#1}{#2}}
\makeatother

\begin{document}
\maketitle

\frontmatter
\chapter{Copyright}

Copyright \copyright{}  2022  Paul Sutton.\\
Copyright \copyright{}  2022  Andrew Yu.\\
Copyright \copyright{}  2022  Ron Nazarov.\\
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled ``GNU
Free Documentation License'' (Appendix \ref{label_fdl}).

\chapter{Preface}

This project has developed from a series of posts on a personal blog site run by zleap. This is a refactoring of that content. This will initially focus on helping people learn the command line for the GNU / Linux operating system.

In turn this will hopefully complement the hosting guides produced by Andrew Yu. As most of those assume at least some knowledge of the command line. So it makes sense to be familiar with this. Learning basic shell scripting, will also help you interpret and modify scripts for your own needs.

This is a big subject area, I am going to start off with a series of posts to take this back to basics, starting off with learning about the command line in modern UNIX-like operating systems and associated commands using the \texttt{bash} shell. Then move on to scripting using Bash.

Although most commands and tips will work on all UNIX-like operating systems, some less common usages and options of these utilities may be absent on some systems. This series focuses on usage of the GNU core utilities with occasional commands from util-linux, as GNU/Linux systems are the most common Free Software setups. Users of other UNIX-like operating systems may consult their system manual for specifics into a command.

This guide is intended for beginners, as some sort of a semi-gentle introduction. However, people with experience already may also find this guide helpful, and as such this guide will cover some 'more advanced' topics. In order to make it possible for many types of readers to read this guide effectively, the  sign
$$\vbox{\hbox{\dbend}\vskip 11pt}$$
is put at the beginning of some paragraphs to indicate a "hard level". If you are new to GNU/Linux systems and it's your first few reads, it is advised to skip these parts. (Yes, this is the ``{dangerous bend}'' sign that Donald Knuth uses.)

\tableofcontents

\mainmatter
\chapter{Preparing the Environment}

$$\vcenter{\hbox{\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend\kern1pt\dbend}\vskip 11pt}$$
This chapter needs to be rewritten.

Firstly, we need access to a GNU/Linux system, there are numerous options here:

\begin{itemize}
\item Install GNU/Linux on your computer;
\item Purchase (or otherwise obtain) computers that use GNU/Linux "by default" or has mainline support thereof, such as these single-board computers:
  \begin{itemize}
  \item \href{https://beagleboard.org}{BeagleBone boards}
  \item \href{https://www.raspberrypi.org/}{Raspberry Pi}
  \item \href{http://www.orangepi.org/}{Orange Pi}
  \item \href{https://www.pine64.org/devices/single-board-computers/rock64/}{ROCK64}
  \item \href{https://www.pine64.org/devices/single-board-computers/pine-a64-lts/}{PINE A64-LTS}
  \end{itemize}
  or these laptops:
  \begin{itemize}
  \item \href{https://www.pine64.org/pinebook/}{Pinebook}
  \item \href{https://www.pine64.org/pinebook-pro/}{Pinebook Pro}
  \item Anything listed on \href{https://ryf.fsf.org/categories/laptops}{the FSF's RYF page}
  \end{itemize}
\item Install GNU/Linux in a virtual machine;
  \begin{itemize}
  \item \href{https://www.qemu.org/}{QEMU}
  \end{itemize}
\item Use a public-access GNU/Linux system, such as \texttt{tilde.team};
\item Use a service from \href{https://www.vern.cc}{Vern.cc};
\item Use a browser-based emulator such as \texttt{https://bellard.org/jslinux/};
\item Use a VM from \libera{tuxiversity};
\item \textit{et cetera}.
\end{itemize}

\chapter{Getting support with this series}

You can get some help via IRC (Internet Relay Chat) -- myself (zleap), Ron (noisytoot), and AndrewYu (Andrew) all hang out on IRC in the channel \libera{tuxiversity}. There are several ways to connect to this via a client. It is probably easier, at least at first, to use a web based interface.

You could also join or help setup a local group such as

\begin{itemize}
\item \href{https://www.codeclub.org.uk}{CodeClub}
\item \href{https://coderdojo.com/}{CoderDojo}
\end{itemize}

The previous section also has links to communities that can also provide help and support.


\chapter{Shell Basics}

We will be mainly working in the \texttt{bash} shell in this series. GNU \texttt{bash} is the most common shell in modern GNU/Linux systems, which is the primary focus of this series.

A \textit{shell} is, in general, the program that you interact with your computer with. When used in a more specific context it refers to a command-line shell that users type commands to. The shell usually does some interpretation on the user input and passes these options launching a program, or they modify the shell's internal environment.

Many users, mostly novice ones, are afraid of command-line interfaces as the stereotypical UNIX command-line is very terse. In rare fields this is still true, but things are generally better than in the old days. For example, visual text editors are widespread among users of bitmap displays and terse teletype-oriented ones like \texttt{ed} are fading away.

Some may ask why the command-line is in consideration at all as we have graphical interfaces that are much more user-friendly. It boils down to the fact that graphical interfaces are usually harder to extend for more features and for specific uses, and even in these cases where you would extend a GUI, knowledge of programming is still necessary, which would be similar to shell scripting. In most cases simple scripts would be easier to write anyway; besides, using the shell day-to-day gives you confidence when you need to write batch operations---and believe me, it made my life much easier.

Now, go ahead and open up a shell. On graphical systems, look for a program called ``Terminal'' or similar. If you see something similar to ``\verb|user@host:~$|'', ``\verb|[user@host ~] $|'', \textit{et cetera}, you're good to go!

\section{What is GNU}

As we are talking about GNU Bash, it makes sense to quickly explain what GNU is. GNU stands for GNU's Not Unix, GNU tools are the tools that came with the original Unix operating system, rewritten and released under the GNU General Public License.\\

\href{https://www.gnu.org/}{The GNU Project}

\begin{itemize}
\item Freedom to use for any purpose
\item Freedom to study the software
\item Freedom to modify
\item Freedom to copy and share the software with others
\end{itemize}

\section{Interpreting the Prompt}

When you see something like ``\verb|user@host:~$|'' or ``\verb|[user@host ~] $|'', the shell is waiting for you to type a command.

The default prompt on most systems consists of four parts:
\begin{enumerate}
\item Your username;
\item Your computer's hostname;
\item Your current working directory;
\item An actual prompt, which is ``\verb|$|'' for normal users and ``\verb|#|'' for root, the superuser.
\end{enumerate}

\danger{The ``\verb|#|'' prompt actually signifies a UID of \verb|0|. If you have a setup where a user that is not ``\verb|root|'' has a UID of \verb|0| (and in this case such a user also has escalated privileges, because it's the UID rather than the username that matters, though most programs are not designed to work like this and can cause issues), its default prompt would also be ``\verb|#|''.}

Just know that such a prompt means that ``the last command (if any) has finished and the shell is waiting for a command''.

\section{Reading the Manual}

The system manual is a good resource for almost any command. To access the system manual, you may use the \verb|man| command.

Now try it!  Type \verb|man| and press the Enter/Return key on your keyboard.
\begin{verbatim}
user@host:~$ man
What manual page do you want?
For example, try 'man man'.
user@host:~$
\end{verbatim}

It looks like that the computer is telling you that \verb|man| doesn't know what manual page to look up \ldots You should think of some keyword to tell \verb|man| to look up as \verb|man| allows you to view specific pages in the system manual. Usually, the entries' names are the name of the program, system call names, standard library function names, file formats (specifically filenames), etc. They are divided into a few sections, which you should read more about with \verb|man man|, i.e. asking the system manual about the manual utility itself. It is wise use the system manual for the command in interest before asking for support---because many times the manual solves your problems, and people don't like repeating a documented solution all the time, hence the ``Read the F* Manual'' ``meme'' spreading around.

When you encounter something that you want to do but you're not sure which command to use, the \verb|apropos| command is useful. \verb|apropos| searches the manual database's page names and summary lines for your search term. For example, if you're looking for a utility to print a file (which is usually \verb|lpr| or \verb|lp|, by the way), you could run \verb|apropos print|, and you'll be presented with a list of related manual entries (though do note how the term ``print'' is used for ``output text onto the screen'' besides ``tell the printer to put ink (or powder) onto paper'' for historical reasons). Since you're looking for a \emph{command} to print, you'd generally want to look in section~1 of the manual (those marked with ``\verb|(1)|'').

Sometimes, you are not looking for the command of an external program, but rather a shell builtin, such as ``\verb|cd|''. In this case, you should use the ``\verb|help|'' command instead of ``\verb|man|''.

\danger{You may have encountered the ``\verb|info|'' command as a system manual command. That is part of the GNU \TeX info manual system, not the traditional UNIX manual system. If you are looking for the manual of GNU utilities, such as ``\verb|bash|'', the \TeX info manuals will be more comprehensive than the manpages, and will generally include more examples and friendly information than what manpages will tell you. However, not many programs outside of GNU utilities have \TeX info manuals.}

\section{Useful general commands}
``\verb|clear|'' clears the screen
``\verb|whoami|'' displays current user name
``\verb|id|''  display user information
``\verb|logout|'' logout of current session
``\verb|date|'' display current date and time
``\verb|ln|'' make links between files

\section{File Operations}

Your UNIX-like operating system has a directory structure, and files are the centre of UNIX-like operating systems. Therefore, learning how to navigate the filesystem and handle files is important.

\subsection{Working Directory}

In any process, there is a \textit{current working directory}. Any filenames that do not begin with a ``\verb|/|'', i.e. those that are not absolute paths, are relative to the process's current working directory. At your shell prompt, you can use the ``\verb|pwd|'' ( present working directory ) command to know your current working directory. This might not seem useful at first as your shell prompt already lists your current working directory, but in the future it will be useful in scripts, where you want to do something like ``save the current location to a variable and return to it sometime later''.

You would see that your shell prompt says that ``\verb|~|'' is your working directory, while ``\verb|pwd|'' says something like ``\verb|/home/user|''. That's because ``\verb|~|'' is an abbreviation to your user's \textit{home directory} (``\verb|$HOME|''), which is usually where you land when you login and is the place a user stores their personal files.

**

this section needs a creative commons diagram of the GNU / Linux directory structure.

**

\subsection{Listing Files}

To list the files and directories of a given directory, use ``\texttt{ls \ph{directory}}''. This lists all the items inside \ph{directory}. If \ph{directory} is unspecified, it lists the current working directory by default.

By default, \verb|ls| does not list hidden files, i.e. files whose filename beings with a dot (``\verb|.|''). If you pass the ``\verb|-a|`` option to it, hidden files are now listed. Note that (``\verb|.|'') and (``\verb|..|'') as independent filenames mean ``current directory'' and ``parent directory'' respectively.

By default, \verb|ls| only lists the filenames of the files. You may want more information. To get more information, you may pass the ``\verb|-l|'' option, which lets \verb|ls| display extra information like filesystem permissions, file sizes (default unit is bytes, use ``\verb|-h|'' for adaptive units), modification dates, \textit{et cetera}.

\subsection{Reading Files}

You may use the ``\verb|cat|'' command to output the data in a specified filename onto ``\verb|cat|'''s standard output (usually the terminal). If multiple filenames are specified as arguments, the files are concatenated.

While the ``\verb|cat|'' command can be used to read files, if the file is long then the output of cat will just scroll the contents up the screen. The ``\verb|less|'' command displays the file content, but allows user interaction to display the file a page at a time.

If you are using ``\verb|less|'' you can press ``\verb|q|'' to quit and return to the bash prompt.

You can also use the ``\verb|more|'' command to view a file a page at a time, as with less, press q to quit.

The `\verb|diff|'' command can be used to display any text that is different between two files. This is useful if you can't remember which is a later version for example.

You can also use the ``\verb|grep|'' to search through a file for specific text,
``\verb|grep pear fruit.txt|'' will search for the text pear in the file fruit.txt.

``\verb|grep|'' stands for GNU Regular Expression Parser and is very powerful so may be discussed later in a video.

The ``\verb|wc|'' command stands for word count, and as you would expect gives the number of words in a document.

\begin{verbatim}
~/test$ wc fruit.txt
 5  5 31 fruit.txt
\end{verbatim}

In this case, 5 lines, 5 words and 31 characters.



\subsection{Creating Files}

To create a file, you can use the ``\verb|touch|'', in this case ``\verb|touch file.txt|'' will create an empty file called file.txt.

``\verb|touch|'' will also allow you to create multiple files, for example ``\verb|touch file.txt file2.txt file3.txt|''

To work with longer files, you can use editors such as ``\verb|nano|'' can also be used so ``\verb|nano file.txt|'' will either open an existing file or create a new file with that file name. You can then edit the file contents.

You can also create files by directing the output of one command in to another. For example, we have previously discussed the ``\verb|ls|'' command, so using ``\verb|ls > filelist.txt|'' will direct the output of ``\verb|ls|'' in to a file called ``\verb|filelist.txt|''. Note that this truncates (i.e. clear) filelist.txt before redirecting the stream. If you want to append instead, use ``\verb|>>|'' instead of ``\verb|>|''.

\danger{It is a bad idea to parse the output of \verb|ls| in scripts. \verb|ls| uses special output formats (dollar symbol notations) and has other caveats for strange filenames. Evaluating the output is potentially a security risk, too. Batch operations should either use shell wildcard or the ``\verb|find|'' command for better robustness.}

\subsection{File manipulation}

Bash allows you to move, copy and rename files.

``\verb|cp|'' copy a file
``\verb|mv|'' move a file or this will also rename a file
``\verb|rename|'' This allows you to rename a file or set of files. Very useful for batch renaming for example:-
``\verb|rename 's/DSCF/newname/' *|'' will take a set of files in this case starting with DSCF and replace that with newname. If you have lots of files on your camera DSCF is an example of a prefix at the start of each photo filename. If you go to the beach you may want to rename that to beach for example, so that set of photos can be identified later. It just renames that portion of the file, so the numerical part remains intact.
``\verb|rm|'' remove or delete a file
``\verb|mkdir|'' create a directory
``\verb|rmdir|'' remove or delete a directory

\subsection{File Permissions}

File permissions are an important security feature in any Unix type environment. Files can either be Read, Write or Executable (e.g a shell script). Depending on who you want to be able to access these files, a file can either be just for the user who created it, a group of users, or anyone else.

The two main commands that are important here are

``\verb|chmod|'' - Changes the file permission, to determine what can be done with it.
``\verb|chown|'' - Changes the file owner
``\verb|chgrp|'' - Changes group owner

\begin{table}[h]
\begin{tabular}{|ccc|ccc|ccc|}
\hline
\multicolumn{3}{|c|}{\textbf{USER}}                               & \multicolumn{3}{c|}{\textbf{GROUP}}                              & \multicolumn{3}{c|}{\textbf{OTHER}}                              \\ \hline
\multicolumn{1}{|c|}{Read} & \multicolumn{1}{c|}{Write} & Execute & \multicolumn{1}{c|}{Read} & \multicolumn{1}{c|}{Write} & Execute & \multicolumn{1}{c|}{Read} & \multicolumn{1}{c|}{Write} & Execute \\ \hline
\multicolumn{1}{|c|}{400}  & \multicolumn{1}{c|}{200}   & 100     & \multicolumn{1}{c|}{40}   & \multicolumn{1}{c|}{20}    & 10      & \multicolumn{1}{c|}{4}    & \multicolumn{1}{c|}{2}     & 1       \\ \hline
\end{tabular}
\end{table}


\subsection{Finding Documents and Files}

The ``\verb|find|'' command will allow you to locate a file or set of files on you system. Basic usage is ``\verb|find -n "file.txt" |''

If you want to find where a particular command is on your system you can use ``\verb|which|''. So an example of this could be ``\verb|which bash|'' the command will then return ``\verb|/usr/bin/bash|''

\subsection{User accounts and passwords}

It is sometimes necessary to add a new user to your system, or remove a user, you can also change a users password if they forget what it is.

\begin{itemize}
\item ``\verb|adduser|'' - add a new user to the system
\item ``\verb|useradd|'' - add a user to a group
\item ``\verb|deluser|'' - delete a user from the system
\item ``\verb|groupdel / delgroup|'' - manage groups on the system
\item ``\verb|addgroup|'' - add group to the system
\item ``\verb|passwd|'' - changes a user's password
\end{itemize}

use of the ``\verb|passwd|'' command usually combines with a user name

``\verb|passwd|'' changes currently logged in users password

\begin{verbatim}
$ passwd
Changing password for user1.
Current password:
New password:
Retype new password:
passwd: password updated successfully
\end{verbatim}

As previously mentioned the ``\verb|man|'' command can be used to find out more information about each of these.

\subsection{Managing Software}

Depending on what distribution you are using, there are several tools that can be used to install or remove software on a GNU / Linux system.

\begin{itemize}
\item ``\verb|apt|'' - Used on Debian and Debian derived systems e.g Ubuntu, Mint
\item ``\verb|rpm|'' - Used on Red Hat, and Red Hat derived systems e.g Fedora, SUSE
\item ``\verb|yum|'' - Frontend for ``\verb|rpm|''
\item ``\verb|dnf|'' - Newer frontend for ``\verb|rpm|'', used on Fedora and Red Hat
\item ``\verb|zypper|'' - Frontend for ``\verb|rpm|'', used on SUSE
\item ``\verb|guix|'' - Used on GNU Guix
\end{itemize}

\subsection{System Administration}

System administration is important. It helps you monitor users, monitor resources etc.
\begin{itemize}
\item[``\texttt{ps}''] lists running processes on your system
\item[``\texttt{top}''] displays processes and allows them to be killed, or change their process priority
\item[``\texttt{nice}'' \& ``\texttt{renice}''] - change process priority
\item[``\texttt{htop}''] more advanced version of top
\item[``\texttt{killall}''] kills a process e.g killall firefox-esr
\item[``\texttt{cron}''] Run a process at a specified time
\item[``\texttt{su}''] Drop to super user - Maintain current user \verb|$PATH|
\item[``\texttt{su -l}''] Drop to super user, using the root user's \verb|$PATH|
\item[``\texttt{sudo}''] super user do, drop to root to just run one command.
\item[``\texttt{reboot}''] restarts the system
\end{itemize}


\subsection{The Path Environment}
 In the previous subsection I mentioned the ``\verb|su|'' and ``\verb|su -|'' lets have a quick look at how the path environment compares, we can view the current path with ``\verb|echo $PATH|''
\begin{itemize}
\item This is the user path /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
\item This is the root path when using ``\verb|su|'' /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
\item This is the root path when using ``\verb|su -|'' /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
\item
\end{itemize}

The difference is mostly that using ``\verb|su -|'' you won't get file not found when trying to run a command that is reserved for the root user.

\subsection{Environment variables}

``\verb|echo $HOSTNAME|'' - Display hostname of system
``\verb|echo $USER|'' - Current user
``\verb|echo $PATH|'' - Display current users path setting

\subsection{Hardware related commands}

On a Unix or GNU / Linux system, everything is a file, this includes hardware, which is represented by a file. All these files are stored in the /dev folder.

``\verb|ls /dev|''

``\verb|lsusb|'' - lists USB devices
``\verb|lspci|'' - lists PCI devices
``\verb|lsblk|'' - lists block devices (e.g hard disks and removable media)

These commands are useful to figure out what is on your system, and where it is. Removable media can be mounted on to a folder, with a command such as ``\verb|mount /dev/usb /media/flash |''  then when you run ``\verb|cd /media/flash|'' you see the files on the device.

It can be a little more involved than this from the command shell though.

The ``\verb|lsblk|'' is very useful for writing USB flash disks, to create a install disk or writing to a SD card in order to create Raspberry Pi disk.

While not fully related to this the ``\verb|dmesg|'' command displays kernel messages, so you can run prior to, and after plugging in a device to see if it has been detected.

\subsection{Restarting an unresponsive system}

This may also be useful - \url{http://tuxdiary.com/2012/06/05/reiub-reset-unusable-linux-box/}

\



\subsection{Videos}

John Collins from \href{https://www.ezeelinux.com/}{EzeeLinux} produced an excellent series of videos covering different topics relating to the BASH shell, We have only touched on this above.

These are hosted on YouTube. For the purpose of this series, the links use the privacy friendly front end on Invidious:

\begin{itemize}
\item \youtube{eH8Z9zeywq0}{Access and Navigation}
\item \youtube{eH8Z9zeywq0}{Editing Text Files}
\item \youtube{s23NqWKxOXk}{Privileges and Permissions}
\item \youtube{4r7V2-EBnR0}{Finding Documentation and Files}
\item \youtube{XVCf0cou6EU}{User Accounts and Passwords}
\item \youtube{lNyJllHk2SA}{Managing Software}
\item \youtube{4\_21KZ3qKEI7}{System Administration Tools}
\item \youtube{57sp8Y0GL40}{Bash Scripting}
\end{itemize}


\section{Editing files}

The nano editor is installed on pretty much every GNU / Linux system, therefore it does make a good starting point, if we want to keep things simple. So before moving on to shell scripting, we should learn how to use this, or at least, open, save and edit files.

\begin{itemize}
\item \href{https://www.nano-editor.org/}{Nano Editor website}
\item \youtube{cLyUZAabf40}{Beginners' guide to nano}
\item \href{https://www.nano-editor.org/dist/latest/cheatsheet.html}{Nano Cheat sheet}

\end{itemize}

\section{Shell Scripting}

Once you have become more used to the command line interface, a shell script can make running the more labour intensive tasks much easier. A shell script is a list of commands within a text file, which will can be run manually or perhaps run at a set time on your system.

\subsection{Getting Started}


\subsection{Checking who you are}

Depending on the script being run and its function, you may want to check the user is running the script as root.

\begin{verbatim}
if [ "$EUID" -ne 0 ]; then
  echo "Please run as root / sudo"
  exit
fi
\end{verbatim}

\subsection{A basic script}

Shell scripts start with ``\verb|#!|'' followed by a path to the interpreter, for example ``\verb|/bin/sh|''.
There is no standard location for where the interpreter is located, but ``\verb|/usr/bin/env|'' often (but not always) exists.
It is possible to use ``\verb|env|'' to find an interpreter without specifying the full path, for example:

As shell scripts can save typing lots of command then the following will display the username and hostname

\begin{verbatim}
#!/usr/bin/env bash
echo $USER
echo $HOSTNAME
\end{verbatim}

\subsection{Environment variables}

Which isn't very friendly in some ways so, using
\begin{verbatim}
echo "hello you are, $USER at $HOSTNAME"
\end{verbatim}
Is probably a little more friendly.

The ``\verb|printenv|'' command will display currently set environment variables.

You can find more about environment variables at \href{https://www.howtogeek.com/668503/how-to-set-environment-variables-in-bash-on-linux/}{How-To Geek - environment variables}.

This can be very useful witin scripts as you can then run a test to see if a particular user is logged in and display a personal message for example.

\subsection{.bashrc file}

The ``\verb|.bashrc|'' file runs when you login or stat a new terminal session. Any instructions contained within are run. So you could put echo "Hello" a the end, and that will be displayed. The innards of this file are beyond the scope of this series, however once you are familar with shell scripts a little more, you will understand what is going on.

\subsection{Alias a command}

So within bash, you can run various commands, each of these has a default behaviour. The ``\verb|ls|'' command just lists files, with no details, but what happens if you want to change this you can use the alias command within a script.  In this case the following appears in my ``\verb|.bashrc|'' file. It may be commented out with ``\verb|#|''.

In this case ``\verb|ls|'' works normally, but typing ``\verb|ll|'' runs ``\verb|ls -l|'' therefore displaying the files in long format with permission and ownership details.

\begin{verbatim}
alias ll='ls -l'
\end{verbatim}

\chapter{Next Steps}

\section{Networking}

It is a good idea to understand networking concepts, this is especially useful if you want to host your own web services.

\youtube{\_IOZ8\_cPgu8}{Networking Beginners}

\begin{itemize}
\item a switch
\item router
\item gateway
\item subnet
\item gateway
\item firewall
\item DMZ
\end{itemize}

As this is a series on bash, then the above will give you an introduction to networking. However within bash there are several tools for networking.

``\verb|ping|'' sends a packet of data to the specified remote computer, this helps to check the remote computer is responding.

``\verb|ping 127.0.0.1|'' will therefore ping the computer you are on, as \texttt{127.0.0.1} represents \texttt{localhost}. This will keep going until stopped with ``\verb|ctrl-d|''. You can ping other computers on your network or on the wider internet. Please \emph{do not flood} other computers.

``\verb|ip addr show|''

Shows local network information for each of the systems network interfaces

\begin{itemize}
\item ``\verb|hostname|'' will display the current system hostname
\item ``\verb|hostname -i|'' will display the localhost address
\item ``\verb|hostname -I|'' will display system IPv4 and IPv6 address
\end{itemize}

see ``\verb|man hostname|'' for full details

\begin{verbatim}

random text to fill in for now

\end{verbatim}



\section{OpenSSH}

OpenSSH is a part of a suite of tools that allow secure remote access to systems. Open Secure Shell.

\href{https://www.openssh.com/}{OpenSSH server}

Install with ``\verb|apt install openssh-server|''

SSH can be used with ``\verb|ssh-keygen|'' to generate a ssh keypair, which can be used to login to a remote system without a password. This is a good option to maintain security.

To generate a key pair with ``\verb|ssh-keygen|''


\begin{verbatim}

testuser@Desktop:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/testuser/.ssh/id_rsa):
Created directory '/home/testuser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/testuser/.ssh/id_rsa
Your public key has been saved in /home/testuser/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:10eg+9WU5kaiaicN4oLP+0Y2L/FmCwKygIQjwo9mNq4 testuser@Desktop
The key's randomart image is:
+---[RSA 3072]----+
|            .    |
|o          . .  .|
|=o        .  ..+.|
|=.o        o..=o |
|oB o   .S.o.. oo.|
|* + o .=..+. o.  |
| o . ooo=+ o.    |
|.   o ooo=o      |
|E    ++.+..      |
+----[SHA256]-----+

\end{verbatim}

I have just accepted the defaults by pressing enter. for both the file name and setting and confirming a passphrase.

If you now type ``\verb|cd .ssh|'' you will find two files.

\begin{verbatim}
id_rsa  id_rsa.pub
\end{verbatim}

\verb|id_rsa| is your \textbf{Private} key and should be treated as such. \verb|id_rsa.pub| is your \textbf{Public} key and can be shared.

It is possible to specify the type of key to be generated with the ``\verb|-t|'' option, which can be used like:

\begin{verbatim}
ssh-keygen -t ed25519
\end{verbatim}

The default is ``\verb|rsa|'', but ``\verb|ed25519|'' is a more modern and faster, but still just as secure, algorithm.

\section{Git}

Git is a tool to help manage software projects, git is used in combination with sites such as \href{https://github.com/}{GitHub}, however there are several alternatives such as
\href{https://codeberg.org/}{Codeberg}, \href{https://sr.ht/}{Sourcehut} and others.

Git is clearly a very powerful tool, learning git is an important skill, but it can be complex and confusing at times, so it is really useful to be able to ask for help.



\section{Self Hosting}

Andrew Yu has produced a really good guide to self hosting, this can be found at

\href{https://host.andrewyu.org/}{Andrew Yu Hosting guide}

This covers :

\begin{itemize}
\item Get a domain name
\item Get a server
\item Set up DNS settings to connect your server and domain name
\item Set up your web server
\item Get a secure HTTPS connection with Certbot
\end{itemize}

\href{https://networkverge.com/common-ports/}{Common network ports}

There are also other guides to set up other types of server, or at least links to.

\section{Gaming}

Gaming may not be something that seems directly related to this. You may be interested in running a game server, for friends or to take part in events such as:

\href{https://libregaming.org/}{Libre gaming night}

There is a related IRC Channel on Libera Chat -- \libera{libregamenight}

\chapter{Hacking and Hackers}

To avoid any confusion, ``\verb|hacker|'' and ``\verb|hacking|'' are described in the original meaning. Hacking is creating something useful or clever from code. A hacker is a person who enjoys the use of programmable systems. So in a similar contect ``\verb|ethical hacking|'' and ``\verb|ethical hacker|'' would fit in here.

The term ``\verb|cracker|'' is taken within the original meaning and would be a person undertaking illegal activities, such as breaking in to computer systems. This definition makes more sense as you have 'safe crackers' or back in World War 2, the enigma code was 'cracked', however the people that did that are considered hackers.




\appendix
\input{fdl-1.3.tex}

\end{document}