forked from lswest/cli-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLI Cookbook.tex
685 lines (531 loc) · 36.4 KB
/
CLI Cookbook.tex
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyvrb}
\usepackage{color}
\usepackage[linktoc=all]{hyperref}
\hypersetup{
colorlinks,
citecolor=black,
filecolor=black,
linkcolor=black,
urlcolor=black
}
\begin{document}
\title{CLI Cookbook}
\author{Lucas Westermann with E. Frank Sandig \\ and other readers of Command \& Conquer}
\maketitle
\textbf{Note: Each command in a longer list is separated by a blank line. All commands on more than one line without a blank line are simply wrapped text - run it as one command! } \\
You can find this on GitHub: \url{https://github.com/lswest/cli-cookbook}\\
If you want to contribute - feel free to clone the repository, or open an issue (if you aren't comfortable with \LaTeX).
\pagebreak
\tableofcontents
\pagebreak
\newcommand\codeHighlight[1]{\textcolor[rgb]{1,0,0}{\textbf{#1}}}
\section{Get information on commands}
\label{Get information on commands}
\subsection{Get a brief summary of bash commands}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{whatis} command
\end{Verbatim}
Replace command with the name of the command you're curious about, and whatis should return a brief summary of what it does (I say should, as not all programs register themselves in the whatis database).
\subsection{Get the location of a command's binary}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{whereis} command
\codeHighlight{which} command
\end{Verbatim}
Whereis and which both return the path to the main executable of the command.
\section{Finding and managing files}
\label{Finding and managing files}
\subsection{sed}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sed}
Example: sed ‘s/[pP][iI][nN][kK] [fF][lL][oO][yY][dD]/Pink Floyd/g’
\end{Verbatim}
Sed can be used for word replacement in files. The example supplied will consolidate all spelling variations of Pink Floyd. It's also frequently used to unify file types (i.e. .JPG .jpeg .jpg into one uniform extension).
\subsection{ls}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{ls -a|grep} \$searchterm
Note: Replace \$searchterm with the term you're looking for.
\end{Verbatim}
This combination has ls list all files (including hidden files and directories), and passes the resulting list to grep, which then filters it according to your search term. The manpage of grep will give you the syntax to negate searches ("find everything that doesn't contain $X$").
\subsection{convert command}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{convert}
Example: convert *.jpg output.pdf
\end{Verbatim}
Convert is a powerful command-line tool that helps to convert between formats. However, it can also be used to merge files together. The example above takes all files that end in .jpg and merges them into a single PDF. The reader who supplied this notes that they find it most useful when scanning large numbers of files. Convert is part of the imagemagick package.
\subsection{Search with updatedb and locate}
\label{locate}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sudo updatedb} # updates database
\codeHighlight{locate} search term
\end{Verbatim}
The locate command quickly searches the database for items matching the search terms supplied. It won't always find the file you're looking for, but is faster than find if you don't know what folder it's in.
\subsection{Search with find}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{find }\$path\codeHighlight{ | grep -Ri "}search term\codeHighlight{"}
\codeHighlight{find }\$path\codeHighlight{ -name "}search term\codeHighlight{"}
\codeHighlight{find }\$path\codeHighlight{ -iname "}search term\codeHighlight{"}
\end{Verbatim}
Replace \textbf{search term} with your search terms. Find will also search from the specified path onwards (for current path, just use '\textbf{.}' minus the quotes). I wouldn't recommend using find to search through your directory from root ('/'), as it could take forever. Much better to use locate there. The only difference between -name and -iname is that -iname is case insensitive, which means .JPG and .jpg are treated as the same text.
\subsection{Search every file recursively}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{find . -type f |xargs grep '}'text to search\codeHighlight{'}
\end{Verbatim}
The find command searches all files (-type f) in the current directory and below, using the search terms supplied to grep and parsed through xargs.
\subsection{Convert files using LibreOffice}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{soffice --headless --nologo --convert-to odp *.ppt}
# set source and destination formats as desire
\end{Verbatim}
Similar to the convert command, this can convert between file formats (any formats you can use/create in LibreOffice). The first two arguments are to hide the LibreOffice interface, and the third argument tells it what to do. The reader who supplied this command mentioned that they found it useful for converting the PowerPoint slides they received from their professors to PDF or ODP. However, converting the latest Microsoft formats (.docx, .pptx, etc.) can/will result in formatting issues in LibreOffice 3.5 in Ubuntu 12.04, as well as in LibreOffice 4.1 in Ubuntu 13.04. Ubuntu 12.10 has not been tested yet.
\section{Packages}
\label{Packages}
\subsection{Debian based systems}
All the following commands are for Debian based systems (which is what Ubuntu is). In the case of trusted keys - not all Debian systems have them.
\subsubsection{Remove leftover configuration files from removed packages}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sudo dpkg --purge `dpkg -l | grep ^rc | awk '\{print \$2\}'`}
\end{Verbatim}
\subsubsection{Create a detailed list of installed packages}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{COLUMNS=200 dpkg-query -l
> packages_list.list}
\end{Verbatim}
COLUMNS is a variable being passed to dpkg-query, and isn't a mandatory part of the command. In this case, however, it helps to format the results for the resulting text file. It essentially queries dpkg for a list of installed packages. The resulting list will contain package names, versions and descriptions. Good for looking up what’s installed and what is done by which package. Alphabetical order.
\subsubsection{Create a list of installed packages for restoring your system}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{dpkg --get-selections | awk '!/deinstall|purge|hold/ \{print \$1\}'
> packages.list}
\end{Verbatim}
This list contains only package names of installed packages. One per line. It is therefore suitable for re-installing all packages from the command line (see below). Alphabetical order.
\subsubsection{Create a list of automatically installed packages}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{apt-mark showauto > package-states-auto}
\end{Verbatim}
This command generates a list containing the names of all packages that have been installed automatically due to dependency reasons, and saves it to the file package-states-auto. This is needed for restoring an installation properly.
\subsubsection{Restore a list of packages}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{dpkg --get-selections | awk '!/deinstall|purge|hold/ \{print \$1\}'
> packages.list}
\end{Verbatim}
\subsubsection{Save all software sources to one file}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{find /etc/apt/sources.list -type f -name '*.list' -exec bash -c 'echo }
\codeHighlight{-e "\textbackslash n## \$1 ";grep "^[[:space:]]*[^#[:space:]]" \${1}' _ \{\} \textbackslash; }
\codeHighlight{> sources.list.save}
\end{Verbatim}
This long command finds all entries in sources.list and the sub-directories, putting the entire list in a file called sources.list.save.
\subsubsection{Save the keys of trusted software sources}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sudo cp /etc/apt/trusted.gpg trusted-keys.gpg}
\end{Verbatim}
This command simply makes a copy of the trusted key list (stored under /etc/apt/) and copies it to the current directory.
\subsubsection{Restore installation}
\begin{Verbatim}[commandchars=\\\{\}]
# firstly, restore sources manually!
\codeHighlight{sudo apt-key add trusted-keys.gpg} # import keyring
\codeHighlight{sudo apt-get update} # update sources
\codeHighlight{xargs -a "packages.list" sudo apt-get install} # install software
\codeHighlight{xargs -a "package-states-auto" sudo apt-mark auto} # set package state
\end{Verbatim}
The way I would recommend restoring the software sources is to copy and paste sources not already present in the existing sources.list file. It can be tedious, but can avoid issues with different versions and installations. The reader who supplied this said that they apply the above procedure to save time when re-installing a system. Or when they have to create a few identical installations.
\subsection{Archlinux}
\subsubsection{Create list of installed packages}
\label{Arch-installed-packages}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{pacman -Qqe | grep -v "\$(pacman -Qqm)" > pkglist-off.txt}
\codeHighlight{pacman -Qqm > pkglist-loc.txt}
\end{Verbatim}
The first command creates a list of all officially installed packages (i.e. nothing from the AUR). The second command creates a list of all locally installed packages. If you use an AUR manager, copy the install package over along with the backup list - or else make note of the URL so you can wget it later.
\subsubsection{Create a local copy of installed packages}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{cp /var/cache/pacman/pkg/*} \$backup-location
\codeHighlight{sudo pacman -Sc}
\end{Verbatim}
The first command will copy the local cached copies of installed packages. However, pacman stores old copies too, so the second command will remove all the old copies, in case space is an issue. Most AUR managers offer a similar option of caching files (not necessarily enabled by default).
\subsubsection{Restoring package installations}
\begin{enumerate}
\item Install Arch as you would usually from a live CD using the AIF (Arch Installation Framework).
\item Mount backup device (in live CD)
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{mkdir /backup-files}
\codeHighlight{mount }/dev/<disk-drive-partition>
\codeHighlight{/backup-files}
\end{Verbatim}
\item Now copy these to the newly created Arch install (mounted under /mnt)
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{mkdir -p /mnt/opt/restore}
\codeHighlight{cd /backup-files}
\codeHighlight{cp -a * /mnt/opt/restore}
\end{Verbatim}
\item Now chroot (change root) to the new Arch install
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{cd /mnt}
\codeHighlight{cp /etc/resolv.conf /mnt/etc}
\codeHighlight{mount -t proc none /mnt/arch/proc}
\codeHighlight{mount -t sysfs none /mnt/arch/sys}
\codeHighlight{mount -o bind /dev /mnt/arch/dev}
\codeHighlight{chroot . /bin/bash}
\end{Verbatim}
\item OPTIONAL: If you made a local copy of the cache, copy the cache:
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{cp /mnt/opt/restore/pkg/ /var/cache/pacman/pkg/}
\end{Verbatim}
\item Now install the official packages into the new system
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{pacman -Sy}
\codeHighlight{pacman -S --needed \$(cat /opt/restore/pkglist-off.txt)}
\end{Verbatim}
\item If you have AUR packages installed, install the AUR helper, and then do the following
\begin{Verbatim}[commandchars=\\\{\}]
\$AURhelper \codeHighlight{-S \$(cat /opt/restore/pkglist-loc.txt | grep -vx }
\codeHighlight{"\$(pacman -Qqm)")}
# Replace \$AURhelper with the name of your preferred helper
\end{Verbatim}
The above command installs all packages listed in the local files backup from \hyperref[Arch-installed-packages]{\ref*{Arch-installed-packages} }.
\end{enumerate}
This long process will result in having your old packages installed into a new system. This can help make the occasional re-install infinitely easier. Or, if you have a system you're completely satisfied with, you can then move it to a new system. The ArchWiki has plenty of information for those of you trying to move more than just the installed packages.
\section{Hard disk commands}
\label{Hard drive commands}
\subsection{Full list of partitions}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sudo fdisk -l} # for legacy BIOS
\codeHighlight{sudo gdisk -l} # for GPT (UEFI) systems
\end{Verbatim}
These commands will print a list of all hard disks and their partitioning structure. If you're unsure of which command to use, fdisk is probably for you. Don't worry - no harm running the wrong command, and it will prompt you that you ought to be using the other command.
\subsection{Back up MBR (Master Boot Record)}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sudo dd if=/dev/hda of=mbr_backup bs=512 count=1}
\end{Verbatim}
This command runs dd on the "input file" (if) of /dev/hda (replace this with the disk you want to back up the MBR of \- most likely /dev/sda). It then reads the first 512 bits (bs=512 \- size count=1 - how many chunks of size 512 to read) and saves them in the "output file" (of) mbr\textunderscore backup. If you're running this from a live CD or disk, remember to save/move the output file to a persistent drive (i.e. hard drive, USB stick, etc.). Otherwise it will be lost the moment you reboot.
\subsection{Back up boot code}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{dd if=/dev/hda of=bootcode_backup bs=446 count=1}
\end{Verbatim}
Similar as with the MBR command, this command backs up a section of the hard drive. In this case, it backs up only the section of the hard drive that contains boot code.
\subsection{Display detailed information on your hard disk}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sudo hdparm -I} /dev/sdx
\end{Verbatim}
This command lists thorough information about the supplied hard disk (replace /dev/sdx with your actual drive).
\subsection{Correct partitioning order}
According to the reader who submitted this:
"\textit{Due to using gparted over and over again for my external 2Gb harddisk my partitioning was off (first p2, then p3, p1 was the extended, in there p7, then p5, then p6).
I corrected this by doing:}"
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sudo sfdisk -d }/dev/sdb\codeHighlight{ > }sdb.old
\codeHighlight{sudo cp }sdb.old sdb.new
\codeHighlight{sudo gedit} sdb.new
\# Change order, don’t forget p4 which did not exist
\codeHighlight{sudo sfdisk }/dev/sdb\codeHighlight{ < }sdb.new
If it's wrong, do the following:
\codeHighlight{sudo sfdisk }/dev/sdb\codeHighlight{ < }sdb.old
Then retry the gedit step.
\end{Verbatim}
I'm honestly of two minds about this suggestion. On one hand, I can accept doing this on usb sticks - though I don't really think you'll need more than one partition on any but the largest. Doing this on any kind of actual hard drive seems extremely dangerous as countless things could go wrong. An out of sequence partition table isn't a problem in any way, so only risk this \underline{\textbf{\textit{if you can't possibly live with it.}}}
\subsection{Fill hard disk with zeros}
\begin{Verbatim}[commandchars=\\\{\}]
\textbf{BE CAREFUL!!!}
\codeHighlight{dd if=/dev/zero of=}/dev/sdb \codeHighlight{bs=64k}
\textbf{BE CAREFUL!!!}
\end{Verbatim}
This command can be extremely dangerous to use if you're not sure what you're doing. It writes to the disk and fills it with zeros, effectively wiping the hard drive. For the security-conscious of you, doing this a couple of times on a drive before throwing it away/giving it away, it will make it much more difficult to restore data from the drive. It's best used in conjunction with the following command. Be sure to replace /sda/sdb with the correct path.
\subsection{Fill hard disk with random data}
\begin{Verbatim}[commandchars=\\\{\}]
\textbf{BE CAREFUL!!!}
\codeHighlight{dd if=/dev/urandom of=}/dev/sdb
\textbf{BE CAREFUL!!!}
\end{Verbatim}
This command is extremely dangerous to use blindly without understanding what you're doing. This is because, similar to the previous command, it fills the hard drive with data. Instead of zeros, it instead uses random data. The reader who submitted this code mentions as well that they do this with old drives before selling them. They also mention that their preference is to re-format them as \textbf{NTFS} at the end as well. Remember to replace /dev/sdb with the correct path.
\subsection{Get UUIDs of your hard disk partitions}
\begin{Verbatim}[commandchars=\\\{\}]
sudo \codeHighlight{blkid}
\end{Verbatim}
Blkid prints the UUIDs of your hard disk partitions. It can also be limited to one hard disk or one partition by just passing /dev/sda1, for instance, to blkid. This is useful if you had to reformat your /home or another partition or if you have wiped your swap and reformatted it using the dd command above and your partitioner of choice. After you know the UUIDs, you can put them in your fstab so the automount at startup works properly with the newly formatted partitions.
\section{Password generation}
\label{Password generation}
\subsection{Numeric passwords}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{makepasswd --string 0123456789 --chars 32}
\end{Verbatim}
This command generates a single 32-character long numeric password. \textbf{Makepasswd is not always installed by default.}
\subsection{Alphanumeric passwords with special characters}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{pwgen -sy 32 64}
\end{Verbatim}
This command generates 64 passwords of a 32-character length that contains letters, numbers and special characters. The reason why we show how to generate multiple ones at first is because it can be useful when setting up a large number of networks at once. If you leave out the y switch, no special characters (y for symbols) are contained in the passwords. So they are easier to remember, but less secure. \textbf{Pwgen is not installed by default.}
\section{wget}
\subsection{Make an offline copy of a website}
\label{}Make an offline copy of a website
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9b5)}
\codeHighlight{Gecko/2008050509 Firefox/3.0b5" -r -k -E -l 8} http://example.com
\end{Verbatim}
This command uses wget to impersonate Firefox's user agent, and then runs recursively through the website (-r defaults to 5 levels, and -l sets the depth, to 8 in this case). The -k switch tells wget to fix the links to make them suitable for local viewing, -E tells it to adjust extensions (i.e. change .asp to .html). Don't forget to change the URL!
\subsection{Download a file (with the ability to continue)}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{wget -c }http://example.com/file.ext
\end{Verbatim}
Wget grabs the supplied link and downloads the file - in the example it would download the file.ext file. The -c switch lets you continue a download if it's interrupted.
\section{Display hardware information}
\label{Display hardware information}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{lshw -C} SECTION
Example: sudo lshw -C network
# lists all information about networking devices
Formatting example (HTML): sudo lshw -html > hardware.html
#creates an html page with the lshw results
\codeHighlight{lsusb}
Example: sudo lsusb -v | more
# lists verbose output and drops it into the more tool for easy reading.
\codeHighlight{lspci -nn}
# -nn prints also device and vendor id. Useful if you’re looking
for a driver, e.g. for WLAN, using databases at ubuntuusers.de or other
\codeHighlight{dmesg | grep KEYWORD}
\codeHighlight{ifconfig}
\codeHighlight{iwconfig}
\codeHighlight{dmidecode -t TYPENUMBER}
\end{Verbatim}
Logically, lsusb prints information about USB devices, and lspci lists information about PCI devices (system bus). Lshw lists information about almost all hardware in your computer. Dmesg prints out system messages, ifconfig prints hardware information about ethernet and wireless interfaces, and iwconfig display information on wireless connections (such as ESSID). Dmidecode dumps a computer's DMI (sometimes called SMBIOS) table contents into a human-readable format. SMBIOS stores specific information about the computer.
The name for the SECTION (called class by lshw) can be found by running either of the following commands:
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{lshw -businfo}
\codeHighlight{lshw -short}
\end{Verbatim}
\section{View running processes}
\label{View running processes}
\subsection{htop}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{htop}
\end{Verbatim}
Htop is a more functional (I find) version of top. It's not always installed by default, but I have yet to find an official repository that doesn't offer the package.
\subsection{View logged in users and processes they own}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{w}
\end{Verbatim}
The command "w" lists all logged in users and the processes associated with those accounts.
\section{Administration}
\label{Administration}
\subsection{Add user to group}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{usermod -aG ‘}groupname\codeHighlight{’ ‘}user\codeHighlight{’}
\end{Verbatim}
This command modifies a user and adds them to the group specific as groupname.
\subsection{Create aliases in your shell}
\label{aliases}
This applies to almost any shell, though the configuration file will be different. I will assume the use of the Bourne Again Shell (Bash) and therefore the file .bashrc.
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{vim ~/.bashrc}
Then add this anywhere in the bashrc file:
\codeHighlight{alias }\$name\codeHighlight{="}command\codeHighlight{"}
This alias will then be active in any new shell you open.
If you want to use it in a currently open shell, run this:
\codeHighlight{source ~/.bashrc}
\end{Verbatim}
After that, just type whatever the name of your alias was, and it will run the specified command. Remember to replace \$name and command with the actual name you'd like the alias to have, and the command you want it to run.
\subsection{Check who opened which ports}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{sudo netstat -lntup}
\end{Verbatim}
This is a fairly straightforward command - it lists users and the ports they have opened.
\section{Miscellaneous fixes}
\subsection{Upside down webcam in Skype}
Before using this command, you should find the path of your v4l1compat.so file. For information on how to find it, see \hyperref[locate]{\ref*{locate}} (using updatedb and locate)
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{bash -c 'LD_PRELOAD=}/usr/lib/i386-linux-gnu/libv4l/v4l1compat.so \codeHighlight{skype'}
#if you're on 11.10, you'll need the following:
\codeHighlight{sh -c 'LD_PRELOAD=}/usr/lib/i386-linux-gnu/libv4l/v4l1compat.so
\codeHighlight{/usr/bin/skype "\$@"'}
\end{Verbatim}
Since you most likely don't want to type this every time, you can alias it in your bashrc file (\hyperref[aliases]{\ref*{aliases}}), or else create a small bash script executable.
\subsection{GRUB boots into black screen}
Occasionally some graphics cards have issues booting into GRUB's framebuffer, which results in a black screen. If you run into this issue, you can try adding \textbf{nomodeset} to the end of your kernel boot line. GRUB2 and legacy GRUB have different ways to edit entries, but the bottom of the GRUB menu should have instructions.
\subsection{Reinstall GRUB using chroot}
Chroot lets you work on an Linux installation that is currently not bootable. This can happen when you set up a dual boot system and install Windows after Linux. Sometimes it happens that you have no other option than reinstalling Windows to fix it. \\ Firstly, boot into a live Linux, e.g. the latest Ubuntu live CD, then open the Terminal.
\begin{Verbatim}[commandchars=\\\{\}]
sudo mount /dev/sda2 /mnt
sudo mount -o bind /dev /mnt/dev
sudo mount -o bind /sys /mnt/sys
sudo mount -t proc /proc /mnt/proc
sudo \codeHighlight{chroot} /mnt /bin/bash
grub-install /dev/sda
exit
\end{Verbatim}
Replace sda2 with your / partition. In Ubuntu live mode, sudo will not prompt for a password. This example works only if you have no seperate /boot partition. If you have not located your /home on a seperate partition, it can happen that you are not the owner of your /home/user directory any more, which leads to problems. You can easily fix that using this command after booting into the installed Linux:
\begin{Verbatim}
sudo chown -hR user:user /home/user
\end{Verbatim}
If you have a seperate /boot partition, the command list is as follows:
\begin{Verbatim}
sudo mount /dev/sda2 /mnt
sudo mount /dev/sda3 /mnt/boot
sudo mount -o bind /dev /mnt/dev
sudo mount -o bind /sys /mnt/sys
sudo mount -t proc /proc /mnt/proc
sudo \codeHighlight{chroot} /mnt /bin/bash
cp /proc/mounts /etc/mtab
grub-install /dev/sda
exit
\end{Verbatim}
Again, you have to replace sda2 and sda3 with your actual partitions.
\section{Video tips}
\subsection{ffmpeg}
\subsubsection{Record your desktop}
If you don't mind using a pre-build tool, using recordmydesktop is probably easiest. However, if you want total control over the recording, you can use ffmpeg
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -r 20 -s 1680x1050+0+0 -i :0.0 }
\codeHighlight{-acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 -y }
\codeHighlight{vidcap.mkv}
\end{Verbatim}
\subsubsection{Use ffmpeg to cut a section of video out}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{ffmpeg -ss} 00:02:23\codeHighlight{ -i} input.foo\codeHighlight{ -t }25\codeHighlight{ -vcodec copy -acodec copy} output.foo
\end{Verbatim}
This command tells ffmpeg that starting from 2 minutes and 23 seconds into the video, to copy out the following 25 seconds (-t 25) from input.foo into output.foo
\subsection{Use vdpau (hardware accelerated decoding) in mplayer}
Nvidia uses the vdpau library to offload video encoding and decoding to the graphics card. To use this in mplayer, do the following:
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{mplayer -vo vdpau -vc ffh264vdpau }file
\end{Verbatim}
Replace the word "file" with the path to the video you want to play.
\section{Encrypted thumbdrive installation}
By the time I write this, everyone is talking and writing about encryption and keeping one's personal data save. This chapter shows how to create a fully encrypted, persistent installation of Linux on a USB thumbdrive, using the command line a lot. This is a larger project, as it takes a lot of time. It is recommended that you already have some experience in normal installation and using the command line. The procedure below has been tested with Backtrack 5R3, Ubuntu 10.04 LTS and Backbox Linux 3.05, all 32bit. It should also work similarily with Ubuntu 12.04 LTS as well as Debian Wheezy and other distros, but this is still to be tested. In this tutorial I will assume you have a Debian based distro.
In Ubuntu, you can easily have encrypted /home and swap on your laptop by just checking one box during the installation process. But this tutorial takes paranoia to a new level: Firstly, everthing exept the /boot partition is going to be encrypted. Secondly, the drive is removeable. So you even can put it in a safe. Should be even a bit more theft proof.
As Ubuntu recognizes new hardware at each startup, you can even use the thumbdrive we create below on several machines.
At last, I recommend not to create the encrypted drive on your productive machine. As we perform hard disk operations, you can easily break your system by misspelling a command. Use a spare computer or a less important one, if you have one. We will take no responsibilty for data loss resulting from the application of this tutorial.
\subsection{What you need to start}
\begin{enumerate}
\item A USB thumbdrive, at least 16GB. I recommend 32GB to have more space for your data, but of course you can use an even larger one if you need more space in your /home. 8GB may be sufficient for the installation of Ubuntu, for instance, but you quickly run out of space if you really use the stick, save some larger files and install some more software.
\item A live Linux, best is the Linux you want to install on the destination thumbdrive. You can use a distro on a second thumb drive (e.g. 2GB) or a DVD. Last time I created the encrypted boot stick I used Backbox Linux.
\item Working internet connection.
\end{enumerate}
\subsection{Steps to carry out}
\subsubsection{Boot your live Linux}
Sometimes you have to select your CD/DVD drive or thumbdrive as primary boot device in your BIOS. Usually, this step is not much of a problem. The latest live distros recognize a lot of different hardware, so booting usually does not go wrong. If it does, you can always try another distro or some boot parameters. You can find help on those problems on the community pages of your distro.
\subsubsection{Get the internet connection working}
This is also not much of a problem with new distros. Operating systems like Ubuntu usually make it work out of the box. Wired connection will default to DHCP. For Wifi, you usually just have to select the network, hand your system the key and click "connect". For some newer Wifi cards it can be necessary to install e.g. the package linux-firmware-nonfree first or do something else. If any problems occur, the community of your chosen distro can help you.
\subsubsection{Partition your thumbdrive}
Pick your partitioner of choice. Gparted does a good job under Ubuntu, but of course it can be done with fdisk on the command line as well. You need one /boot partition, that is not going to be encrypted. I recommend creating a partition of about 2GB and formatting it to ext3. The rest of the thumbdrive should be a logical partition in an extended one. You don't have to format that second partition now. We will create the encrypted volume there later. With fsdisk, the commands are as listed below:
\begin{Verbatim}[commandchars=\\\{\}]
sudo fdisk /dev/sdb
# delete existing parttions, maybe more than one
d [enter]
1 [enter]
# create first partition
n [enter]
p [enter]
1 [enter]
[enter]
+2048M [enter]
# create extended partition
n [enter]
e [enter]
2 [enter]
[enter]
[enter]
# create logical partition
n [enter]
l [enter]
[enter]
[enter]
# set the first partition as ext3
t [enter]
1 [enter]
83 [enter]
\end{Verbatim}
Make sure that the first partition is set active. I don't know how to do that with gparted, but it's easily done with fdisk as follows:
\begin{Verbatim}[commandchars=\\\{\}]
sudo fdisk /dev/sdb
a [enter]
1 [enter]
w [enter]
\end{Verbatim}
In this tutorial, sdb is the thumb drive. You may have to change that on your system. You get a good overview of your drives and partitions to find proper name in gparted or by the following command:
\begin{Verbatim}[commandchars=\\\{\}]
sudo fdisk -l
\end{Verbatim}
\subsubsection{Install needed software}
We need software for logical volume management (lvm) and encryption (luks). You get it installed with:
\begin{Verbatim}[commandchars=\\\{\}]
sudo apt-get update && sudo apt-get install hashalot cryptsetup lvm2
\end{Verbatim}
\subsubsection{Fill the installation partition with random data}
This step is not necessary, but levels up your paranoia skills. With an encrypted volume in a partition filled with random data, it is very hard to even tell where your data actually reside. This step will take some time, as some GBs of random data have to be written. We can accomplish it with our old friend dd as follows:
\begin{Verbatim}[commandchars=\\\{\}]
dd if=/dev/urandom of=/dev/sdb5
\end{Verbatim}
Due to sdb being our thumbdrive, the second partition is called sdb5 because it's the first logical one.
\subsubsection{Set up encryption}
The following command will set up encryption on our installation partition. I think the selected options are a good trade-off between performance and security.
\begin{Verbatim}[commandchars=\\\{\}]
cryptsetup -y --cipher aes-xts-plain --key-size 512 luksFormat /dev/sdb5
\end{Verbatim}
Cryptsetup will ask if you are really sure and state that you will lose all data on the destination partition. If you are sure to have named the correct partition, type uppercase YES. After this, cryptsetup prompts two times for your passphrase. I recommend using a real phrase of at least five random words or even passwords you can remember instead of just a single password. Correct horse battery staple is already taken ;) After cryptsetup has set up encryption, we can open the encrypted partition for the further steps using:
\begin{Verbatim}[commandchars=\\\{\}]
cryptsetup luksOpen /dev/sdb5 pvcrypt
\end{Verbatim}
Cryptsetup will prompt for your passphrase.
\subsubsection{Create logigal volume}
The following commands set up the physical volume, volume group and our logical volume for the root partition. The last command sets up an ext4 file system on the newly created logical volume, which we will use later for the installation.
\begin{Verbatim}[commandchars=\\\{\}]
pvcreate /dev/mapper/pvcrypt
vgcreate vg /dev/mapper/pvcrypt
lvcreate -n root -l 100%FREE vg
mkfs.ext4 /dev/mapper/vg-root
\end{Verbatim}
\subsubsection{Deactivate journaling}
This step is also not necessary, but can increase file system performance a bit.
\begin{Verbatim}[commandchars=\\\{\}]
tune2fs -o journal_data_writeback /dev/mapper/vg-root
tune2fs -O ^has_journal /dev/mapper/vg-root
e2fsck -f /dev/mapper/vg-root
\end{Verbatim}
\subsubsection{Install Linux}
Now you can install Linux on your thumbdrive using the graphical installer provided with the live system. It is important that you select "something else" or "manual partitioning" when asked what to do by the installer. Then you have to select /dev/sdb1 as /boot partition and /mapper/vg-root as / partition (option "mount point"). And you have to select /dev/sdb ans destination drive for the boot loader. This is very important. If you don't select the proper drive you may break your system! When the installation process has finished, click on "continue testing". \textbf{Don't click on restart!} We still have some things to do in the live system to get the encrypted installation working.
\subsubsection{Configure installed system for lvm and encryption}
Open a terminal window. We will now configure our new installation using the commands blkid and chroot we got to now above. First, we need the UUID of the encrypted volume. Execute the following and note the UUID:
\begin{Verbatim}[commandchars=\\\{\}]
sudo blkid
\end{Verbatim}
Next, we use chroot to configure the installed Linux to use lvm and encryption. The commands are as follows:
\begin{Verbatim}[commandchars=\\\{\}]
sudo mkdir /mnt/cryptOS
sudo mount /dev/mapper/vg-root /mnt/cryptOS
sudo mount /dev/sdb1 /mnt/cryptOS/boot
chroot /mnt/cryptOS
mount -t proc proc /proc
mount -t sysfs sys /sys
apt-get update
apt-get update && apt-get install hashalot cryptsetup lvm2
\end{Verbatim}
Finally, we have to create a file so that our Linux finds and uses the encrypted volume. Open your favourite console editor, e.g. vim or nano, and create the file /etc/crypttab. Do not leave the chroot environment! Save the following line to that file, containing the UUID you noted above:
\begin{Verbatim}[commandchars=\\\{\}]
pvcrypt /dev/disk/by-uuid/uuid-from-above\t none \t luks
\end{Verbatim}
At last, we have to check if our fstab contains the proper information; still being in the chroot environment. Open /etc/fstab in your favourite editor. The file should look like that:
\begin{Verbatim}[commandchars=\\\{\}]
#/etc/fstab: static file system information. #
<file system> <mount point> <type> <options> <dump> <pass>
#proc /proc proc defaults 0 0
#/dev/mapper/vg-root UUID=c8d9b9a0-2198-4966-bc3a-39259df6a2c2 / ext4
noatime,errors=remount-ro 0 1
#/dev/sdb1 UUID=6af425ad-99b8-44a5-9ee1-0349141f9b1f /boot ext3 relatime 0 2
\end{Verbatim}
Of course, you will have different UUIDs. I recommend using noatime on the thumbdrive as mount option. This will again increase file system performance a bit. But still, the performance will not be as good as with a non-encrypted drive. You can now close the chroot session with:
\begin{Verbatim}[commandchars=\\\{\}]
exit
\end{Verbatim}
\subsubsection{Boot into the encrypted sytem}
Congratulations, you're done with the set up! You can now restart, select your thumbdrive as boot device and test the installtion. If the boot process seems to be stuck, check the ttys (ctrl+alt+F1 and so on). One will prompt for your luks passphrase. After the system is up, you can install your favourite software, e.g. gnunet, i2p, tor and other stuff you need and start using your fully encrypted sytem. Have fun!
\section{Useful Links}
\begin{itemize}
\item{\url{http://www.linuxcommand.org}}
\item{\url{http://www.commandlinefu.com}}
\item{\url{http://www.shell-fu.org}}
\item{\url{http://www.ss64.com}}
\end{itemize}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{}
\end{Verbatim}
\end{document}