Arch Linux crypto installation

After live cd is loaded you need to setup network connection. For ethernet follow the next steps:

ip link set eth0 up #set interface up
ip addr add 10.69.0.100/24 broadcast 10.69.0.255 dev eth0 #set ip address
ip route add default via 10.69.0.1 #set gateway

Setting gateway in the example above may fail. If so, you can try next commands:

ip route add 10.69.0.1 dev eth0
ip route add default via 10.69.0.1 dev eth0

It is good to set right time now:

timedatectl set-ntp true

Next step is to prepare our filesystems. We will be using LVM on LUKS. For /boot i am using partition on my USB flash drive /dev/sdb1 formatted to ext2. Encrypted partition with LUKS is /dev/sda1, you may choose whatever you want.

So lets create LUKS encrypted partition:

cryptsetup -v --cipher aes-xts-plain64 --key-size 512 --hash sha512 luksFormat /dev/sda1

You need to enter secure password in prompt, so choose wisely. Now open our partition:

cryptsetup luksOpen /dev/sda1 lvm

We opened encrypted partition /dev/sda1 as lvm name and it is now available at /dev/mapper/lvm. To create LVM follow the next steps:

pvcreate /dev/mapper/lvm #create physical volume
vgcreate ArchVol /dev/mapper/lvm #create volume group named ArchVol
lvcreate -L 4G ArchVol -n swap #in ArchVol create 4GB partition named swap
lvcreate -l 100%FREE ArchVol -n root #in ArchVol create partition named root filled all remaining space

After this time to format our new logical partitions:

  • root available at /dev/mapper/ArchVol-root
  • swap available at /dev/mapper/ArchVol-swap
  • boot is my USB flash drive at /dev/sdb1
mkfs.ext4 /dev/mapper/ArchVol-root
mkswap /dev/mapper/ArchVol-swap
mkfs.ext2 /dev/sdb1

Mount aour filesystems and activate swap:

mount /dev/mapper/ArchVol-root /mnt
mkdir /mnt/boot
mount /dev/sdb1 /mnt/boot
swapon /dev/mapper/ArchVol-swap

Okay, now exact Arch installation:

pacstrap /mnt base

This bootstraps base system. To generate our /etc/fstab accordingly to mountpoints:

genfstab -U /mnt >> /mnt/etc/fstab

Now lets chroot to our system:

arch-chroot /mnt

Inside we need to set proper timezone and time:

ln -s /usr/share/zoneinfo/Europe/London /etc/localtime
hwclock --systohc

To setup locales just select and generate them:

nano /etc/locale.gen
locale-gen
/etc/locale.conf LANG=en_US.UTF-8

Fill the hostname:

nano /etc/hostname
nano /etc/hosts

Arch Linux has many available ways to configure network. So i am using systemd-networkd. For wired ethernet connection create the next config /etc/systemd/network/wired.network:

[Match]
Name = eth0

[Network]
Address = 10.69.0.100/24
Gateway = 10.69.0.1
DNS = 8.8.8.8

We are almost done. The few things which are left is generating initramfs image and installing bootloader. Our initramfs image must be configured with extra features such as lvm and encrypt. Edit configuration file /etc/mkinitcpio.conf:

HOOKS="...  encrypt  lvm2  ...  filesystems  ..."
MODULES="i915"  #if you use intel graphics

Generate initramfs: mkinitcpio -p linux

Set root password: passwd root

Install and configure bootloader:

pacman -S grub

Next you need to add your encrypted partition UUID to grub config. You can get it by running next command:

blkid /dev/sda1

# example output
/dev/sda1: UUID="cddd0a60-8281-4a09-8cce-1c5cb8849f62" TYPE="crypto_LUKS" PARTUUID="61979b00-998a-409d-aeb1-08e50f45023c"

Note the UUID part. Add it to /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="cryptdevice=UUID=cddd0a60-8281-4a09-8cce-1c5cb8849f62:lvm"

Install bootloader to MBR of our USB flash drive and generate config:

grub-install --target=i386-pc /dev/sdb1
grub-mkconfig -o /boot/grub/grub.cfg

At this point we have everything done. Time to unmount our partitions and reboot.

exit
umount /mnt/boot
umount /mnt
swapoff /dev/mapper/ArchVol-swap
reboot

Congratulations! Enjoy your fresh Arch Linux system on encrypted partition and boot USB flash drive as a key!

Arch Linux crypto installation

Network Manager restart after suspend/hibernate

Open a terminal and type the following:

sudo nano /etc/systemd/system/wifi-resume.service Now paste the script in there with a right click. Exit with CTRL + X and press Y to save. Now to activate it: sudo systemctl enable wifi-resume.service

Script:

#/etc/systemd/system/wifi-resume.service
#sudo systemctl enable wifi-resume.service
[Unit]
Description=Restart networkmanager at resume
After=suspend.target
After=hibernate.target
After=hybrid-sleep.target

[Service]
Type=oneshot
ExecStart=/bin/systemctl restart network-manager.service

[Install]
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target

Hope this helps. It works on my laptop.

Network Manager restart after suspend/hibernate

GPG Quick Start

A quick HOWTO for getting started with GnuPG.

Your Key

Private and public keys are at the heart of gpg’s encryption and decryption processes. The best first step is to create a key pair for yourself.

Generate a private key.

gpg --gen-key

You’ll have to answer a bunch of questions:

What kind and size of key you want; the defaults are probably good enough.

How long the key should be valid. You can safely choose a non-expiring key for your own use. If you plan to use a key for public signing, you might want to consider a yearly expiration.

Your real name and e-mail address; these are necessary for identifying your key in a larger set of keys.

A comment for your key, perhaps to distinquish a key used for special tasks like signing software releases. The comment can be empty.

A passphrase. Whatever you do, don’t forget it! Your key, and all your encrypted files, will be useless if you do.

Keyserver Registration

You might also want to register your key with public keyservers so that others can retrieve your key without having to contact you directly.

First up, you need to identify your key’s ID or fingerprint. The former is easier to use, the latter a bit more secure.

In the example below,

  • the ID is 8F54CA35; it’s on the line marked pub;
  • the fingerprint is 00E5 2D6D 91C0 20D0 F596 2CC5 1E36 9C62 8F54 CA35.
[~]$ gpg --fingerprint heinlein
pub   1024D/8F54CA35 2000-11-10
      Key fingerprint = 00E5 2D6D 91C0 20D0 F596  2CC5 1E36 9C62 8F54 CA35
uid                  Paul Heinlein <heinlein@madboa.com>
uid                  [jpeg image of size 3853]
uid                  Paul Heinlein (Galois, Inc.) <heinlein@galois.com>
sub   1024g/6088B91E 2000-11-10

To send them, you’ll need to locate a public key server. MIT’s is well known, but there are others.

# using ID (GnuPG versions 1 and 2)
gpg --keyserver pgp.mit.edu --send-keys '8F54CA35'
# using fingerprint (GnuPG version 2 and higher)
gpg --keyserver pgp.mit.edu \
    --send-keys '00E5 2D6D 91C0 20D0 F596  2CC5 1E36 9C62 8F54 CA35'

ASCII Version

You may also want to generate an ASCII version of your public key for distribution by e-mail or posting on a web site.

gpg --armor --output pubkey.txt --export 'Your Name'

Encrypting a file for personal use

Encrypting files for your personal use is quite easy.

Encrypt a file called foo.txt. The argument to the --recipient option should be the all or part of the name you used when generating your private key.

# the long version
gpg --encrypt --recipient 'Your Name' foo.txt

# using terse options
gpg -e -r Name foo.txt

The encrypted version of the file will by default be named foo.txt.gpg. You can modify that behavior using the --output (-o) option.

Decrypt the encrypted file. You’ll be asked to provide the passphrase you used when generating your private key. If you don’t use the --output option, the contents of the encrypted file will be sent to standard output.

gpg --output foo.txt --decrypt foo.txt.gpg

Encrypting a file for someone else

