The ed editor

It might happen as system administrator to have to work, especially to troubleshoot something, on an old Linux distro or Unix system where you don’t have none of the following known editors: vi, Emacs, Joe, ex, Sam or sed. In this case you will have at least ed, one of the oldest Unix inline text editor. So, you have guessed, this article will focus on this less famous, by today standard, editor. We will illustrate examples not just extracts from the man pages, which is not too much useful for novices.

The ed editor, like vi, works basically in two modes, edit and command. Once you start ed you will be presented with this screen without any prompt and in command mode:

Nothing is happening yet, you need to display the prompt by pressing P followed by Enter, as you can see an asterisk will be shown:

Afterwards you can type the letter i followed by newline (insert), to start typing something:

ed

Note that you must “announce” you want to finish editing with a . in a line on its own.
The number 13 is the number of characters written to the file.
You can save and exit ed with the command w (and the name of the file) and q, like in vi.

ed

To open an existing file we just runs ed followed by the filename.
Because is a line editor we won’t see the all content of the file by default, but just how many characters we have.
Let’s say we want to see its content. In this case we simply type p and press enter:

ed

We want to add the same word but in the different language, so we switch into “insert” mode
So as before we switch into command mode and with i we edit and add the line below, then we save without specifying
the filename as we already did by opening this file:

ed

Now we want to display the whole content of the file, we can do that with the command ,p
We can also this display the last line by simply using p

ed

As you can see the last line we typed is, by mistake, on top and not in the bottom.
There is also a small typo : a missing exclamation mark; let’s fix this typo first.
We just move to the desired line by typing its number followed by Enter
Then to amend the line we will use a sed like syntax.


OK we have selected the line and now we type command
s/$/!/p
to add that specific character at the end of the line ($ means EOL).

ed

Now it’s time to move our line to the bottom: you basically specify with the command m the desired line number. We must know the total number of lines as we cannot enter a not existing line.

ed

Now we want to add a new line or better to append it, we will use the command a for that.

ed

Now, let’s say we want to remove a line, we will use d for that.

ed

Finally, let’s say we want to append the result of a command, we can use the command !r for that:

ed

Note that the ! alone will just have the aim to run some system command without exiting ed, like in vi.ed

This is it: I hope you found interesting even if you won’t be using it often.

NixOS

This article will introduce NixOS, a not well known Linux distro, but which nevertheless holds some interesting and modern features in its core.

The most important are:

  • Atomic upgrades and installations
  • Possibility to rollback to a previous configuration
  • Configuration based on a functional language

Features

NixOS is a Linux distribution based on the functional package management system called Nix which aims to solve the common issues that makes traditional Linux systems hard to maintain for modern applications and requirements.

  1. Atomic upgrade and installations
    The main feature of the NixOS system is that packages are not installed in the common directories, according to the Filesystem Hierarchy Standard, but inside /nix/store/ in its own subfolder with a name starting with a hash code and followed by the software name. There, all relative dependences and configuration files are kept.
    By making un upgrade, be it a system or an application one, you never overwrite any pre-existing file or create problems in some other application; as a matter of fact you can have several versions of the same software, for instance different versions of HTTP Apache, along with their dependences, without having to implement a container solution – which by the way is supported in NixOS.

  2. Possibility to rollback to a previous configuration
    It is the most important characteristic of this system to make possible rolling back to a previous state of configuration without the need to pre-arrange any snapshot through software or hardware solution; of course the only preconditions are no corrupted boot loader and no hardware issues.

  3. Configuration based on a functional language
    True an idempotent, functional language we can configure the system by editing files with the nix extension and then apply the configuration. As already mentioned, once the configuration is applied it is possible to rollback to a previous one and not only, also to transfer the configuration to another NixOS system, apply it and obtain the same results. The goal is also to free the administrator from directly editing system configuration files.

