Introduction To Linux - NIH HPC Systems

Transcription

Introduction to LinuxBash and Basic GNU/Linux and Unix Concepts112/4/2019

This class will .Get your toes wet. Unix andLinux are gargantuan topics thatonly come into focus withexperience.Provide some basic conceptinformation for users familiarwith MacOS or Windows.Get you familiar with Linuxcommands.Get you started inunderstanding command lineinterfaces. 212/4/2019

Class outlineHistory of LinuxKernel and shellsThe bash shellFiles and directoriesFile ownership and permissionsEssential Linux commands with exercisesFile transferProcessesCompressing filescron 312/4/2019

HistoryLate 60’s through 1980’s Unix is the result of Bell Labsresearch (Dennis Ritchie, BrianKerningham, Ken Thompson, et al).Originally written in assemblylanguage. Unics (Unix) was named in contrastto MIT’s Multics operating system. Berkeley Software Distribution (BSD),or Berkeley Unix derived from BellLabs’ work in part due to governmentmonopoly agreements. Unix led to the BSD family ofoperating systems in the 1990’s.412/4/2019

HistoryRichard Stallman, in 1983 Started the GNU (GNU’s Not Unix!)project Open-sourced versions of standard suiteof Unix utilities found in BSD GNU is also a software license – allowsfor code modifications as long as they areshared Utilities used in Linux, BSD-derived andproprietary Unix operating systems All commands in this lesson are fromGNU512/4/2019

HistoryLinus Torvalds, in 1991 Released the first version of his Linuxkernel. Started as a study in processorarchitectures while at the University ofHelsinki, Finland, and to this day stillhas the authority on what getsincluded in the Linux kernel In 1992 adopted the GNU license andrapidly gathered developers Combined the GNU suite of utilitieswith a new operating system kernel(GNU/Linux)612/4/2019

HistoryBy the mid 1990’s/early 2000’sGNU/Linux starts to gathermain-stream adoption, especiallyin research and academic circlesdue to structural similarities withUnix and BSDGains large market share ofcommercial serversBecomes usable for desktopadoptionPresent on gadgets (e.g. Androidsmartphones, home routers, carinformation systems, etc) 712/4/2019

Popular Linux DistributionsRed Hat Enterprise LinuxCentOSFedoraDebianUbuntuSuse LinuxLinux Mint 812/4/2019

Linux in Science (why?) Popular due to shared functional legacy with Unix systemsassociated with research (Irix, SunOS/Solaris, etc.)Source code availability and semi-liberal licensing made it easyfor researchers to adjust the kernel as needed.Community backing and “perfect- storm” of enthusiasm for theproject led to critical mass of development (in contract to theBSD family)Licensing and well known Unix-style APIs make it easy forvendors of HPC equipment to write drivers for their hardware.Wide range of tools available for users (compilers, scientificlibraries, debuggers, etc).Performance, functionality and portability912/4/2019

Concepts: Kernel Operating system “kernel” is the core software used to“talk” to computer hardwareIt’s a core and modular system of drivers used to create astandardized environment for interfacing with hardwareResource manager for allocating memory and time tosystem and user processes as well as interacting with files(I/O)Kernel operates in its own memory or sses10MemoryDevices12/4/2019

Your Shell 11On user log-in, the system runs a shellA shell is the environment within whichyou will interface with the kernel viacommandsIt determines the syntax for complexcommand-line operations and shellscriptingThe shell you’re using is called “bash,” thesuccessor to the venerable “Bourne Shell”called “sh”BASH: “Bourne Again SHell”12/4/2019

Various Shells 12sh – the original UNIX shell (Bourne shell)bash – written as a replacement/extension ofshcsh – C shell based on the C programminglanguage developed in the late 1970stcsh – enhanced version of C shellksh – Korn shell developed in the early 1980’s,backward compatible with sh, but with somefeatures of cshzsh – extended version of sh, developed in1990dash – developed as replacement for ash inDebian12/4/2019

Linux accountsTo access a Linux system, you need to have an account A Linux account includes the following:- username and password- uid and gid- a home directory, which is where you are placed bydefault when you log in- a default shell 1312/4/2019

Using SSH to log in:But First! Introducing OpenSSH: 14SSH is the “Secure SHell”All traffic over SSH is encryptedDeveloped as a secure alternative toRSH and TelnetSSH supports a file-transfer subsystem- SCPSSH can act as an encryption layer forarbitrary network connections12/4/2019

