Saturday, March 5, 2016

Basic Linux Commands

This blog article contains list of Basic Linux Commands mostly for the beginners. With a little introduction, a terminal is the command interpreter also the Linux shell, it is where Linux commands are typed to be executed.

A Linux kernel is the heart of a Linux system while a Shell Script is a list of commands stored in a file that the shell executes non-interactively.

Their are different types of Linux command shells, for example;
  •   bourne shell ( $ character prompt)
  •   c shell (% character prompt)



TYPES OF COMMANDS
  1. Simple commands - Example: who
  2. Complex commands - Example: who am i
  3. Compound commands - Example: date ; who am i ;

When you open the terminal, the Home Directory is the directory in which you start out after you log in.
NOTE: In Linux systems, it is case sensitive, Home, hOme,HOME,homE are three different files or folders depending on the scenario.

LINUX COMMANDS
su (super user) -allows a user to automatically log in as root/super user.
sudo (super user do) -allows a permitted user to execute a command as the superuser or as root. According to the character prompt of your shell, it changes to # character prompt.

NOTE: It is not advisable to use su for day to day use since it can result to serious errors when accidentally used hence sudo is considered safer than su.

cal Displays calendar of present month and year.
cal 1990 Displays all the months of the year 1990.

date Prints current date and time.

cd Change directory command, it changes the working directory.
cd Desktop Changes the directory to Desktop.

find Allows one to search for a file in a given directory.
find file_name

who It displays who is logged on, in which tty and time the user logged on.

whoami Displays your username associated with the current user ID

uptime Displays the time the system has been up/on.

exit Exits the user from the terminal screen.

clear This command clears the visible area of the terminal screen.

du Shows how much space each file takes up.

top Provides a quick overview of the currently running processes, number of users logged in and the uptime.

kill is used to terminate processes that mostly cannot be terminated in a normal way. By executing it, you specify the respective process ID displayed from the top command.
kill 175 Kills process ID=175.

passwd Allows users to change their own password.

free Displays the amount of memory used and free system memory.

ifconfig Used to configure the kernel network interfaces.

ping Allows a user to verify that a particular IP address accepts requests, used to test connectivity and determine the response time
ping www.google.com

w Prints the current system users.

shutdown Shuts down the computer.
shutdown -r Reboots of the computer after shutdown.
shutdown -h
Halts the computer after shutdown
shutdown -c Cancels the shutdown process

tar puts file(s) into an archive
tar cvzf file_name.tar.gz /home/My_archives The command will create a compressed file for the directory /home/My_archives.
-f writes the output to a file and not the screen as is usually the case.
-c creates new tar archive
-r adds files to existing archive
-t outputs the contents of an archive
-u add files, but only if they are newer than the files already in the archive
-x extracts files from an archive
-j packs the resulting archive with gzip
-v lists the files processed

Listing Files
ls List files of the current directory you are in.
ls -a view hidden files
ls -F shows files and directories
ls -1 produces a listing
ls -l list all files with permissions
You can use all the ls arguments at a go i.e;
ls -aF1l
lsblk
Stands for list block devices, prints block devices by their assigned name and also Usb devices.

cat
Views the contents of a file.
cat -b file_name numbers the output minus the blank spaces.
cat -n file_name numbers the outputs.

wc Counts the words, lines, and characters in a file.
wc -lwc file_name
wc -l file_name counts lines
wc -w file_name counts words
wc -c file_name counts characters

Copying Files
cp command
cp file_name source destination
cp source destination Directory/
Copies a file to a given directory.

Renaming Files
mv command
mv file_name new file_name

Removing Files
rm command
rm file_name
rm -i file_name
Interactive mode for removing files.

Creating New Files
You can use different text editors to create new files like leafpad, gedit, nano etc depending with your Linux distribution. For this blog am using vim.
Ensure you have vim installed;
Type vim to open the terminal enabled editor
press 'a' to insert text and 'Esc' key to get out of insert mode
 n goes to the next file in the file search
