Saturday, December 28, 2013

The Usefullness of Linux

     One thing every Linux user should familiarize themselves with at some point in time is the terminal. Bash scripting is not too different from any other scripting language in my opinion. Once you get familiar with bash, all of a sudden you may indeed realize the power and flexibility behind Linux. Note: if you try this in Windows' command line, it is possible... but... good luck figuring it out - it's a mess!
     Here I wish to share the commands I find useful, and may periodically update this post as I discover new commands... Any combination of commands can be saved in a bash file (your_file.sh); Remember to include #! /bin/bash at the beginning of the file.

  • Install an app or library: sudo apt-get install app-name-here
  • Uninstall an app or library: sudo apt-get remove app-name-here
  • Update all apps and libraries: sudo apt-get update
  • Wireless Fix:
    rfkill unblock all
    rfkill list all
  • Operating System Version: cat /etc/lsb-release
  • VirtualBox Shared Folders: (Note: 'shared_folder_name' is created by clicking on 'Shared folders' in the VirtualBox Manager and then by clicking add folder icon)
    cd ~
    cd /media
    sudo mkdir
    folder_name_here
    sudo mount -t vboxsf shared_folder_name /media/folder_name_here
  • List items in folder: ls
  • Navigate directories:
    # navigate forward
    cd /folder/path
    # navigate backwords
    cd ../..
  • Kill process: sudo killall program_name_here
  • Current username: whoami
  • IP and Network information: ifconfig
  • Network Ports: iwconfig and ip addr show
  • Port-scan a computer/address (requires nmap): nmap -sS -O computer_ip_address
  • Edit file: gksudo gedit /path/to/file.txt OR sudo nano /path/to/file.txt
  • Get list of installed applications: dpkg --get-selections
  • Start app (server-side SSH - WARNING: See below!): DISPLAY=:0.0 application_name
  • Send virtual file input to app upon execution:
    app_name << EOF
    your file content here
    EOF
  • Evaluate a console command incompatible to bash syntax:
    # wmctrl is a non-native bash command and sublime is an active text editor
    # see programming bellow for more bash syntax
    function func{
         eval
    "wmctrl -a sublime"
    }
  • Gain root-level access: sudo su root
  • Get running time: uptime
  • Take a screenshot (prt sc - requires gnome-screenshot):
    gnome-screenshot -f /home/user/Desktop/pic.png
  • Copy file: cp /path/to/file.txt /path/to/newfile.txt
  • Move file: mv /path/to/file.txt /new/path/to/file.txt
  • Open port in firewall: sudo ufw allow 1500
  • Close port in firewall: sudo ufw deny 1500
  • Mount device to folder: sudo mount /path/to/source /path/to/folder
  • List connected USB devices: lsusb
  • Port Forwarding:
    echo 1 > /proc/sys/net/ipv4/ip_forward
    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    sudo iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
  • Run app or process in background: google-chrome "http://www.blogger.com/" &
  • Search path for a program file: which $program_name
  • Hibernation or Sleep: sudo pm-suspend
  • Manual for a command: man command
  • View and Manage Ethernet Media:
    man ethtool
    man mii-tool
  • System processes and monitoring top # htop
  • Send messages to users on remote server: w; write 'user' |pts\$n|;
  • Set static IP:
    nano /etc/network/interfaces
    # enter the following
    auto eth0 # set on startup
    iface eth0 inet static # dhcp for temporary IP
    address 10.0.0.25 # the static IP
    netmask 255.255.255.0
    network 10.0.0.0 # IP for 10.0.0.X networks
    broadcast 10.0.0.255
    gateway 10.0.0.1 # default IP of device - similar to the IP used to access router config
  • Set temporary IP (good for tapping into a routerless network; aka: a network where IPs are not assigned by any DNS or other address manager):
    ifconfig eth0 down
    ifconfig eth0 X.X.X.X up # replace 'X' with custom IP


Setup SSH (Linux to Linux):
  1. sudo apt-get install ssh  # on both systems
  2. sudo apt-get install openssh-client  # on the accessing system
  3. gksudo gedit /etc/ssh/sshd-config  # on the accessed system
  4. Type a port number of choice. For example: Port 1500 or Port 4321. Save.
  5. Make sure only one ssh process is running to avoid a mess. Check with ps -e | grep ssh If more than one are running, kill the process (sudo killall sshd) or if none are running, start one up with sudo /etc/init.d/ssh start. (For some installations, first type eval "sudo $(which sshd)" because sshd may be located in another directory such as /usr/sbin/sshd .)
  6. From the accessing computer's terminal, establish a connection with ssh -p port username@ipaddress for example: ssh -p 1500 admin@128.0.4.12
  7. For added security, install and run Firestarter on the accessed computer. Under 'Policy,' add a rule to allowed services. Include ssh port number and IP of accessing computer.
  8. To exit the session, press Ctrl+D.
  9. Copy a file from server to client:
    SCP -P 1500 admin@128.0.4.12:/path/to/file.txt /path/copied/to
  10. Copy a file from client to server:
    SCP -P 1500 /path/to/file.txt admin@128.0.4.12:/path/copied/to
  11. Copy folder & files from server:
    SCP -P 1500 -r admin@128.0.4.12:/server/folder /path/copied/to
  12. Copy folder & files to server:
    SCP -P 1500 -r /client/folder admin@128.0.4.12:/path/copied/to 