Logging in MacOS: Finder - Applications - Utilities - Terminal Now type: “ssh username@helix.nih.gov” At the prompt, enter the account password Windows: Launch PuTTY. Under “Host Name (or IP address), type:username@helix.nih.gov 15 and click “Open”At the prompt, enter the account password12/4/2019

Logging outDON’T run this now, but to log out of a ssh session on aLinux system, you would type exit: exit1612/4/2019

More on shells What shell am I in? Typing “echo SHELL” will show you!You should see ‘/bin/bash’Typing “echo 0” will also show your shell SHELL and 0 are shell variables more about variableslaterList of available shells on the system can be displayed bytyping “chsh --list-shells”The chsh command can be used to change your defaultshell as well, but on Biowulf & Helix never change it to ashell that ends in LOCKED, SUSPENDED, DISABLED –you will lock yourself out of your account!1712/4/2019

Shell preferences When you login, startup scripts are run to setup yourenvironmentFor bash, you can customize your environment by adding ormodifying environment variables and aliases in the .bashrcfile in your home directory.Examples:alias ls ‘ls -rtl’alias bwulf 'ssh USER@biowulf.nih.gov'PATH PATH:/data/myusernameEDITOR /usr/bin/vimPS1 "[\u@\h \w \# ]"set -o noclobber1812/4/2019

Summary of Linux findgrepheadhistorylesslnlsmanmkdir19allows manipulation of textplace suspended job into backgrounddisplay calendarview contents of a filechange directorychange permissions on a file/directorycopy a fileextract a field of data from text outputcompare files line by lineoutput text to the terminal or to a filetext editorbring suspended job to foregrounddisplay file typesearch for filessearch a file or command output for a patternview beginning of filedisplay list of most recent commandsscroll forward or back through a filecreate a link to a filelist files in a directoryview information about a commandmake directorymorescroll through file a page at a timemvchange the name of a file (move)nano/picotext editorsprintenvdisplay shell variablespsshow current process informationpwdprint current working directoryrmdelete or remove a filermdirdelete or remove a directorysedstream editorsleeppausesortperform a sort of texttailview end of the filetouchcreate an empty file or update timestampstrcharacter substitution tooluniqremove identical, adjacent linesvi/vimtext editorwcprint number of lines, words or characterswhichshows full path of a commandwhoamidisplays username12/4/2019

Our cast! (of characters)CharacterName/Location\Backslash (above the enter key)/Slash (left of right shift key) Back-tick (left of the number 1, above the tab key) Pipe (shift-\)[ and ]Brackets (left of the backslash){ and }Braces or “curly” brackets (shift-[ and shift-]) and Angle brackets (left of the right shift key) Tilde (shift- )!, @, #, ,%, , &, *,(, )(!) Bang/exclamation mark, (@) at sign, (#) hash, ( )dollar/string, (%) percent, ( ) caret, (&) ampersand, (*)asterisk/start, and the left and right parenthesis.2012/4/2019

Linux Command BasicsLinux commands are case-sensitivels is not the same as LS Linux commands may have options that come after thecommand that start with a “–” and followed by a letter or “- -”and a word: ls –r ls --reverse Linux commands may allow for arguments: ls /tmp You can run more than one command on the same line byseparating the commands with a semicolon (;) ls;date Most Linux commands have a manual page or help to describehow they can be used .more about this later! 2112/4/2019

Linux Command Examplepromptcommandoptionargument ls –r /home/ USER2212/4/2019

Exercise #1: First commands[username@helix ] Type “whoami”, press Enter – who are you? Type “pwd”, press Enter – where are you? Type “echo HOME” – what does it show?Type “echo USER” – what does it show?Type “echo PWD” – what does it show? HOME, USER and PWD are three more examples ofshell variables as we saw earlier with SHELL2312/4/2019

Concepts: Files and ProcessesIn Unix, and by extension, Linux,everything is either a file or a process.Meaning everything can be interfaced viathe file system(s). Files: text, data, documents, traditionalfiles Directories: directories are special textfiles that contain a bunch of other files Devices: all disks, video hardware, audiohardware, processors, memory, USBports – all hardware can be interfacedvia files (usually in /dev) Processes: all running processes can be“seen” via the file system (in /proc) –each has a unique identifier (PID)2412/4/2019

