Linux: Command Line Interface! October 2018 Presenting
Total Page:16
File Type:pdf, Size:1020Kb
Computing in Engineering Education presents Bytes && Bites on Linux: Command Line Interface! October 2018 Presenting Keshavamurthy Judit Flo Gaya: Indireshkumar : Systems Administrator Computational Scientist Why are you here? ● Never heard of linux, I love MacOS or Windows! ● Have heard but never used ● Have some idea and want to know a bit more ● Convince me! ● I already used it and want to know more! ● Free food! Why Linux? Where is it used? ● Open Source ● SuperComputers/Clusters ● Backbone of the internet ● Present everywhere! ● Cloud Platforms ● Research Why Linux? Where is it used? ● It is open source (free!) and highly efficient and reliable. ○ If you aren’t happy with some feature, you can change it! ○ You have full control on your computer/device with minimal space ● The top 500 supercomputers in the world are running Linux as of one year ago (https://www.top500.org/statistics/sublist/) ● Linux servers form the backbone of internet (https://en.wikipedia.org/wiki/Usage_share_of_operating_systems#Market_share_by_category) ● It is present everywhere: smartTVs, powering your Android, Kindles, 3D printers ● Cloud platforms (AWS, Google cloud, even Azure) are mostly based on linux for building linux servers, which are much cheaper than their windows counterparts (bonus, azure’s network is linux managed https://www.wired.com/2015/09/microsoft-using-linux-run-cloud/) ● Linux - essential for data science, machine learning, and deep learning. ● Cool home projects (including Internet of Things) with open software; great OS for startups etc. https://www.youtube.com/watch?v=a7RXsBQjGNM ● MacOS is based on unix (see also: https://www.lifewire.com/mac-os-x-is-not-linux-distribution-2204744) What is Linux? ● An open-source operating system ○ Very similar to Unix, which is not open source (soap opera material). We use GNU Linux/Unix interchangeably ● An operating system (OS) is the software used for all interactions with the computer’s hardware. Other examples - Windows and Mac OS X(which itself is mostly unix-like). ● Unix (Linux) “philosophy” is to design simple and highly specialized programs that have been perfected through years so that they can be used together to build a more complicated software. ● It’s the do-it-yourself philosophy to the max One Linux? ● https://en.wikipedia.org/wiki/List_of_Linux_distributions ● The main distros: ○ Debian ■ Ubuntu ■ LinuxMint ○ RedHat: ■ Fedora ■ CentOS ○ Slackware: ■ Suse The shell ● Part of OS user interacts with. Shell passes the user commands to the kernel (core of OS) ● Multiple shells: bash and tcsh are the most popular. Others: csh, mosh, etc. We use bash in this tutorial. ● There are many ways to accomplish the same task, you can pick and choose which one you like the most. Linux Command Line Interface (CLI) We will use an ssh tool to connect to a linux server. ● If you have a Mac, you can use the terminal on the Mac and the native ssh capability. There are small differences even within bash (shell) between the MacOS implementation (based on UNIX BSD) and the linux (GNU) version of it, but it will not be an issue for this workshop). ● If you have windows, please install MOBAXterm (https://mobaxterm.mobatek.net/) Basic Commands 101 ● Which user am I using?: ● See what’s in the file: >whoami >less test ● What shell are we in?: >cat test >echo $SHELL ● Create a directory ● Where am I? >mkdir dir1 >pwd ● Move files (note file is moved into a directory) ● How can I move from one directory to another: >mv test dir1 >cd <directory_name> >ls -l dir1 ● Change to directory one level up.. ● Copy files >cd .. >cp dir1/test . ● Change back to home directory .. >ls -l >cd >ls -l dir1 ● List files .. ● Removing a directory >ls >mkdir dir2 ● Listing, longer form with more details.. >rmdir dir2 >ls -l >rmdir dir1 ● Create an “empty file”: ● Cannot remove dir1! Try the following: >touch test >rm -r dir1 Basic Commands 101 (Cont) ● Moving a directory (moving is renaming!): Some fun with the terminal: >mkdir dir1 >telnet towel.blinkenlights.nl >mv dir1 dir2 >fortune >mv dir2/dir1 /tmp/dir3 ● What time is it Something useful: >date >pwgen ● What is this file? >factor 169 >file test >curl wttr.in/Boston >file dir1 >for i in {1..9}; do for j in $(seq 1 $i); do echo -ne $i× >file /usr/bin/bash $j=$((i*j))\\t; done; echo;done >file ● Copying multiple files >touch test1 test2 test3 >cp test* dir2 ● What does a command do: >man cat >man less Basic Commands 101 (Cont) ● Man is your friend! Ask for help! ● What are flags/options on commands? ls -l vs ls rm vs rm -r ● Multiple editors: pico, nano, vim, emacs, etc. ● We will concentrate on vim. Some fun references: ○ https://www.openvim.com/ ○ https://vim-adventures.com/ ● vi vs vim ● vim has two modes: normal mode and insert mode ○ When you open a file with vim, you are in the normal mode. → Command mode ○ To change to insert mode: press i (lower case letter i) → Input text ○ Leave the insert mode: press the escape key esc ○ To leave a vim session without saving, go to normal mode and : press :q! Vimtutor ● Vim ships with its own tutorial. If you type “vimtutor” at the cursor, you will be in the tutorial. ○ Work through lesson 1 in vimtutor ○ Once we are done with lesson 1, we will create a simple program (bash script) and run it. ● Now run vimtutor! Bash Scripting ● Open a file and type this: #!/bin/bash #A primitive script to make directories and copy files date rm -rf dir1 dir2 dir3 dir4 touch test1.txt test2.txt test3.txt test4.txt mkdir dir1 dir2 dir3 dir4 #Do you see anything “wrong” below? If you do, correct it! mv test.txt dir1 mv test20.txt dir2 mv test3.txt dir3 mv test4.txt dir4 date echo "All Done!" ● Run the script with: >bash files.sh Intermediate and advanced Linux 1) Useful commands: #!/bin/bash a) grep (egrep), cut b) pipe (|) for i in {1..4} c) redirection (>,>>,<) do d) wildcards touch test$i.txt e) variables (explain $) mkdir dir$i f) du and df mv test$i.txt dir$i g) ulimit done 2) Bash scripting again! a) loops echo All done b) variables c) create a script with a loop to do what files-simple.sh does. Intermediate and advanced Linux (Cont) ● OS specific topics ● Linux distributions (I’d like to do this before so that they can be a bit independent with a) processes (top, ps, ps -ef, kill (signals), their own linux) nohup, nice, etc.) a) centos, ubuntu, suse, etc. b) Memory (free, swap) b) package managers (yum, apt) c) tar, gzip, file, dd c) Installing software: fortune, cowsays, d) export command (bash), setenv (csh, tcsh) sl, bastet e) Cron ● How to remote into your linux (and others) f) users (useradd, passwd, sudo) a) Ssh g) Logs b) Scp ● Network: c) Ssh keys a) Ifconfig, ethtool d) Tunneling ssh b) proc/net ● File systems: xfs, ext3, ext4, fat, fstab c) Nfs, mount ● File structure : /etc, /root, /home d) Ping ● File permissions e) host/dig f) iptables Some URLs Linux tutorials https://people.ischool.berkeley.edu/~kevin/unix-tutorial/toc.html http://www.ee.surrey.ac.uk/Teaching/Unix/ https://ryanstutorials.net/linuxtutorial/ https://www.guru99.com/unix-linux-tutorial.html https://www.tutorialspoint.com/unix/ Bash tutorials https://linuxconfig.org/bash-scripting-tutorial-for-beginners https://ryanstutorials.net/bash-scripting-tutorial/ Online Linux Terminals http://www.webminal.org/ https://linuxcontainers.org/lxd/try-it/ https://itsfoss.com/online-linux-terminals/.