:e filename switches to another file or to open a filename after you are in vim intro-page
:u undoes the last edit command
:q quits vim
:q! forces close of vim
:wq
saves and quit
:x saves the modified files and exits the editor
:w file_name save as (to new file_name)

Directories and their Manipulation
pwd Prints the working directory.
mkdir directory_name Creates a new directory
mkdir -p notes/linux/commands/permissions Creates parent directories notes/linux/commands if they don't exist.
cp -r source destination Copies a directory.
cp -r docs/book docs/school work/src /mnt/zip Copies multiple dir book,school, src to the zip directory.
mv
source destination Moves file and directories.
rmdir directory_name Removes an empty directory. 
rm -r directory_name Removes a directory will all its contents.

User_Groups and Permissions
sudo adduser username adds a user
sudo userdel username deletes a user

/etc/passwd Stores users information. 
/etc/group Stores all information about groups.

sudo passwd username Changes user password. 
sudo groupadd groupname Adds a group.
sudo groupdel groupname Deletes a group.
sudo adduser username groupname Adds a user to a group.
sudo deluser username groupname Removes a user from a group.
sudo chmod 777 foldername Changes the permissions of a folder.

File or folder permissions can be changed with three permissions(read,write and execute) each with a unique number identified with it.
4 -read permission (r)
2 -write permission (w)
1 -execute permission (x)


With basic math 4+2+1=7(All the 3 permissions equals full permission).

777, 775, 770, 700 etc
1st instance of the three figure - Owners permission to a file or folder.
2nd instance of the three figure - Group permission to file or folder.
3rd instance of the three figure - Everybody else/others permission to a file/folder.

775 owner and group full permission while others read and execute permissions only to a file or folder
777 owner, group and others full permissions of read, write and execute to a file or folder

sudo chmod 777 filename/foldername Gives owner, group and everybody else all full permissions of read, write and execute to a file or folder.

sudo chmod 775 filename/foldername Gives owner, group full permissions of read, write and execute while everybody else read and execute permission only to a file or folder.

sudo chmod 777 foldername -R (for recursive folders inside)

Ownership
sudo chwon -R username file_name/folder_name Changes the user ownership of a folder/file. sudo chgrp -R groupname file/folder Changes the group ownership of a folder/file.

Man Pages
Every version of Linux comes with an extensive collection of help pages called man pages (short for manual pages ). The man pages are the authoritative source about your Linux system.
They contain complete information about both the kernel and all the utilities. If you type;
man ls
It brings out a list of information about the ls commands and arguments that go along with it.

Happy learning.

[root@e13olf]# exit


Wednesday, March 2, 2016

How to Install Arch Linux

A step to step clean installation of arch Linux either in a new machine or virtual machine. You can get the ISO image from www.archlinux.org/download/



Boot the image through a boot medium(flash drive or compact disk).
You will received by an Arch Linux boot menu. Select the preferred version, note that the first in the list(x86_64) is the Arch Linux version of the 64 bit the the second selection(i686) is the 32 bit.After loading you'll be presented with a tty1 logged in screen.

Next is the managing/partitioning of the hard drive.
To find out how the hard drive is called, type;
lsblk

Partition the disk type;
cfdisk /dev/sda

sda being the drive to partition. You may receive a selection label type table, select the dos table. You will see the free space available to you.
Click on New then select Primary
Set it as Bootable, now the boot column will be marked with an *

To make changes to the drive select Write, press enter then write in the words 'yes' then enter for the changes to take effect.
Go back by selecting quit.

To see how the disk has been partitioned type
lsblk

You will see a sda1 partitoned under sda
Now to format the disk
mkfs.ext4 /dev/sda1

Next step is to mount the partition the local file system. You type;
mount /dev/sda1 /mnt

Next step is to install arch Linux into that partition. An internet connection is needed for this.
To connect to a WiFi internet access,if you do not have a working Ethernet connection to the wireless by type;
wifi-menu