More on Files Each file (and directory) has a nameThe filename can contain letters, numbers and specialcharacters – best to start with name or numberEvery file has a unique path to its locationExample: /home/student2/read-write.txtA filename MUST be unique within a directory thoughfiles with the same filename can exist in differentdirectoriesLike Linux commands, filenames are case sensitive so afile named “myfile” and “Myfile” can co-exist in the samedirectory – the names are different.Filenames can be lengthy2512/4/2019

More on Directories A directory is a special type of file that can hold other files –often referred to as a folder in Windows or MacOSThe “working directory” is the directory with which your shellis currently associated where you currently are! When youfirst login, you will normally be in your home directory,/home/username Use the ‘pwd’ command to print working directory Special directory notations:. refers to the current working directory. refers to the parent directory (one level back – the parentdirectory of /home/username would be /home) 2612/4/2019

Pathname Every file has a unique path to its location for example:/home/student2/Projects/docs/final report.doc ‘/home/student2’ is the home directory for student2‘Projects’ is a directory in /home/student2 - student2is the parent directory of Projects‘docs’ is a directory in ‘Projects’ – docs is asubdirectory or child directory of Projects‘final report.doc’ is a file in ‘docs’2712/4/2019

Concepts: The File System 28Linux and Unix-like file systems are arranged in a treestructure, all with the same bottom level, called “root”(/).Unlike Windows there are no drives, drive letters orany separate conceptual “space” for storage hardware.New hardware will come in the form of a “file system”attached (mounted) to some arbitrary point in thedirectory structure.12/4/2019

Linux Directory Structure //bin/boot/dev/etc/home /lib /opt/proc/sbin/tmp/usr/var 29rootbare essential commandsOS Kernelshardware devicessystem files, configurationhome directoriesLibraries needed by the system3rd party applicationsRunning processesadministrative commandstemporary spaceoperating system applicationsLogs, databases and other variable length stuff12/4/2019

cd and ls commands The “cd” command is used to change directory location. Without anargument, “cd” takes you to your home directoryThe “ls” command is used to list the files in a directory. Like many Linuxcommands, it can take a number of “flags” as options to change the behaviorof the command cd /home/ USER ls cd /etc pwd ls ls –l ls -rl cd pwd3012/4/2019

Exercise #2: “cd” and “ls” commandsType “cd /data/classes/linux” Try “ls -l” Try “ls -a” Try “ls -la” How are the above outputs different? Type “ls –lt” How is this output ordered? Type “ls –l /tmp”The above shows how providing an argument to the ‘ls’ command displaysthe contents of a directory without first changing to the directory Type “cd /home/ USER” to change to your home directory Now type “cd -” To what directory did that take you? Now type “cd .” To what directory dis that take you? Type “cd /home/ USER” to get back to your home directory 3112/4/2019

Finding your way home!The “ ” is a special character that is short-hand for“/home/username”The shell variable HOME also stores the path of“/home/username”Several ways to get to your home directory: cd cd HOME cd /home/username cd /home/ USER cdBut you can also use the “ ” and HOME as arguments withother commands: ls /tmp ls HOME/LinuxClass 3212/4/2019

Absolute and Relative paths The starting “/” in thedirectory argument explicitlyspells out a pathname –specifying an absolute or fullpathNo leading “/” means you arespecifying a path that isrelative to the currentworking directory.“cd /tmp” is different from“cd tmp”33 cd/home/username# Absolute path: cd /home/username/tmp# Relative path: cd tmp# Using These do the same: cd /tmp cd /home/username/tmp12/4/2019

Help! Many commands provide a ‘--help’ option which will display information onthe various options and there means. For example: ls --help There is also the “man” command, which will provide a manual listing onthe use of standard Linux commands such as ls one page at a time man ls One can scroll forward or back one line at a time using the up & downarrow keys and scroll forward or back one page at a time by hitting the ‘f’or ‘b’ keys, respectively.Exit out of a man page by typing ‘q’Try both of the above commands3412/4/2019

Users and Groups Users are associated with a

ls;date Most Linux . file named “myfile” and “Myfile” can co-exist in the same directory –the names are different. Filenames can be lengthy 25. More on Directories 12/4/2019 A directory is a special type of file that can hold other files – often referred to as a folder in Windows or MacOS The “working directory” is the directory with which your shell is currently associated .