Running Applications Server-Side:
If an app were to run from the console, console access will not be returned until the app closes. So, if an app were run through an SSH terminal, either another SSH connection will need to be made to close the program or someone server-side would need to close the app. However, there is a work-around: start a new console server-side and from that console, start the application. Here is a demo (Mint to Kubuntu tested - requires xdotool):
DISPLAY=:0.0 konsole
DISPLAY=:0.0 xdotool type "google-chrome-stable"
# enter: KP_Enter; space: space; backspace: BackSpace
# Run application:
DISPLAY=:0.0 xdotool key KP_Enter


Bash User Interface:
Sometimes it can be convenient to add a user interface for a user's comfort and to reduce confusion. Zenity does just this. But of course, one can take Zenity another step forward with YAD - as the developers know it as: "Zenity on Steroids." All the features of Zenity and then some. Here is a demo (requires YAD):
frmdata=$(yad --title "Test Form" --form --field "Address" --field "name")
frmaddr=$(echo $frmdata | awk 'BEGIN {FS="|"} { print $1 }')
frmname=$(echo $frmdata | awk 'BEGIN {FS="|"} { print $2 }')
echo $frmaddr
echo $frmname



Basic Bash Programming:
# variables
str="hello world"
var=2

# write to a file

echo "hello" > file.txt

# read from a file
while IFS= read -r line;
do
     echo $line
done < file.txt

# create a folder
mkdir folder_name

# write to console
echo "hello world"

# read from console

read var


# conditioning
if ["true" = "true"]; then
     echo "this is true"
elif ["a" == "b"]; then
     echo "maybe not"
else
     echo "No!"
fi





# comment line

# foreach loop
for i in $( ls ); do
     echo item: $i
done

# for loop

for i in `seq 1 10`; do
     echo $i
done
for ((i=0; i<10; i++)); do
     echo yes
done

# while loop

CTR=0
while [  $CTR -lt 10 ]; do
    echo The counter is $CTR
    let CTR=CTR+1 
done

# until loop

CTR=0
while [  $CTR -lt 10 ]; do
     echo The counter is $CTR
     let CTR=CTR+1
done




# functions
function func {
     # echo params
     echo $1 $2 $3
}
func hello world !

# menu prompt

OPTIONS="Hello Qt"
select opt in $OPTIONS; do
     if ["$opt" = "Qt"];
     then
          echo done
          exit
     fi
done

# a static class could

# be seen as a bash file

# regex ( ~ )

[[ "Hello World" =~ "Hello"$ ]] && 
echo matched

# evaluation
eval "expression"

# delete file or directory
rm -r /path/to/file.txt





[My Simple Back Story]

     It has been close to two years now of getting use to the ways of Linux - a free and secure operating system. Depending on the title and version of Linux, one may face varying levels of difficulty. Linux does have a stereotype of being 'challenging' and 'only used by computer experts.' How do I know this? Because I was once one of those to hear them; even further, I saw one of the first Linux operating systems in action and immediately agreed about it's difficulty (even though I never even touched it once). 

     I gave it a try one day just to see - perhaps about four years ago this was. Kubuntu, my first choice, simply because it was blue. I realized that, although a little different from Windows and OSX, it actually had a very familiar feel to it. A 'start' button in the lower left, the time, network, and other tasks in the lower right, windows showed up in the task bar and have the usual minimize/maximize/close buttons at the top... Browsing the internet is just as easy. With Sublime Text, I have an advanced version of notepad. With OpenOffice or LibreOffice, I have free and compatible version of MSOffice. With Wine (a windows emulator) I can run windows-based applications such as windows notepad.
     Later, my roommate in college, introduced me to Linux Mint. Just like Kubuntu, Mint is a child branch of the Ubuntu based systems. Linux Mint is probably the easiest title out there to date. I've been using it for the past three days and all I can say is 'Wow, where have you been all my life?!'
     Here is a little something interesting I thought of: if you can master Linux Mint, you will be able to handle both Windows and Mac. Also, as a result of persuading some friends to convert from Mac to Linux, I learned of a project called Macbuntu which is OSX functionality with Linux as the underlying engine.
     All this is some pretty cool stuff. I was convinced the moment I learned of the simplicity. This operating system is for the naive all the way to the expert. And remember, you've already had a taste of Linux if you've ever surfed the web!