Wireless devices will be listed, WiFi names are likely to start with 'wl' select a wireless network and type it's password if it requires any.

Test the connection
ping -c 3 www.google.com

Choose a mirror type;
nano /etc/pacman.d/mirrorlist

Through the list of find a mirror near you for better download speed,

Hit Alt+6 to copy a particular mirror, use the 'Page Up' key to got on top and then hit Ctrl+U to paste the line on top, hit Ctrl+x to exit and type Y to save changes

Next install the packages, run; 
pacstrap -i /mnt base base-devel

When prompt with Enter a selection (default=all): hit enter to install all the selection
To proceed with the installation hit enter. (This is the default as pressing Y)

The next step is set up the fstab file which in Linux is the text file that contains all the partitions and hard drives that you need to automatically mount on system boot. Type
genfstab -U /mnt > /mnt/etc/fstab

If you run the below command you will see the data has been written;
cat /mnt/etc/fstab

Change over from running on the Arch live CD into partition that was just installed into.
arch-chroot /mnt /bin/bash

Next install the boot loader;
pacman -S grub

Configure the downloaded grub
grub-install /dev/sda

NOTE: Install it to the drive but not the partition

Next is to configure the init file system;
mkinitcpio -p linux

Next configure grub again;
grub-mkconfig -o /boot/grub/grub.cfg

You have successfully installed a bootable Linux environment, next step is to install tools for a usable desktop environment;
pacman -S vim

Another important tool is the bash-completion to auto-complete commands;
pacman -S bash-completion

Install the gnome desktop environment or any of your favourable Linux desktop environment;
pacman -S gnome gnome-extra

Set up the locale, this specifies the regional and language stuff of the system;
nano /etc/locale.gen

Using the arrow keys select your locale configuration by uncommenting (removing the # character)
The default and most common locale is en_US.UTF-8 UTF-8 ( English Speakers)
Hit Ctrl+ O then enter
To leave the screen hit Ctrl+X
To update the system to use the locale, type;
locale-gen

To specify English or another language to be used type;
nano /etc/locale.conf

Then type; 
LANG=en_US.UTF-8

Hit Ctrl+ O then enter. To leave the screen hit Ctrl+X to exit nano

The next thing is to set the time zone (continent,Your Country's capital city)
ln -s /usr/share/zoneinfo/Africa/Nairobi /etc/localtime

For a person in New York
ln -s /usr/share/zoneinfo/America/New_York /etc/localtime

Set up the hardware clock on the machine to track the time correctly
hwclock --systohc --utc

Setting the custom hostname for your system, run;echo qwerty > /etc/hostname (replace qwerty with your hostname)

Setting up the root user password. Run;
passwd

enter the new UNIX password then retype it to verify it.

Next step is creating the users and replace 'qwerty' with your username run;
useradd -m -G wheel,storage,power -s /bin/bash qwerty

Give the password for the new user in my case was qwerty;
passwd qwerty

To allow this user to do administrative jobs as sudo, install sudo;
pacman -S sudo


Allow the users in wheel group to be able to perform admin tasks with sudo, to edit that run; 
EDITOR=nano visudo

Move down then uncomment this line in the open sudoers line 
%wheel ALL=(ALL) ALL

Then type :wq to write and quit

Unmount and reboot the machine
exit
unmount
u mount /mnt
shutdown -r now


Remove the existing media (flash drive or cd). You will brought to a login screen. Input your username and password, you need to enable the gdm (gnome desktop manager) service for gnome desktop and then restart the box:
sudo systemctl enable gdm

After login, it is advisable to keep your system up-to-date
sudo pacman -Syu

If their are error messages it's because the network devices have been set, type;
sudo systemctl enable NetworkManager
sudo systemctl start NetworkManager


Then try re-running the previous command to update your packages


There you have it, a working Arch Linux Distro.


[root@e13olf]# exit