Wednesday, 31 October 2018

Introduction to Linux

Linux

Open source operating system
Windows and OS X are mainly used on Desktops
Used not only on personal computers, but web servers, electronics
Android is a variation of Linux (they removed some stuff and customized it for mobile)

      Tired of privacy issues in Windows/OS X?
      Learn to manage web servers or build electronics/robots?

Different “versions” of Linux, one of the most popular is Ubuntu

1.    Download Ubuntu
2.    Install VMware Player for Windows 64-bit
a.    Remove all USB drives from computer
3.    If you are installing on other computer, burn ISO to DVD
4.    Create a new virtual machine
a.    Select ISO
                    i.     Ubuntu
                   ii.     bucky
                 iii.     bacon123
b.    Store virtual disk into a single file
c.     Customize Hardware > Memory: 2-4 GB (2048 MB)
                                           i.        too much will take away from your host OS
5.    Finish & Play

Ctrl+Altto switch to main OS



Basic architecture of Linux
simpleDiagram.png

Bottom layer is hardware
      1’s and 0’s

Next layer is the kernel
      Core operating system
      Software that tells hardware directly what to do

Top layer are Applications or user processes
      Programs that you use and make
      Browser, games, Text editor…
      When making programs, you don’t need to tell CPU how to work



Kernel decides which app is allowed to use the CPU at any time
Manages memory for each app
Computer with a single core CPU, it can appear several apps are running at the same time
What’s actually happening is each app uses CPU for small amount of time, process pauses, and then another one does
CPU can switch between processes so fast they appear to be running simultaneously by humans
Kernel manages all of these operations (also for multi-core CPU’s)

Kernel also gives each app it’s own chunk of memory
That way, programs don’t mess with each other when running



Basic Commands

Shell window (or Terminal) is a program where you can type commands

Search computer > Terminal > Open
Right Click > Lock to Launcher

Right click > Profile > Profile Preferences
Cursor shape > I-beam

 

You can also right click and unlock things you don’t want


name@host:path$


      Make a file called Story on the Desktop
      Add some text in it

Display current working directory

   pwd


Display the contents of a file

   cat Desktop/Story

 

List all contents in directory (and detailed list)

-a shows dot files (hidden files) usually used as configuration files

   ls

  ls -l

  ls -a

   ls -la




Navigating

Move directories (parent, child, home)

  cd ..

  cd Other

   cd


Create and delete directories

  mkdir Other

  mkdir Tuna

   rmdir Tuna




Working with Files

Create a file. If it already exists, doesn’t change it

   touch Story

   touch Bacon


Delete (remove) a file

   rm Bacon


Copy a file

   cp Story Story2


Move a file

   mv Story2 Other/Story2


You can also rename a file using

   mv Story Tuna


Display content

   echo hey now




Other Commands

Search - print lines from Story that include the word bacon

   grep bacon Story


See the difference between two files
   diff file1 file2

To change password
   passwd

You can use variables to store info

  NAME=Bucky

   echo $NAME


Clear terminal, all data is still stored

   clear

   echo $NAME


Learn more about a commands

   info echo




Shell Tips
You can use the up arrow to cycle through previous commands

Editors
When working on servers or over a network, you don’t have GUI text editors



You can send the results of a command using

   ls > Crap


Will overwrite it if it already exists

   pwd > Crap


To append

   pwd >> Crap


For a list of programs (processes) running

   ps




Permissions

File permissions

   ls -l


permissions.jpg

r = readable
w = writable
x = executable

First is user permissions (who owns the file)
Second is permissions for group members (more on groups later)
Last are permissions for anyone (global)

To change permissions

chmod options permissions filename


Give group permission to read the file (to remove use - instead of +)

   chmod g+r Tuna


Set permissions for:
      u - user
      g - group members
      o - other people (from the outside world)

chmod u=rwx,g=rx,o=r myfile


Easier format

   chmod 754 filename


Here the digits 7, 5, and 4 each individually represent the permissions for the user, group, and others, in that order.
      4 stands for "read"
      2 stands for "write"
      1 stands for "execute"
      0 stands for "no permission”

7 is the combination of permissions 4+2+1 (read, write, and execute)



Compress files

   gzip Crap


Decompress them

   gunzip Crap.gz


For multiple files

tar cvf archive.tar file1 file2

   tar cvf Sample.tar Story Crap


      c = create mode
      v = displays output in terminal (leave it out if you don’t want to display file names)
      f = file options (argument after this must be file name of tar)

To extract files

   tar xvf Sample.tar


x = extract mode



Linux Directory Overview

      Click “Files” icon on launcher bar
      Choose “Computer” on left

