diff --git a/assets/Unix_history-simple.svg b/assets/Unix_history-simple.svg new file mode 100644 index 0000000..09fed05 --- /dev/null +++ b/assets/Unix_history-simple.svg @@ -0,0 +1,551 @@ + + + + + image/svg+xml + + image/svg+xml + + + + + + + background + + + + Layer 1 + + + + + + OpenServer 6.x + + UnixWare 7.x (System V R5) + + HP-UX 11i+ + + + 1969 + + + 1971 to 1973 + + + 1974 to 1975 + + + 1978 + + + 1979 + + + 1980 + + + 1981 + + + 1982 + + + 1983 + + + 1984 + + + 1985 + + + 1986 + + + 1987 + + + 1988 + + + 1989 + + + 1990 + + + 1991 + + + 1992 + + + 1993 + + + 1994 + + + 1995 + + + 1996 + + + 1997 + + + 1998 + + + 1999 + + + 2000 + + + 2001 to 2004 + + + 2006 to 2007 + + + 2008 + + + 2005 + + 2009 + 2010 + 2011 + 2012 to 2015 + 2016 + 2017 + 2018 + 2019 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Open source + Mixed/shared source + Closed source + + + HP-UX 1.0 to 1.2 + + + + OpenSolaris & derivatives (illumos, etc.) + + + + System III + + + + System V R1 to R2 + + + + OpenServer 5.0.5 to 5.0.7 + + + + OpenServer 5.0 to 5.04 + + + + SCO UNIX 3.2.4 + + + + SCO Xenix V/386 + + + + SCO Xenix V/386 + + + + SCO Xenix V/286 + + + + SCO Xenix + + + + Xenix 3.0 + + + + Xenix 1.0 to 2.3 + + + + PWB/Unix + + + + AIX 1.0 + + + + AIX 3.0-7.2 + + + + + OpenBSD 2.3-6.8 + + + + + OpenBSD 1.0 to 2.2 + + + + SunOS 1.2 to 3.0 + + + + SunOS 1 to 1.1 + + + + Unix/32V + + + + Unix Version 1 to 4 + + + + Unix Version 5 to 6 + + + + Unix Version 7 + + + + Unnamed PDP-7 operating system + + + + BSD 1.0 to 2.0 + + + + BSD 3.0 to 4.1 + + + + BSD 4.2 + + + + Unix Version 8 + + + + Unix 9 and 10 (last versions from Bell Labs) + + + + NexTSTEP/ OPENSTEP 1.0 to 4.0 + + + + Mac OS X Server + + + + Mac OS X, OS X, macOS 10.0 to 10.15 (Darwin 1.2.1 to 19) + + + + Minix 1.x + + + + Minix 2.x + + Minix 3.1.0-3.4.0 + + + Linux 2.x + + + + Linux 0.99 to 1.2.x + + + + Linux 0.0.1 + + + + BSD 4.4-Lite & Lite Release 2 + + + + NetBSD 0.8 to 1.0 + + + + NetBSD 1.1 to 1.2 + + + + + NetBSD 1.3 + + + + + NetBSD 1.3-8.1 + + + + FreeBSD 1.0 to 2.2.x + + + + 386BSD + + + + BSD Net/2 + + + + Solaris 10 + + + + Solaris 11.0-11.4 + + + + + + System V R4 + + + + Solaris 2.1 to 9 + + + + BSD 4.3 + + + + SunOS 4 + + + + HP-UX 2.0 to 3.0 + + + + HP-UX 6 to 11 + + + + System V R3 + + + + UnixWare 1.x to 2.x (System V R4.2) + + + + + BSD 4.3 Tahoe + + + + BSD 4.3 Reno + + + + FreeBSD 3.0 to 3.2 + + + + FreeBSD 3.3-12.x + + + + Linux 3.x + + + + Linux 4.x + + + + OpenServer 10.x + + + + + DragonFly BSD 1.0 to 4.8 + + + + BSD Net/1 + + + Unix-like systems + + + + + + Linux 5.x + + diff --git a/assets/add_user_01.png b/assets/add_user_01.png new file mode 100644 index 0000000..1239738 Binary files /dev/null and b/assets/add_user_01.png differ diff --git a/assets/add_user_02.png b/assets/add_user_02.png new file mode 100644 index 0000000..ad11ea1 Binary files /dev/null and b/assets/add_user_02.png differ diff --git a/assets/add_user_03.png b/assets/add_user_03.png new file mode 100644 index 0000000..6704047 Binary files /dev/null and b/assets/add_user_03.png differ diff --git a/assets/add_user_04.png b/assets/add_user_04.png new file mode 100644 index 0000000..6d6a3d0 Binary files /dev/null and b/assets/add_user_04.png differ diff --git a/assets/add_user_05.png b/assets/add_user_05.png new file mode 100644 index 0000000..6f66a70 Binary files /dev/null and b/assets/add_user_05.png differ diff --git a/assets/add_user_06.png b/assets/add_user_06.png new file mode 100644 index 0000000..3021532 Binary files /dev/null and b/assets/add_user_06.png differ diff --git a/essential/introduction_to_administration.md b/essential/introduction_to_administration.md index 729a5c9..3d83e75 100644 --- a/essential/introduction_to_administration.md +++ b/essential/introduction_to_administration.md @@ -1,14 +1,523 @@ # Users +Linux is a [multi user](https://en.wikipedia.org/wiki/Multi-user_software) operating system. +For an end user's perspective this means we can have multiple accounts on one machine. +For example, one for each member of your household or company. +User can log in, out or switch accounts when seated at the computer or do multiple simultaneous logins via `ssh` or remote desktop software. + +From a more internal point of view, Linux uses different accounts to do perfrom different tasks on the system. +These accounts are often called [system accounts](https://unix.stackexchange.com/questions/80277/whats-the-difference-between-a-normal-user-and-a-system-user). + +We can list all know accounts with the following command. + +``` +waldek@debian:~$ cat /etc/passwd | cut -d ":" -f 1 +root +daemon +bin +sys +sync +games +man +lp +mail +news +uucp +proxy +www-data +backup +list +irc +gnats +nobody +_apt +systemd-network +systemd-resolve +messagebus +systemd-timesync +sshd +waldek +systemd-coredump +waldek@debian:~$ +``` + +Accounts are nothing more than a correctly defined line in a text file! +We can add and remove accounts as we please but luckily there are some tools to help us. + ## Adding and removing users +### GNOME + +In the gnome activities window we can search for *add user* and when we launch the program we'll see the dialog below. + +![GNOME add user](../assets/add_user_01.png) + +As adding a user is quite *invasive* to the system we need to prove we have the administration rights to do so. + +![GNOME add user](../assets/add_user_02.png) + +Now we can add a user. + +![GNOME add user](../assets/add_user_03.png) + +Done! + +![GNOME add user](../assets/add_user_04.png) + +Now we can verify the account's existence by logging in. + +![GNOME add user](../assets/add_user_05.png) + +Or via the command line. + +``` +waldek@metal:~$ su david +Password: +david@metal:/home/local/waldek$ tail -3 /etc/passwd +nvpd:x:125:137:NVIDIA Persistence Daemon,,,:/var/run/nvpd/:/usr/sbin/nologin +_flatpak:x:126:138:Flatpak system-wide installation helper,,,:/nonexistent:/usr/sbin/nologin +david:x:1005:1005:david,,,:/home/david:/bin/bash +david@metal:/home/local/waldek$ +``` + +We can also **remove** a user via the graphical interface. + +![GNOME add user](../assets/add_user_05.png) + +### command line + +This why you're here no? +To learn how to manager a Linux system like a pro. +So let's look into how to add and remove users via the command line. +As it's an *administrative* task we'll need to prove we have to right to do so. +This implies the use of `sudo`. + +``` +waldek@debian:~$ sudo add +addgroup addpart add-shell adduser +waldek@debian:~$ apropos adduser +adduser.conf (5) - configuration file for adduser(8) and addgroup(8) . +adduser (8) - add a user or group to the system +waldek@debian:~$ sudo adduser +[sudo] password for waldek: +adduser: Only one or two names allowed. +waldek@debian:~$ +``` + +The `adduser` program takes one or two arguments. +The behaviour changes quite dramatically depending on the number of arguments. +When you give *one* argument, the program will add a user to the system. +When using *two* arguments the program will add a user to a group. + +``` +waldek@debian:~$ sudo adduser ilies +Adding user `ilies' ... +Adding new group `ilies' (1001) ... +Adding new user `ilies' (1001) with group `ilies' ... +Creating home directory `/home/ilies' ... +Copying files from `/etc/skel' ... +New password: +Retype new password: +passwd: password updated successfully +Changing the user information for ilies +Enter the new value, or press ENTER for the default + Full Name []: + Room Number []: + Work Phone []: + Home Phone []: + Other []: +Is the information correct? [Y/n] +waldek@debian:~$ tail -1 /etc/passwd +ilies:x:1001:1001:,,,:/home/ilies:/bin/bash +waldek@debian:~$ su ilies +Password: +ilies@debian:/home/waldek$ +``` + +If we want to add `ilies` to the group of administrators we can use the same program but with **two** arguments, first the user, then the group in question. + +``` +ilies@debian:~$ sudo apt update +[sudo] password for ilies: +ilies is not in the sudoers file. This incident will be reported. +ilies@debian:~$ exit +exit +waldek@debian:~$ sudo adduser ilies sudo +Adding user `ilies' to group `sudo' ... +Adding user ilies to group sudo +Done. +waldek@debian:~$ su ilies +Password: +ilies@debian:/home/waldek$ sudo apt update +[sudo] password for ilies: +Get:1 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB] +Hit:2 http://deb.debian.org/debian bullseye InRelease +Get:3 http://deb.debian.org/debian bullseye-updates InRelease [39.4 kB] +Get:4 http://security.debian.org/debian-security bullseye-security/main Sources [120 kB] +Get:5 http://security.debian.org/debian-security bullseye-security/main amd64 Packages [146 kB] +Fetched 350 kB in 0s (902 kB/s) +Reading package lists... Done +Building dependency tree... Done +Reading state information... Done +1 package can be upgraded. Run 'apt list --upgradable' to see it. +ilies@debian:/home/waldek$ +``` + +`ilies` is now a user account with full administrator rights! +An important detail you should know is that a user's groups are only loaded upon login. +This has as a side effect that when you add a user to a group, they need to log out and back in order for the changes to take effect. +There are [tricks](https://superuser.com/questions/272061/reload-a-linux-users-group-assignments-without-logging-out) to bypass the behaviour though but I must say I always log out and back in myself. + +Removing a user is done with the `deluser` program. + +``` +waldek@debian:~$ sudo deluser ilies +Removing user `ilies' ... +Warning: group `ilies' has no more members. +Done. +waldek@debian:~$ tail -1 /etc/passwd +systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin +waldek@debian:~$ grep ilies /etc/passwd +waldek@debian:~$ su ilies +su: user ilies does not exist or the user entry does not contain all the required fields +waldek@debian:~$ +``` + +It's worth pointing out that both `adduser` and `deluser` are scripts. +We can verify this as follows. + +``` +waldek@debian:~$ file $(sudo which adduser) +/usr/sbin/adduser: Perl script text executable +waldek@debian:~$ file $(sudo which deluser) +/usr/sbin/deluser: Perl script text executable +waldek@debian:~$ head $(sudo which deluser) +#!/usr/bin/perl + +# deluser -- a utility to remove users from the system +# delgroup -- a utilty to remove groups from the system +my $version = "3.118"; + +# Copyright (C) 2000 Roland Bauerschmidt +# Based on 'adduser' as pattern by +# Guy Maor +# Ted Hajek +waldek@debian:~$ +``` + +The *real* heavy lifting is done by two *other* programs called `useradd` and `userdel`. +When writing your own scripts to add and remove users you should use these programs an not the `perl` scripts! + +``` +waldek@debian:~$ file $(sudo which useradd) +/usr/sbin/useradd: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=cddbc9a8bae8b6f626a47a2b5ed1ea76081eae6d, for GNU/Linux 3.2.0, stripped +waldek@debian:~$ file $(sudo which userdel) +/usr/sbin/userdel: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=cdf46de13b11b883951743d5652347a141bafba7, for GNU/Linux 3.2.0, stripped +``` + ## Switching user +There are **two** different ways of *switching* user accounts or performing tasks *as* someone else: `su` and `sudo`. +`su` means **switch user**, while `sudo` means **switch user and do**. +`sudo` is a bit more modern but both have their [utility](https://www.redhat.com/sysadmin/difference-between-sudo-su). +So, some examples. + +``` +waldek@debian:~$ whoami +waldek +waldek@debian:~$ sudo whoami +root +waldek@debian:~$ sudo -u steve whoami +steve +waldek@debian:~$ sudo -u ilies bash +ilies@debian:/home/waldek$ whoami +ilies +ilies@debian:/home/waldek$ cd +ilies@debian:~$ pwd +/home/ilies +ilies@debian:~$ +``` + +Above we can see that `sudo` is used to perform a task *as* an other user. +We can use this to launch single commands but also to launch a `bash` shell. +A more *classic* way to switch identity is to use `su`. +Below you can see the difference between a simple `su` call and with the `-` argument. +The difference is subtle but quite important. +Have a read of the `man su` for more information. + +``` +waldek@debian:~$ su ilies +Password: +ilies@debian:/home/waldek$ whoami +ilies +ilies@debian:/home/waldek$ pwd +/home/waldek +ilies@debian:/home/waldek$ exit +exit +waldek@debian:~$ su - ilies +Password: +ilies@debian:~$ pwd +/home/ilies +ilies@debian:~$ whoami +ilies +ilies@debian:~$ +``` + +For the lazy... + +``` +-, -l, --login + Start the shell as a login shell with an environment similar to a real + login: + + o clears all the environment variables except TERM and variables + specified by --whitelist-environment + + o initializes the environment variables HOME, SHELL, USER, LOG‐ + NAME, and PATH + + o changes to the target user's home directory + + o sets argv[0] of the shell to '-' in order to make the shell a + login shell +``` + +## Groups + +Groups are a second fundamental concept of any Linux distribution. +Every user has a **primary** group, most of the time the same name as their user account, and none/one/more **secondary** groups. +These groups can be made by us or by the distribution. +For example, `sudo` is the group that all administrators need to be a member of. +A few examples above I added `ilies` to the `sudo` group. +Once this was done, and `ilies` logged back in, he could perform administrative tasks. + +`adduser` is used with **two** arguments to add a `user` to a `group`. +To create or delete groups we use `addgroup` and `delgroup`. +An example: + +``` +waldek@debian:~$ groups +waldek cdrom floppy sudo audio dip video plugdev netdev +waldek@debian:~$ su - ilies +Password: +ilies@debian:~$ groups +ilies +ilies@debian:~$ exit +logout +waldek@debian:~$ sudo addgroup friends +[sudo] password for waldek: +Adding group `friends' (GID 1003) ... +Done. +waldek@debian:~$ sudo adduser ilies friends +Adding user `ilies' to group `friends' ... +Adding user ilies to group friends +Done. +waldek@debian:~$ su - ilies +Password: +ilies@debian:~$ groups +ilies friends +ilies@debian:~$ +``` + +Just like a user account is *just* a line in a text file, the group definitions are the same. + +``` +waldek@debian:~$ tail -5 /etc/group +waldek:x:1000: +systemd-coredump:x:999: +steve:x:1001: +ilies:x:1002: +friends:x:1003:ilies +waldek@debian:~$ sudo adduser steve friends +Adding user `steve' to group `friends' ... +Adding user steve to group friends +Done. +waldek@debian:~$ tail -5 /etc/group +waldek:x:1000: +systemd-coredump:x:999: +steve:x:1001: +ilies:x:1002: +friends:x:1003:ilies,steve +waldek@debian:~$ +``` + +And we can delete the group as follows. + +``` +waldek@debian:~$ sudo delgroup friends +Removing group `friends' ... +Done. +waldek@debian:~$ tail -5 /etc/group +ssh:x:111: +waldek:x:1000: +systemd-coredump:x:999: +steve:x:1001: +ilies:x:1002: +waldek@debian:~$ su - ilies +Password: +ilies@debian:~$ groups +ilies +ilies@debian:~$ +``` + # Permissions +Unix like systems differ greatly from a Windows system because they are not just *multi tasking* but also *multi user*. +Users and groups imply [permissions](https://en.wikipedia.org/wiki/File-system_permissions#Notation_of_traditional_Unix_permissions), meaning **who** is allowed to do **what** on a system. +Permissions are tied to a **file system**, either real or virtual, but can't exist outside of this context. +When we list the files in a directory, we can add the `-l` flag to see more information. +In the example below I create a file called `helloworld` and show it's details with `ls -l`. + +``` +waldek@debian:~$ touch helloworld +waldek@debian:~$ ls -l +total 0 +-rw-r--r-- 1 waldek waldek 0 May 16 10:32 helloworld +waldek@debian:~$ +``` + +Next up I create a directory to store my pictures in. + +``` +waldek@debian:~$ mkdir photos +waldek@debian:~$ ls -l +total 4 +-rw-r--r-- 1 waldek waldek 0 May 16 10:32 helloworld +drwxr-xr-x 2 waldek waldek 4096 May 16 10:33 photos +waldek@debian:~$ +``` + +The file and directory above points us to suite a bit of information. +Let's break it down. +The table below slices each individual element apart. +The first column indicates the *type* of file. +This can be a directory, link, block device, character device, among others. +Have a look [here](https://en.wikipedia.org/wiki/Unix_file_types) for an elaborate explanation of all possible types in Linux. + +| type | owner | group | others | nr links | owner | group | size | mod date | name | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| - | rw- | r-- | r-- | 1 | waldek | waldek | 0 | May 16 10:32 | helloworld | +| d | rwx | r-x | r-x | 2 | waldek | waldek | 4096 | May 16 10:33 | photos | + ## Octal number system -## Changing permissions +The octal number system is the most used one to represent permissions in Linux. +At first it might me a bit confusing but it's not *that* complicated. +You just have to **know** that: + +* **read** is worth **4** +* **write** is worth **2** +* **execute** is worth **1** + +The **sum** of these permissions is the octal representation. +Below is a little grid to help you understand. + +| sum | 4 | 2 | 1 | +| --- | --- | --- | ---| +| 0 | - | - | - | +| 1 | - | - | x | +| 2 | - | w | - | +| 3 | - | w | x | +| 4 | r | - | - | +| 5 | r | - | x | +| 6 | r | w | - | +| 7 | r | w | x | + +Each file in Linux has **three** different permissions. + +1. for the **owner** of the file +1. for the **group** of the file +1. and for **everyone** who is not the *owner* nor in the *group* + +In the example below I use `chmod` to change the *mode* or permissions of a file. +I *highly* encourage you to read the `man chmod`. + +``` +waldek@debian:~$ chmod 777 permission_test +waldek@debian:~$ ls -l permission_test +-rwxrwxrwx 1 waldek waldek 0 May 16 10:54 permission_test +waldek@debian:~$ chmod 000 permission_test +waldek@debian:~$ ls -l permission_test +---------- 1 waldek waldek 0 May 13 16:45 permission_test +waldek@debian:~$ chmod 123 permission_test +waldek@debian:~$ ls -l permission_test +---x-w--wx 1 waldek waldek 0 May 13 16:45 permission_test +waldek@debian:~$ chmod 640 permission_test +waldek@debian:~$ ls -l permission_test +-rw-r----- 1 waldek waldek 0 May 16 10:54 permission_test +waldek@debian:~$ +``` + +When searching or files we discovered that we don't have the permission to read certain files, and now we know *why* that is! +A small specific example. + +``` +waldek@debian:~$ file /etc/shadow +/etc/shadow: regular file, no read permission +waldek@debian:~$ cat /etc/shadow +cat: /etc/shadow: Permission denied +waldek@debian:~$ ls -l /etc/shadow +-rw-r----- 1 root shadow 1001 May 13 13:49 /etc/shadow +waldek@debian:~$ whatis shadow +shadow (5) - shadowed password file +waldek@debian:~$ +``` + +As administrator we do have the right to read this file. +So we can add `sudo` before the command to peek into the file. + +``` +waldek@debian:~$ sudo tail -2 /etc/shadow +steve:$y$j9T$UHjx5oOFKyFR6VMY6y1IB/$EnLPe41c46wW9xN.6oKNUhHw2XH4xchlxb8w7k2T853:19125:0:99999:7::: +ilies:$y$j9T$5ykW1sIfDSOFUbaFEcS6i0$/r73taI9vEcXNuMGw.3evLqhWDuPTVaC1dK6wjmpTb2:19125:0:99999:7::: +waldek@debian:~$ +``` + +What on earth are this lines? +Well, those are the passwords of your user accounts which are stored in the [shadow](https://en.wikipedia.org/wiki/Passwd#Shadow_file) file format. +Historically they where [plain text](https://en.wikipedia.org/wiki/Plaintext) but now they are [hashed](https://en.wikipedia.org/wiki/Hash_function). +Don't forget you can read up about them in your terminal with `man shadow`, `man passwd`. + +``` +waldek@debian:~$ whatis passwd +passwd (5) - the password file +passwd (1) - change user password +passwd (1ssl) - compute password hashes +waldek@debian:~$ man 5 passwd +``` + +Now is a good time to point out the manual **sections**. +The manual for the passwd file and program are not the same. +We can read the file manual with `man 5 passwd` and the program manual with `man 1 passwd`. +More information of the sections can be found via `man man` where the table below comes from. + +| section | description | +| --- | --- | +|1 | Executable programs or shell commands | +|2 | System calls (functions provided by the kernel) | +|3 | Library calls (functions within program libraries) | +|4 | Special files (usually found in /dev) | +|5 | File formats and conventions, e.g. /etc/passwd | +|6 | Games | +|7 | Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) | +|8 | System administration commands (usually only for root) | +|9 | Kernel routines [Non standard] | + +## Essential programs + +Below is a small list of often used programs when dealing with file permissions. +We'll go a into detail on some of them right after. + +| name | description | +| --- | --- | +| `id` | print real and effective user and group IDs | +| `chmod` | change file mode bits | +| `chown` | change ownership of a file | +| `umask` | set file mode creation mask | +| `chgrp` | change group ownership | +| `passwd` | change user password | +| `su` | run a command with substitute user and group ID | +| `sudo` | execute a command as another user | ## Changing ownership @@ -16,6 +525,15 @@ ## History +[Here](https://www.jeffgeerling.com/blog/brief-history-ssh-and-remote-access) you have a nice article on the history of remote shells. +The two main ones are: + +* [rsh](https://en.wikipedia.org/wiki/Remote_Shell) +* [telnet](https://en.wikipedia.org/wiki/Telnet) + +But today we **don't** use those anymore! +We use [ssh](https://en.wikipedia.org/wiki/Secure_Shell) now! + ## SSH and SFTP # Bandit diff --git a/essential/introduction_to_the_commandline.md b/essential/introduction_to_the_commandline.md index 388c48d..2f3c628 100644 --- a/essential/introduction_to_the_commandline.md +++ b/essential/introduction_to_the_commandline.md @@ -644,6 +644,7 @@ The output above teaches us that the options `-la` change the output so it becom To *not ignore entries starting with .* we can use two different flags, `-a` or `--all`. The former is called short arguments and the latter long ones. The long ones are more *verbose* so it's often easier to understand what a line *does* just by reading it. +You can read up on the [history of arguments](https://blog.liw.fi/posts/2022/05/07/unix-cli/). Note that `ls` only has one way of showing the help by adding `--help` whereas the help for `htop` can be shown with both `-h` or `--help`. This inconsistency is sadly a byproduct of the decentralized nature of Linux. To add to this mess some programs use *single dash long form* (`-help`) but those programs are rather rare. @@ -786,7 +787,7 @@ rbash (1) - restricted bash, see bash(1) waldek@hellodebian:~$ ``` -# Exercise +## Exercise **Read** some manual pages on the commands we've seen until now. **Apply** some options you read about in the manual to experiment with said programs. @@ -981,7 +982,14 @@ assets waldek@hellodebian:~/Documents$ ``` -### `nano` +## Links + +TODO + + +# Editors + +## `nano` The *easiest* to use command line editor that comes installed with most Linux distributions is called `nano`. Below is a screenshot of an empty text file, opened up in `nano`. @@ -1002,13 +1010,13 @@ Here you can just hit **ENTER**. ![nano save](../assets/nano_03.png) -# Exercise +### Exercise Create some text files with `nano`. Move them around with `mv`, delete them with `rm` and copy them with `cp`. Can you edit the `/etc/passwd` file? -### `vi` and `vim` +## `vi` and `vim` On of the most [popular](https://www.journaldev.com/41292/top-best-text-editors-linux) text editors is by far `vim`. It is an *improvement* on an older editor called `vi`, vim(proved). @@ -1019,7 +1027,98 @@ The paragraph below is taken from the [wikipedia](https://en.wikipedia.org/wiki/ > vi is a modal editor: it operates in either insert mode (where typed text becomes part of the document) or command mode (where keystrokes are interpreted as commands that control the edit session). For example, typing i while in command mode switches the editor to insert mode, but typing i again at this point places an "i" character in the document. From insert mode, pressing ESC switches the editor back to command mode. A perceived advantage of vi's separation of text entry and command modes is that both text editing and command operations can be performed without requiring the removal of the user's hands from the home row. As non-modal editors usually have to reserve all keys with letters and symbols for the printing of characters, any special commands for actions other than adding text to the buffer must be assigned to keys that do not produce characters, such as function keys, or combinations of modifier keys such as Ctrl, and Alt with regular keys. Vi has the property that most ordinary keys are connected to some kind of command for positioning, altering text, searching and so forth, either singly or in key combinations. Many commands can be touch typed without the use of Ctrl or Alt. Other types of editors generally require the user to move their hands from the home row when touch typing: -# Exercise +### Exercise + +In order to use `vim` we need to install it. +Most Linux distributions come with some form of `vi` or `vim` installed. +On a modern Debian system this is: + +``` +waldek@debian:~$ vi +vi view vim.tiny +waldek@debian:~$ vi +``` + +`vim.tiny` is a version of `vim` compiled with only the basic features. +We can see what this means by adding the `--version` argument. + +``` +waldek@debian:~$ vim.tiny --version +VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Oct 01 2021 01:51:08) +Included patches: 1-2434 +Extra patches: 8.2.3402, 8.2.3403, 8.2.3409, 8.2.3428 +Modified by team+vim@tracker.debian.org +Compiled by team+vim@tracker.debian.org +Small version without GUI. Features included (+) or not (-): ++acl -farsi +mouse_sgr -tag_old_static +-arabic -file_in_path -mouse_sysmouse -tag_any_white ++autocmd -find_in_path -mouse_urxvt -tcl +-autochdir -float +mouse_xterm -termguicolors +-autoservername -folding +multi_byte -terminal +-balloon_eval -footer -multi_lang +terminfo +-balloon_eval_term +fork() -mzscheme +termresponse +-browse -gettext -netbeans_intg -textobjects ++builtin_terms -hangul_input +num64 -textprop +-byte_offset +iconv -packages -timers +-channel +insert_expand -path_extra -title +-cindent -ipv6 -perl -toolbar +-clientserver -job -persistent_undo +user_commands +-clipboard +jumplist -popupwin -vartabs ++cmdline_compl -keymap -printer +vertsplit ++cmdline_hist -lambda -profile +virtualedit +-cmdline_info -langmap -python +visual ++comments -libcall -python3 +visualextra +-conceal -linebreak -quickfix -viminfo +-cryptv -lispindent -reltime +vreplace +-cscope +listcmds -rightleft +wildignore ++cursorbind +localmap -ruby -wildmenu +-cursorshape -lua +scrollbind +windows +-dialog -menu -signs +writebackup +-diff -mksession -smartindent -X11 +-digraphs +modify_fname -sound +xfontset +-dnd +mouse -spell -xim +-ebcdic -mouseshape -startuptime -xpm +-emacs_tags -mouse_dec -statusline -xsmp +-eval -mouse_gpm -sun_workshop -xterm_clipboard ++ex_extra -mouse_jsbterm -syntax -xterm_save +-extra_search -mouse_netterm +tag_binary + system vimrc file: "$VIM/vimrc" + user vimrc file: "$HOME/.vimrc" + 2nd user vimrc file: "~/.vim/vimrc" + user exrc file: "$HOME/.exrc" + defaults file: "$VIMRUNTIME/defaults.vim" + fall-back for $VIM: "/usr/share/vim" +Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -Wdate-time -g -O2 -ffile-prefix-map=/build/vim-DtwDbo/vim-8.2.2434=. -fstack-protector-strong -Wformat -Werror=format-security -DTINY_VIMRC -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 +Linking: gcc -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE -lm -ltinfo -lselinux -lacl -lattr -ldl +waldek@debian:~$ +``` + +We can install a more complete version which is called `vim-nox`. + +``` +waldek@debian:~$ apt search vim-nox +Sorting... Done +Full Text Search... Done +vim-nox/stable 2:8.2.2434-3+deb11u1 amd64 + Vi IMproved - enhanced vi editor - with scripting languages support + +vim-tiny/stable,now 2:8.2.2434-3+deb11u1 amd64 [installed] + Vi IMproved - enhanced vi editor - compact version + +waldek@debian:~$ +``` + +I highly advise you to install the more complete version. +Once done, have a look at the *new* programs installed with tab complete. + +``` +waldek@debian:~$ vim +vim vimdiff vim.nox vim.tiny vimtutor +waldek@debian:~$ which vim +/usr/bin/vim +waldek@debian:~$ ls -l /usr/bin/vim +lrwxrwxrwx 1 root root 21 May 13 11:20 /usr/bin/vim -> /etc/alternatives/vim +``` The only way to learn `vim` is to *use* it. Luckily `vim-nox` comes with a dedicated *program* to learn it called `vimtutor`. @@ -1028,7 +1127,7 @@ Enjoy! Once you have completed the `vimtutor` I urge you to have a look at one of the *many* cheat sheets [online](https://vim.rtorr.com/). -# Opening files +# Opening or peaking into files We can use `vim` or `nano` to open text files and write to them, but there are a few programs worth knowing that can *just* show the content of a text file. You'll use these all the time!