Archive for September 20th, 2018

Prepare (Burn) USB drive on Linux – Install Linux USB drive instead of CD / DVD

Thursday, September 20th, 2018

prepare-burn-usb-drive-on-linux-install-linux-usb-drive-instead-of-cd

If you have a new Laptop and you planned to install a Linux any distribution OS on it but the laptop is a new hardware model and doesn't have a CD / DVD drive as most of notebook nowadays you might be wondering how to setup USB Linux bootable image, well you can simply use your favorite distro provided ISO (Debian / Ubuntu / Fedora) whatever burn it to a USB flash drive and boot from USB the Linux installer program. of course assuming ou have it configured in your UEFI BIOS.
For the task you need to Burn the respecive distribution ISO to the USB using simple command line.

To do so simply download your Linux distribution latest ISO file and issue with root in physical terminal / gnome-terminal or konsole.
 

cat debian-linux-iso-name.iso > /dev/sdb; sync

 

WARNING DATA DANGEROUS!!!
If you happen to keep any data on the USB flash drive, above command would wipe it out from USB drive so be sure to prepare your existing data a backup somewhere before proceeding.

Also beware to not provide the wrong /dev/sdb device to the command as you might end up wiping out your Hard Drive ( SDD ) drives or other attached external hard drives under /dev/sdb.
Thus in advance be sure to check the exact USB /dev/ name using commands such as:
 

linux:~# fdisk -l

Disk /dev/sdb: 3.8 GiB, 4007657472 bytes, 7827456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x04030201

 

Device     Boot Start     End Sectors  Size Id Type
/dev/sdb1        1080 7827455 7826376  3.7G  c W95 FAT32 (LBA)

 

linux:~# dmesg|grep -i sdb


dmesg – output is pretty verbose so I'm skipping completely its output.

 

linux:~# lsusb 
Bus 002 Device 004: ID 04b3:3107 IBM Corp. ThinkPad 800dpi Optical Travel Mouse
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 04f2:b221 Chicony Electronics Co., Ltd integrated camera
Bus 001 Device 014: ID 0951:1607 Kingston Technology DataTraveler 100
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

 

linux:~# lsblk |grep -i /media
└─sdb1                   8:17   1   3.7G  0 part /media/hipod/KINGSTON


An alternative way is to use the good old dd tool.

 

linux:~# dd if=/your/path/debian-7.5.0-amd64-netinst.iso of=/dev/sdb bs=4M
sync


Its even possible to create the bootable Linux image to USB with a simple cp command instead of catting like:

 

linux:~# cp Fedora-Workstation-Live-x86_64-28-1.1.iso /dev/sdX
sync

 

Finally unmount the USB drive (if it was mounted during the command operations):

linux:~# umount /media/hipod/KINGSTON

For Windows users to burn the ISO files you can use anything starting from Nero / CDBurnerXP or if you prefer free software use
Win32DiskImager or use Rufus to create it.

Enjoy booting and installing your Linux via USB 🙂

Create user and password on Linux non interactive and add it to sudo a tiny Dev Ops script

Thursday, September 20th, 2018

Bash-Final-the-Bourne-again-shell-logo
A common task for SysAdmins who managed a multitude of servers remotely via Secure Shell was to add a user and assign password by using a script, this was sometimes necessery to set-up some system users and create access for university users on 10 / 20 testing Linux servers.

Nowadays this task of adding user to a list of remote servers and granting the new user superuser permissions through /etc/sudoers is practiced heavily by the so called Dev Ops (Just another Buziness Word for Senior System Admiistrators with good scripting skills and a little bit of development experience – same game different name.

The Dev Ops System Integration Engineers use this useful add non-interactive user via SSH in Cloud environments in order to prepare superuser (root permissioned through /etc/sudoers) user, that is later be used for lets say deployment on a few hundred of servers of lets say LAMP (Linux + Apache + MySQL + PHP) or LEMP (Linux NGINX MySQL PHP) or Software Load Balancer HAProxy  balacing for MySQL clusters / Nginx Application servers / JIRAs etc, through a Playbook script with some deployment automation tool such as Ansible.

Well enough talk here is the few lines of code which does create a user locally:
 

linux:~# apt-get install –yes sudo
linux:~# useradd devops –home /home/devops -s /bin/bash
linux:~# mkdir /home/devops
linux:~# chown -R devops:devops /home/devops
linux:~# echo 'username:testpass' | chpasswd


Though this lines could be invoked easily by passing it as arguments via ssh it is often unhandy to run them on remote host, because some of the remote hosts against executed, might have already the user existent with granted permissions for sudo

Thus a much better way to do things is use below script and first upload it to remote servers by running the scp command in a loop:

while read line; do
scp  root@$i:/root/
ssh "
create_user_noninteractive_and_add_to_sudoers.sh"
done < servers_list.txt


Where servers_list.txt contains a list of remote IPs:

#!/bin/bash
# Create new user/group and add nopasswd login to sudoers
# Author: Georgi Georgiev
# has to be run sa root – sudo devops
# hipo@www.pc-freak.net

 

u_id='devops';
g_id='devops';
pass='testpass';
sudoers_f='/etc/sudoers';

check_install_sudo ()  {
if [ $(dpkg –get-selections | cut -f1|grep -E ‘^sudo’) ]; then
apt-get install –yes sudo
else
        printf "Nothing to do sudo installed";
fi
}

check_install_user () {

if [ “$(sed -n “/$u_id/p” /etc/passwd|wc -l)” -eq 0 ]; then
apt-get install –yes sudo
apt-get install –yes sudo
useradd $u_id –home /home/$u_id
mkdir /home/$u_id
chown -R $u_id:$g_id /home/$u_id
echo "$u_id:$pass" | chpasswd
cp -rpf /etc/bash.bashrc /home/$u_id
if [ “$(sed -n “/$u_id/p” $sudoers_f|wc -l)” -eq “0” ]; then
echo "$u_id ALL=(ALL) NOPASSWD: ALL" >> $sudoers_f
else
        echo "$u_id existing. Exiting ..";
        exit 1;
fi

else
        echo "Will do nothing because $u_id exists";
fi

}

check_install_sudo;
check_install_user;


By the way this task was the simplest task given by a Company where I applied for a Dev Ops System Engineer, so I hope this will help someone else too.

P.S. If you prefer Shell scripts (even though much harder, time consuming etc.) as a mean of automation as an alternative to Ansible / Chef I suggest you check out and perhaps try to do the task with http://fuckingshellscripts.org 🙂