Verify Checksum

      Download file
      Navigate to that directory in Terminal

    algorithm filename

   md5sum ubuntu-11.10-dvd-i386.iso

   sha1sum ubuntu-14.04-server-amd64.iso

      Compare this against the official checksum (look at this from different computers)

   sha256sum ubuntu-14.04-server-amd64.iso | grep c7bf55250ca7a7ad897fd219af6ef3d4768be54fb3e2537abb3da8f7f4ed8913

      red means they are the same



      Make new profiles
      Change background transparency

Compress files

  gzip Crap


Decompress them

  gunzip Crap.gz


For multiple files

tar cvf archive.tar file1 file2

   tar cvf Sample.tar Story Crap


      c = create mode
      v = displays output in terminal (leave it out if you don’t want to display file names)
      f = file options (argument after this must be file name of tar)

To extract files

  tar xvf Sample.tar


x = extract mode



Installing Stuff

apt - Advanced Package Tool (Linux built in software installer)

sudo apt-get update

java -version

sudo apt-get install default-jre


When you download something

      Download from internet
      Extract file

# Navigate to bin and run shell script

cd

cd pycharm-xxx/bin

bash pycharm.sh




Users

      A user is anyone who uses the computer
      Most users have restricted access to what they can do
      This is usually a good thing, keeps them from deleting important system files
      There is always a super user named “root” that can do anything
      Regular users are sometimes able to perform commands they usually couldn’t by running the command as the super user

    sudo cat shadow


Note: Even if you are the owner of a server, you usually log in as a non-super user. This help to prevent you from accidentally doing anything bad. 

Every file on Linux is owned by a User and a Group

cd

cd Desktop

ls -la


permissions | owned by user | group


# To add a new user

sudo useradd mom

sudo passwd mom




Groups

By default, whenever a user is created they belong to a group with the same name

# Create a new group

sudo groupadd girls

sudo usermod -a -G girls mom


-g will edit their primary group
-a -G just adds (appends) them to another group (keeps their primary)

To delete a user

sudo userdel mom




Linux Directory Overview

# Go to home

cd


# Go up into the root directory

cd ../..

 

ls

 

# Most of your configuration files are in here

cd etc

cat passwd


These are all the users for the system

login name | password | user ID | group ID | real name | home directory | shell path


Password symbols
x  - password stored in separate shadow folder (they are encrypted, never stored in clear text)
*  - can’t log in
::  -  no password required to log in

# won’t work

cat shadow


sudo cat shadow

cat passwd


      If you ever want to add a new user manually, just add a new line (or delete to remove user) from this file
      Need to be super user (sudo) to edit
      There are easier ways to do this, and you can just add users from the command line, but this is what happens behind the scenes



Groups

Groups are a handy way you can set permissions for a bunch of users at once

cat group


group name | password | group ID | additional members




SSH

      If you want to connect to a computer remotely (from another computer) then you can use SSH
      Buy some server online and they send you default login information
      By default we use a password, but passwords can be easily hacked
      Instead we can use a SSH key (almost impossible to crack)

First, we need to login to the server with the default credentials:

ssh root@host


It will make us change the default password, just pick a normal password for now…

Close the terminal, that’s all we needed to do for now



On our own computer, generate our keys (we will have private key on our computer, and put public key on server)

ssh-keygen -t rsa

Press enter twice (you can protect with pass phrase if you want)

# Add our public key to the server

ssh-copy-id root@host


      Type your password to verify it’s you and then your public key will be added
      Close the Terminal and log back in using

ssh root@host


We don’t need a password anymore because the key private saved on our computer get verified by the server’s public key



For extra security, disable the password login (only our SSH key has access)

sudo nano /etc/ssh/sshd_config


# Modify this line

PermitRootLogin yes

PermitRootLogin without-password


CTRL+X > y > Enter

# Put changes into effect

reload ssh


If anyone else tries to login now (even if they guess the password right)it will say access denied



SFTP

sftp root@104.236.7.12


# Go to master directory on server

cd ../..

ls -la


# The homepage for your website is in this directory

cd var/www/html

ls -la




To upload files to the server

# Move back into html

cd html

put Desktop/index.html

Now go open and browser and view:

# To upload a whole directory

mkdir Books

put -r Desktop/Books





To Download Files

# Download a file from the server

get remoteFile localFile

get index.html Desktop/index.html


# To download an entire directory (and all contents)

cd ..

ls

get -r html Desktop






1 comment:


  1. Thanks for sharing such a good content with us. keep share these kind of content.i would like to read more.
    Linux Training in Delhi

    ReplyDelete

Proper way to install nvidia 390 fix error

Proper way to install nvidia 390 if you see any error in the process look below; command  sudo apt purge --autoremove '*nvidia*...