Introduction to remote command line Linux Research Computing Team University of Birmingham
Linux/UNIX/BSD/OSX/what? v All different v UNIX is the oldest, mostly now commercial only in large environments (e.g. banking mainframe) v Practically from a user perspective, the same v Focus on command line tools
Distributions of Linux v Groups of software at specific versions v Lots of them: Red Hat Fedora Suse Debian Ubuntu Mint..
Remote graphical applications v UoB only provide BlueBEAR v No graphical desktop (Gnome/KDE/Unity) v Remote connection via ssh (PuTTy) v X server running for graphical applications Exceed (UoB desktops) Xming (free) Xquartz (OSX)
Practical v http://www.birmingham.ac.uk/bear Then follow these links in order: v Services v Linux Compute (BlueBEAR) v Access & File Transfer v Windows software to access BlueBEAR v the help page for installing and configuring PuTTY and Exceed (last paragraph of the Windows software to access BlueBEAR section) or v http://tinyurl.com/bear0915
Graphical text editing v From the command prompt run gedit to get a graphical text editor running on the remote system v In the X server you have two copy buffers Copy and paste, as normal Middle click buffer
Exceed (useful tip) v Select text to place it into the copy buffer v Middle click to paste text
A word about directory layout v Unlike Windows (and DOS systems) there are no drives (i.e. no C:\, D:\, U:\ etc.) v Everything exists under a directory, /, called the root directory. v Useful directories to know: /home each user usually has a directory here with their own files in it, known as their home directory. v On BlueBEAR users home directories are located at /gpfs/bb, not /home, for technical reasons, but /home still exists. /tmp temporary files. Any user can create files in this directory. Often deleted when the computer reboots. v On BlueBEAR there is also /scratch on each compute node which is for the same purpose but much larger.
What is a path? A concrete strip to the bottom of the garden v Where the system looks to find programs echo $PATH v Command not found the program isn t in your path
Welcome to the command line v Files and programs are CaSe-SeNsItIvE v You type commands into a program called the shell v Commands are case-sensitive v The tab key is magic, pressing it: once: complete the current command or filename if there is 1 match twice: list all the matching options if there is more than 1 match
Command line syntax v Varies from program to program v Typically of the form tar -cpf foo.tar ~/to_archive/ <command> -<character> <argument> <file> tar -cpf foo.tar ~/to_archive/
Help? How do I use this command? v Each command should have a manual page for it. To view it type man command, e.g. to view the manual for the man command type: man man Press q to quit, use the arrow keys to scroll
Looking at your files v Basic file commands: ls list files cd change directory pwd print current (working) directory
Looking at your files (continued) v Basic file commands: less filename view the contents of your (text) files (press q to quit), space to page down, up/down arrows
Looking at your files (continued) v Basic file commands: tail -f filename watch the end of file as it grows
Organising your files v Basic directory management commands: mkdir newdirectory create directory rmdir directory remove directory (only works if directory is empty)
Organising your files v Basic file management commands: touch file create an empty file rm file remove a file (there is no recycle bin!) cp file newfile copy a file (creates a duplicate) mv file newfile move a file (renames the file)
dot files and directories v User config settings stored in them v Please don t delete! v Useful ones to edit (to set things every time you login):.bash_profile.bashrc BUT BE CAREFUL!
Links v There are two types of links on unix systems: symbolic links known as symlinks, like a shortcut to the file hard links a new reference to the same file (you are very unlikely to ever use these) v Creating a symlink: Use the ln link command with the -s, symlink, option. E.g. to create a link to /gpfs/bb/username/myfile called /tmp/link_to_myfile type: ln s /gpfs/bb/username/myfile /tmp/link_to_myfile
Users and groups v Username maps to a numeric UID Command id returns details v Member of least one group v Group names map to a numeric GID Command groups lists your groups groups username
User permissions v By default users have no special permission v root is equivalent of administrator v Command su - username would change to that user if you know the password v Modern systems use sudo to do commands with elevated permission (or as another user) v UoB central systems don t allow sudo or su to normal users
File Permissions v Files (and directories) can have different permission sets for groups and users Files r read the file w write to the file x execute (run) Directory r list contents of directory w create new files/folders x traverse (e.g. cd to directory)
File Permissions v Try doing ls ld ~/ v This shows permissions on home directory v First character indicates file (-), directory (d) or link (l), next 3 characters is user, next 3 group, final 3 is all other users v + sign indicates extended ACLs applied
File permissions mkdir testdir touch testdir/testfile chmod is used to change permissions chmod g+rwx testdir (then check with ls -ld testdir) chmod o+x testdir/testfile
File permissions v As well as using rwx etc, bit masks can also be used, the following are equivalent: chmod 755 directory chmod u+rwx,go+rx directory
Shell variables v Lots set at login env v Can set own export FOO=bar echo $FOO Value of $FOO is now available to other things in shell v ~/ - shortcut to home directory v ~someuser/ - shortcut to another user home directory
Some useful tools v grep string filename Look for string occurrences in the file grep ldap /etc/nsswitch.conf What does do? grep c ldap /etc/nsswitch.conf
Some useful tools v diff file1 file2 Shows the differences between two files diff /etc/nsswitch.conf ~/nsswitch.conf
Editing files v Can edit from Windows and use share Windows files use different line endings dos2unix ~/filename v Lots of editors under Linux nano is a basic, easy to use one Others joe, vim, emacs
Editing files v Create and edit a new file in home directory: nano ~/testfile.txt Type in some text Save the file with CTRL+o Exit with CTRL+x Check the contents of the file (Remember cat or less?) Edit the file again, try CTRL+k, CTRL+u, CTRL+w to see what they do
Pipes and redirection v (pipe) can be used to send output of command into another ls al /gpfs/bb grep $USER v > redirects the output of a command E.g. to a file STDOUT STDERR 2>&1
Pipes and redirection v Try these: ls al /gpfs/bb grep $USER ls al ~/ > ~/ls.output Cdgffgffg > ~/error.output Cdgffgffg > ~/error.output 2>&1
Scripts v Allow series of commands to be repeated v Can pass arguments in, use variables etc. v Need to be executable to run from command line v First line shows the interpreter (or shell) to use, e.g. #!/bin/bash #!/usr/bin/perl
Scripts v Use the text editor to create a new script file to: Create a directory (mkdir) Change to a directory (cd) List the directory contents with all details (ls) Create a new empty file (touch) List the directory contents with all details (ls) v Remember to set the shell in the first line #!/bin/bash v Make it executable (chmod) v Run the script./scriptname (You could also try redirecting the output to a file)
Scripts #!/bin/bash mkdir ~/tempdir cd ~/tempdir ls -al touch somefile ls -al
Archiving and compressing v A tar file (traditionally tape archive) is a way of grouping a set of files together into a single file v gzip or bzip2 are often used to compress files v So for example a.tar.gz file would be similar to a zip file
Archiving and compressing v Create a new tar file, being verbose of the files, preserving file permissions: tar -cvpf ~/newfile.tar ~/testdir v Compress the file: gzip -9 ~/newfile.tar v List contents of compressed file: tar -tzf ~/newfile.tar.gz v Extract contents to a new directory: mkdir ~/newdir cd ~/newdir tar -xvzpf ~/newfile.tar.gz
Process management v Everything you execute causes at least 1 process to be generated v Programs which fork may create multiple subprocesses v The command top shows the processes running using most CPU (q to exit)
Process management v Processes may be in several different states: Running in foreground Suspended (stopped, no further processing happens) Backgrounded (running but no longer attached to terminal) Zombie (In a bad way, typically no longer doing anything useful)
Process management v Command line processes can be stopped using CTRL+z v Running bg will then background the process v Jobs lists processes from the current terminal and their state v fg returns a suspended or background process to the controlling shell
Process Priority v Each process has a priority which determines how much CPU time will be allocated when the CPU is busy with many applications v renice can be used to change the priority of a process (users can only reduce) v On shared systems, it is sociable to renice heavy compute processes which you are leaving running
Looking for processes v The command ps will list your running processes and their processid v ps -ef grep vi Would look for processes called vi
Signals v Signals are used to tell processes to do something, typically: To tell a process to reread a config (HUP) To tell a process to terminate (TERM) To forcefully kill a process (KILL) HUP and TERM rely on signal handler in code kill -SIGNAL processid
Any questions?