Configuration examples

  1. Adding additional features
    Let’s say we want to install the KDE desktop manager, in this case we don’t need to use any command to download and install the relative package, like commonly in Linux, we can just edit the main configuration file configuration.nix in this way: NixOS reconfiguration


    This to enable the needed feature, then we have to run the:

    # nixos-rebuild switch

    Now, if this is the second main configuration change after installation, when we reboot next we will encounter the following screen if we choose not to load the default configutation:

    All configurations at boot
    Therefore if we wish we can boot the previous configuration.
    NOTE: we haven’t updated any kernel here and yet we can choose to revert to a previous configuration.

    1. Enabling a service
      Through the below configuration file we can enable or disable a service, for instance we wish to disable the Firewall (iptables):
      rebuilding configuration



      This is similar to Anybody who has some exposure to Puppet or similar system management tools might have noticed that in this case it is easier to apply the desired configuration.

  2. Installing a specific package
    We can of course install any desired package, from the Nix Packages collection (Nixpkgs) which is a set of over 40 000 packages.

    The command is

    # nix-env --install <package_name>;

    And no, we don’t have any rpm or deb package here.

    NixOS – installing a package

  3. Further configuration on packages
    For example, we want gcc to not be upgraded, in this case we need to issue the command:

    # nix-env --set-flag keep true gcc

  4. Rolling back to a previous configuraton
    We have found something we don’t like after applying a system configuration, no problem, we can rollback the change by typing the below (and pressing ENTER of course).:

    # nixos-rebuild switch --rollback

Conclusions

NixOS is a pure functional Linux distribution and different fromt the mainstream ones and it’s especially conceived to overcome the challenges posed by modern applications and systems complexities; certainly it deserves a try.

Security news

A potential bypass of the sudo runas security mechanisms has been discovered; When sudo is configured to allow a user to run commands as an arbitrary user via the ALL keyword in a Runas specification, it is possible to run commands as root by specifying the user ID -1 or 4294967295.

For more info, please consult the sudo website at https://www.sudo.ws/alerts/minus_1_uid.html

IT pill

Classic shell scripting (Bash, Korn Shell, etc.) is like démodé nowdays, but neverthless still used in Linux and other Unix like systems.
Therefore is still worth to mention about a static analysis tool written in Haskell https://www.shellcheck.net/
This tool will analyze your script and display each case of improper code or not following best practices.

IT pill

This time is going to be a Linux tip, that it could be useful.

It can happen you want to limit the amount of memory a Linux service can utilize, to be precise its maximum memory.
For instance, here we are assuming we want the nginx HTTP proxy and web server to not occupy more than 1024 MB or 1 GB. It is just an example, you can limit it to 2 GB if you want. Anyway how can we do that?
We can simply add a specific option in the service script and reload systemd along with nginx.

First of all we are going to show the default maximum memory nginx can use, of course if available in the system:

cat /sys/fs/cgroup/memory/system.slice/nginx.service/memory.limit_in_bytes

9223372036854771712

As you can see the value (in bytes) is basically like unlimited, so we need to reduce it drastically, to 1 GB as mentioned.

Now with the command below, we can create the backup file /lib/systemd/system/nginx.service.org, and insert the line option MemoryMax=1G just after [Service].

sed -i.org ‘/^\[Service\]/a MemoryMax=1G’ “/lib/systemd/system/nginx.service”

Then we need to reload the systemd daemon and restart the service

systemctl daemon-reload && systemctl restart nginx

And then we can verify the new value:

# cat /sys/fs/cgroup/memory/system.slice/nginx.service/memory.limit_in_bytes
1073741824

If we choose a memory value very low or if our process goes out of memory it will be killed by the kernel as in this log example:

— Unit nginx.service has begun starting up.
Aug 03 12:45:58 nabuchadnezzar8 kernel: nginx invoked oom-killer: gfp_mask=0x14000c0(GFP_KERNEL), nodemask=(null), order=0, oom_score_adj=0
Aug 03 12:45:58 nabuchadnezzar8 kernel: nginx cpuset=/ mems_allowed=0
Aug 03 12:45:58 nabuchadnezzar8 kernel: CPU: 2 PID: 9015 Comm: nginx Tainted: G OE 4.15.0-20-generic #21-Ubuntu
Aug 03 12:45:58 nabuchadnezzar8 kernel: Hardware name: Dell Inc. Latitude E7240/05PTPV, BIOS A21 05/08/2017
Aug 03 12:45:58 nabuchadnezzar8 kernel: Call Trace:
Aug 03 12:45:58 nabuchadnezzar8 kernel: dump_stack+0x63/0x8b
Aug 03 12:45:58 nabuchadnezzar8 kernel: dump_header+0x71/0x285
Aug 03 12:45:58 nabuchadnezzar8 kernel: oom_kill_process+0x220/0x440
Aug 03 12:45:58 nabuchadnezzar8 kernel: out_of_memory+0x2d1/0x4f0
Aug 03 12:45:58 nabuchadnezzar8 kernel: mem_cgroup_out_of_memory+0x4b/0x80
Aug 03 12:45:58 nabuchadnezzar8 kernel: mem_cgroup_oom_synchronize+0x2e8/0x320
Aug 03 12:45:58 nabuchadnezzar8 kernel: ? mem_cgroup_css_online+0x40/0x40
Aug 03 12:45:58 nabuchadnezzar8 kernel: pagefault_out_of_memory+0x36/0x7b
Aug 03 12:45:58 nabuchadnezzar8 kernel: mm_fault_error+0x90/0x180
Aug 03 12:45:58 nabuchadnezzar8 kernel: __do_page_fault+0x4a5/0x4d0
Aug 03 12:45:58 nabuchadnezzar8 kernel: do_page_fault+0x2e/0xe0
Aug 03 12:45:58 nabuchadnezzar8 kernel: ? page_fault+0x2f/0x50
Aug 03 12:45:58 nabuchadnezzar8 kernel: page_fault+0x45/0x50
Aug 03 12:45:58 nabuchadnezzar8 kernel: RIP: 0033:0x7f8ccf1a92c2
Aug 03 12:45:58 nabuchadnezzar8 kernel: RSP: 002b:00007ffffd66e530 EFLAGS: 00010206
Aug 03 12:45:58 nabuchadnezzar8 kernel: RAX: 00007f8ccc75ad18 RBX: 000055592b93ee50 RCX: 000000009fd0d5d6
Aug 03 12:45:58 nabuchadnezzar8 kernel: RDX: 0044000020800801 RSI: 00000000000000c5 RDI: 000000009fd0d5d6
Aug 03 12:45:58 nabuchadnezzar8 kernel: RBP: 0000000000000004 R08: 00007f8ccc75e610 R09: 000055592b93e2c8
Aug 03 12:45:58 nabuchadnezzar8 kernel: R10: 0000000000000018 R11: 0000000000000000 R12: 000055592b931858
Aug 03 12:45:58 nabuchadnezzar8 kernel: R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
Aug 03 12:45:58 nabuchadnezzar8 kernel: Task in /system.slice/nginx.service killed as a result of limit of /system.slice/nginx.service
Aug 03 12:45:58 nabuchadnezzar8 kernel: memory: usage 488kB, limit 488kB, failcnt 1824
Aug 03 12:45:58 nabuchadnezzar8 kernel: memory+swap: usage 0kB, limit 9007199254740988kB, failcnt 0
Aug 03 12:45:58 nabuchadnezzar8 kernel: kmem: usage 356kB, limit 9007199254740988kB, failcnt 0
Aug 03 12:45:58 nabuchadnezzar8 kernel: Memory cgroup stats for /system.slice/nginx.service: cache:0KB rss:132KB rss_huge:0KB shmem:0KB mapped_file:0KB dirty:0KB writeba
Aug 03 12:45:58 nabuchadnezzar8 kernel: [ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name
Aug 03 12:45:58 nabuchadnezzar8 kernel: [ 9015] 0 9015 20645 1461 192512 218 0 nginx
Aug 03 12:45:58 nabuchadnezzar8 kernel: Memory cgroup out of memory: Kill process 9015 (nginx) score 0 or sacrifice child
Aug 03 12:45:58 nabuchadnezzar8 kernel: Killed process 9015 (nginx) total-vm:82580kB, anon-rss:0kB, file-rss:5844kB, shmem-rss:0kB
lines 1257-1292/1306 99%

For a list of systemd resource control directives you can visit the relative official documentation.

IT pill

Is there any tool equivalent to the option “Save for Web” present in IrfanView or Photoshop in Gimp for Linux?

Yes.
In Debian based distros you need to install the package gimp-plugin-registry and In CentOS the package is called gimp-save-for-web. Of course you need to install Gimp first.