The really cool thing about GnuPG is that you can safely encrypt files for others using publicly available keys.

Import your friend’s key, which you might have received via e-mail or on a floppy. If the file is named key.asc, then just use the --import option to add it to your keyring:

gpg --import key.asc

That’s it! You can verify the import using the --list-keys option.

Alternatively, you might be able to find your friend’s key on a public keyserver. Here’s what a session looks like when someone searches for my key.

[~]$ gpg --keyserver pool.sks-keyservers.net --search-keys 'paul heinlein'
gpg: searching for "paul heinlein" from hkp server pool.sks-keyservers.net
(1)  Paul Heinlein <heinlein@madboa.com>
       1024 bit RSA key 8F54CA35, created: 2014-06-16 (revoked)
(2)  Paul Heinlein <heinlein@ohsu.edu>
     Paul Heinlein <heinlein@madboa.com>
     Paul Heinlein <heinlein@cse.ogi.edu>
     Paul Heinlein (Galois, Inc.) <heinlein@galois.com>
       1024 bit DSA key 8F54CA35, created: 2000-11-10
Keys 1-2 of 2 for "paul heinlein".  Enter number(s), N)ext, or Q)uit > Q
[~]$

A few notes on this:

  • The first result (marked “revoked”) was the result of a failed test I conducted. You can ignore it.
  • My key has several e-mail addresses attached to it. That’s perfectly normal.
  • In the example above, I chose the Q)uit option. Had I pressed 1 or 2, that key would have been downloaded and added to my local keyring.

Once you’ve got the other person’s public key, encrypt a file using it.

gpg --encrypt --recipient 'myfriend@his.isp.net' foo.txt

You’ll end up with a file called foo.txt.gpg that you can send as an e-mail attachment or make available for downloading via ftp or the web.

Decrypting a file from someone else

If someone sends you an encrypted file, the file has typically been encrypted using your public key. Decrypting it is no different than decrypting a file you’ve encrypted for your own use.

gpg --output foo.txt --decrypt foo.txt.gpg

Detached Signatures

GnuPG can come in handy when you want to be assured that the file you’ve just downloaded is the one its creator wants you to have. The OpenVPN developers, for instance, release GnuPG signatures for all their downloads.

To verify a file using its detached signature, you must first have imported the signer’s public key. Assume we’ve downloaded crucial.tar.gz and the developers have also released a signature file, crucial.tar.gz.asc. Once you’re confident that you have the developers’ public key in your local keyring, then the verification step is easy:

gpg --verify crucial.tar.gz.asc crucial.tar.gz

Creating a detached signature is similarly easy. The following example will create a signature for your-file.zip called your-file.zip.asc.

gpg --armor --detach-sign your-file.zip

People who have imported your public key into their keyrings can then verify that their version of your file is identical to theirs.

Basic Key Management

After a while, you will probably have several keys in your ring. It’s easy to list them all:

gpg --list-keys

Should you lose trust in or contact with a person with a key in your ring, you’ll want to delete it:

gpg --delete-key 'myfriend@his.isp.com'

For further reading

To move beyond these simple instructions, consult the GnuPG Documentation.

Source: https://www.madboa.com/geek/gpg-quickstart/

GPG Quick Start

How to backup LXC containers

Moving LXC containers between host systems

This is how I migrate LXC containers between systems. I’ve successfully moved ubuntu based 12.04 containers to a 14.04 host, and they work great.

  • Shutdown the container
    # lxc-stop -n $NAME
    
  • Archive container rootfs & config
    # cd /var/lib/lxc/$NAME/
    # tar --numeric-owner -czvf container_fs.tar.gz ./*
    

    The ‘–numeric-owner’ flag is very important! Without it, the container may not boot because the uid/gids get mangled in the extracted filesystem. When tar creates an archive, it preserves user / group ownership information. By default, when extracting, tar tries to resolve the archive user/group ownership names with the ids on the system running tar. This is intended to ensure that user ownership is resolved on the new system, in case the UID numeric values differ between systems.

    This is bad for an LXC filesystem because the numeric uid/gid ownership is intended to be preserved for the whole filesystem. If it gets resolved to a different value, bad things happen.

  • Copy the file to your new server
    # rsync -avh container_fs.tar.gz user@newserver:/var/lib/lxc/
    
  • Extract rootfs
    # mkdir /var/lib/lxc/$NAME/
    # cd /var/lib/lxc/$NAME/
    # tar --numeric-owner -xzvf container_fs.tar.gz ./*
    

