UNIX and Linux System Administration and Shell Programming
Total Page:16
File Type:pdf, Size:1020Kb
UNIX and Linux System Administration 9/3/12 6:35 PM UNIX and Linux System Administration and Shell Programming version 22 of September 3, 2012 Copyright © 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012 Milo This book includes material from the http://www.osdata.com/ website and the text book on computer programming. Distributed on the honor system. Print and read free for personal, non-profit, and/or educational purposes. If you like the book, you are encouraged to send a donation (U.S dollars) to Milo, PO Box 5237, Balboa Island, California, USA 92662. This is a work in progress. For the most up to date version, visit the website http://www.osdata.com/ and http://www.osdata.com/programming/shell/unixbook.pdf — Please add links from your website or Facebook page. file:///Volumes/Mac%20FreeAgent%20GoFlex%20Drive/from%20key%20disk/osdata%20stripped/programming/shell/shellbook.html Page 1 of 146 UNIX and Linux System Administration 9/3/12 6:35 PM UNIX and Linux Administration and Shell Programming chapter 0 This book looks at UNIX (and Linux) shell programming and system administration. This book covers the basic materials needed for you to understand how to administer your own Linux or UNIX server, as well as how to run your own personal desktop version of Linux or Mac OS X. This book goes beyond the typical material in a shell scripting class and presents material related to either downloading and compiling existing software (including ports to new hardware and/or operating systems) or for preparing your own software for release via the internet. requirements You need a willingness to learn. You need a working computer or server or access to one. The computer needs a working version of UNIX, Linux, Mac OS X, AIX, HP/UX, Solaris, etc. (it can be a dual boot computer). The new version of Mac OS X 10.8.1 (Mountain Lion) is now available on the Mac App Store at www.apple.com as of August 23, 2012. The new version of Mac OS X 10.8 (Mountain Lion) is now available on the Mac App Store at www.apple.com as of July 25th, 2012. Tell them you heard about it from www.osdata.com when you register your new copy. You need a working connection to the internet, preferably a high speed connection. You may want to have a domain name of your own and web hosting to try out controlling a server. There is a chapter on how to use GoDaddy to obtain these servcies for low cost with great telephone tech support. The OSdata.com website where this book is offered to the public is hosted by Host Gator. You may use any other hosting service you want. options Almost anyone can slog through and learn at least some of this material, but an aptitude for this material greatly helps learning. If you are strong at grammar, then you will probably be able to master. This material. mathematical ability is useful, but not necessary. Many portions of this book require root or administrator access. While you learn better if you can actually try out each command for yourself, you can just read about root material if you don’t have root or administrator access. Some portions of this book require special software. Most of the software can be downloaded for free. Those with Mac OS X should have the Developer Tools installed. These are available for free on either the install DVD/CD or from Apple at http://connect.apple.com/ A static IP address is in general useful and is required for some portions of this book. chapter contents 1. cool shell tricks 2. basics of computers 3. UNIX/Linux history 4. choice of shells 5. connecting to a shell (Telnet and SSH; terminal emulator) 6. shell basics (book conventions; root or superuser; starting your shell; login and password; prompt; command example) 7. login/logout (login; select system; account name; password; terminal type; logout; exit) 8. passwd (setting password; local password; periodic changes; 100 most common passwords; secure passwords; superuser) 9. command structure (single command; who; failed command; date; options, switches, or flags; universal time; arguments; options and arguments; operators and special characters) 10. quick tour of shell commands 11. man (using man for help; man sections) 12. cat (creating files; example files for this book; viewing files; combining files) file:///Volumes/Mac%20FreeAgent%20GoFlex%20Drive/from%20key%20disk/osdata%20stripped/programming/shell/shellbook.html Page 2 of 146 UNIX and Linux System Administration 9/3/12 6:35 PM 13. command separator (semicolon) 14. less, more, pg 15. file system basics (graphics examples; directory tree; important directories; home directory; parent and child directories; absolute paths; relative paths; dots, tildes, and slashes) 16. pwd 17. command history 18. built-in commands 19. ls 20. cd 21. cp 22. mv 23. rm (recursive) 24. sysadmin and root/superuser 25. sudo 26. su 27. who 28. major directories 29. shred 30. df 31. du 32. ps 33. w 34. uptime 35. top 36. free 37. vmstat 38. defaults (screencapture; Mac Flashback Trojan) 39. init (init; Linux run levels) 40. ifconfig (view configuration; static IP address) 41. arp 42. netstat (view connections; main info; routing address) 43. route (view connections; routing commands) 44. ping (test packets; measuring) 45. nslookup 46. traceroute (entire route; etiquette) 47. sysstat 48. at (example; removing a job; timing) 49. tar 50. touch (multiple files; specific time) 51. find 52. sed (fixing end of line; adding line numbers) 53. awk (remove duplicate lines) 54. screencapture (from graphic user interface; changing defaults; command line screenshots) 55. installing software from source code 56. test bed Appendix: A. computer history B. Forth-like routines file:///Volumes/Mac%20FreeAgent%20GoFlex%20Drive/from%20key%20disk/osdata%20stripped/programming/shell/shellbook.html Page 3 of 146 UNIX and Linux System Administration 9/3/12 6:35 PM cool shell tricks for UNIX, Mac OS X, and Linux chapter 1 summary This chapter looks at cool shell tricks for UNIX, Linux, and Mac OS X to give you an idea of the power of the shell. A quick summary of how to get to the shell is included in this chapter (more detailed explanations, including what to do when things go wrong, are in following chapters. If you need a primer on computer terminology, please look at the next chapter on basics of computers. cool shell tricks This chapter has a handful of cool shell tricks. These are intended to show a beginner that a command line shell can be as fun as any graphic user interface and get across the idea that there is a lot of power in the shell that simply doesn’t exist in a standard graphic user interface. definitions UNIX is one of the ground-breaking operating systems from the early days of computing. Mac OS X is built on top of UNIX. Linux is a variation of UNIX. The shell is the command line interface for running UNIX (and Mac OS X and Linux) with just typing (no mouse). operating system The software that provides a computer’s basic tasks, such as scheduling tasks, recognizing input from a keyboard, sending output to a display screen or printer, keeping track of files and folders (directories), running applications (programs), and controlling peripherals. Operating systems are explained in more detail for beginners just below. UNIX UNIX (or Unix) is an interactive multi-user multitasking timesharing operating system found on many types of computers. It was invented in 1969 at AT&T’s Bell Labs by a team led by Ken Thompson and Dennis Ritchie. Some versions of UNIX include: AIX, A/UX, BSD, Debian, FreeBSD, GNU, HP-UX, IRIX, Linux, Mac OS X, MINIX, Mint, NetBSD, NEXTSTEP, OpenBSD, OPENSTEP, OSF, POSIX, Red Hat Enterprise, SCO, Solaris, SunOS, System V, Ubuntu, Ultrix, Version 7, and Xenix. Linux An open-source version of the UNIX operating system. graphical user interface A graphical user interface (GUI) is a windowing system, with windws, icons, and menus, operated by a mouse, trackball, touch screen, or other pointing device, used for controlling an operating system and application programs (apps). The Macintosh, Windows, Gnome, and KDE are famous examples of graphical user interfaces. command line interface A command line interface (CLI orcommand line user interface CLUI) is a text only interface, operated by a keyboard, used for controlling an operating system and programs. shell The shell is the command line interface for UNIX, Linux, and Mac OS X. In addition to intrepetting commands, it is also a programming language. shell uses UNIX (and therefore also Mac OS X and Linux) has more than 200 basic commands (also called tools or utilities) that are distributed with the standard operating system. This collection and the ease with which they work together is the major source of the power of UNIX. The vast majority of these standard tools are designed to be used from a command line (the shell). The shell is most commonly used to control servers. Servers are the computers used to host websites. The most common operating system for the world’s web servers is Linux. If you learn shell scripting and system administration, you can run your own server and possibly get a job. The shell can be used to control a desktop or portable computer. Some tablets and smart phones have a shell. The iPhone actually has a shell, but it can’t be accessed witout jailbreaking the iPhone. The shell will often run even when a computer is partly broken.