If you’re using an overlay backed container, you’ll also need to migrate the container this new one is based off of. Lastly, you might see a few warnings about skipped socket files:

tar: /var/lib/lxc/$NAME/rootfs/dev/log: socket ignored

I’ve ignored this error, and haven’t had any issues with any of the containers I manage. If you have further issues, add your error messages to the original post and I’ll elaborate.

 

Source: http://stackoverflow.com/a/34194341

How to backup LXC containers

LXC share folder

Exposing a directory on the host machine to an LXC container

  1. Log into the container and create an empty directory, this will be the mount point
  2. Log out and stop the container.
  3. Open to your container’s config file
    • For regular LXC containers: /var/lib/lxc/mycontainer/config
    • For unprivileged LXC containers: $HOME/.local/share/lxc/mycontainer/config
  4. Add a new line above the lxc.mount directive, that follows the format below. Substitute proper paths as necessary:
    • lxc.mount.entry = /path/to/folder/on/host /path/to/mount/point none bind 0 0
    • Both of these paths are relative to the host machine.
    • Location of the root fs in the container can be found at:
      • For regular LXC containers: /var/lib/lxc/mycontainer/rootfs/
      • For unprivileged LXC containers: $HOME/.local/share/lxc/mycontainer/rootfs

Note: If the host’s user does not exist in the container, the container will still be mounted, but with nobody:nogroup as the owner. This may not be a problem unless you need to write to these files, in which case you’ll need to give everybody write permission to that folder. (i.e. chmod -R go+w /folder/to/share)

Example

I want to share /home/julianlam/foobar to my unprivileged container bazquux. In bazquux, I want this folder to be found at /mnt/baz.

In the container:

$ cd /mnt
$ sudo mkdir baz
$ logout

In the host, I will add the following line above lxc-mount in /home/julianlam/.local/share/lxc/bazquux/config:

lxc.mount.entry = /home/julianlam/foobar /home/julian/.local/share/lxc/bazquux/rootfs/mnt/baz none bind 0 0

Source: https://gist.github.com/julianlam/07abef272136ea14a627

LXC share folder

Bridge interface for lxc-containers

Script for creating bridge interface and applying  iptable routing rule:

#!/bin/bash

CMD_BRCTL=/sbin/brctl
CMD_IFCONFIG=/sbin/ifconfig
CMD_IPTABLES=/sbin/iptables
CMD_ROUTE=/sbin/route
NETWORK_BRIDGE_DEVICE_NAT=lxc-br0
HOST_NETDEVICE=eth0
PRIVATE_GW_NAT=192.168.0.1
PRIVATE_NETMASK=255.255.255.0

${CMD_BRCTL} addbr ${NETWORK_BRIDGE_DEVICE_NAT}
${CMD_BRCTL} setfd ${NETWORK_BRIDGE_DEVICE_NAT} 0
${CMD_IFCONFIG} ${NETWORK_BRIDGE_DEVICE_NAT} ${PRIVATE_GW_NAT} netmask ${PRIVATE_NETMASK} promisc up
${CMD_IPTABLES} -t nat -A POSTROUTING -o ${HOST_NETDEVICE} -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward

Edit the container config:

# Network configuration
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = lxc-br0
lxc.network.name = eth0
lxc.network.hwaddr = 00:FF:AA:00:00:01
lxc.network.ipv4 = 192.168.0.2/24
lxc.network.ipv4.gateway = 192.168.0.1

Edit the container interfaces:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.0.2
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 8.8.8.8

Enjoy the working network inside your lxc-container!

Bridge interface for lxc-containers