EKT332 COMPUTER NETWORK LAB 1 INTRODUCTION TO GNU/LINUX OS
Lab #1 : Introduction to GNU/Linux OS Objectives 1. Introduction to Linux File System (Red Hat Distribution). 2. Introduction to various packages installed in Red Hat Distribution. 3. Introduction to Linux Command Line. 4. Introduction to Programming GNU C compiler. Background 1. Unix and Linux Linux is based on Unix Unix philosophy Unix commands Unix standards and conventions There is some variation between Unix operating systems Especially regarding system administration Often Linux-specific things in these areas 2. Unix System Architecture The shell and the window environment are programs Programs only access to hardware is via the kernel (figure 1) Figure 1
3. Unix Philosophy Multi-user A user needs an account to use a computer Each user must log in Complete separation of different users files and configuration settings Small components Each component should perform a single task Multiple components can be combined and chained together for more complex tasks An individual component can be substituted for another, without affecting other components 4. What is Linux? Linux kernel Developed by Linus Torvalds Strictly speaking, Linux is just the kernel Associated utilities Standard tools found on (nearly) all Linux systems Many GNU utilities Written by the Free Software Foundation Some claim the OS as a whole should be GNU/Linux Linux distributions Kernel plus utilities plus other tools, packaged up for end users Generally with installation program Distributors include: Red Hat, Debian, SuSE, Mandrake
5.Using a Linux System Login prompt displayed When Linux first loads after booting the computer After another user has logged out Need to enter a username and password The login prompt may be graphical or simple text If text, logging in will present a shell If graphical, logging in will present a desktop Some combination of mousing and keystrokes will make a terminal window appear A shell runs in the terminal window 6. Linux Command Line The shell is where commands are invoked A command is typed at a shell prompt Prompt usually ends in a dollar sign ($) After typing a command press Enter to invoke it The shell will try to obey the command Another prompt will appear Example: $ date Thu May 28 10:28:05 BST 2008 $ The dollar represents the prompt in this course do not type it
7.Command History Often it is desired to repeat a previously-executed command The shell keeps a command history for this purpose Use the Up and Down cursor keys to scroll through the list of previous commands Press Enter to execute the displayed command Commands can also be edited before being run Particularly useful for fixing a typo in the previous command The Left and Right cursor keys navigate across a command Extra characters can be typed at any point Backspace deletes characters to the left of the cursor Del and Ctrl+D delete characters to the right Make sure not to log out by holding down Ctrl+D too long 8. Creating Files with cat There are many ways of creating a file One of the simplest is with the cat command: $ cat > cucumber bread yoghurts fish fingers
Note the greater-than sign (>) this is necessary to create the file The text typed is written to a file with the specified name Press Ctrl+D after a line-break to denote the end of the file The next shell prompt is displayed ls demonstrates the existence of the new file 9. Specifying Files with Wildcards Use the * wildcard to specify multiple filenames to a program: $ ls -l *.txt -rw-rw-r--1 fred users 108 Nov 16 13:06 report.txt -rw-rw-r--1 fred users 345 Jan 18 08:56 notes.txt The shell expands the wildcard, and passes the full list of files to the program Just using * on its own will expand to all the files in the current directory: $ rm * Names with wildcards in are called globs, and the process of expanding them is called globbing 10. Process Monitoring: ps The ps command gives a snapshot of the processes running on a system at a given moment in time Very flexible in what it shows, and how: Normally shows a fairly brief summary of each process Normally shows only processes which are both owned by the current user and attached to a terminal Unfortunately, it doesn t use standard option syntax
Instead it uses a mixture of options with one of three syntaxes: Traditional BSD ps: a single letter with no hyphen Unix98 ps: a single letter preceded by a hyphen GNU: a word or phrase preceded by two hyphens 11. Process Monitoring: pstree Displays a snapshot of running processes Always uses a tree-like display, like ps f But by default shows only the name of each command Normally shows all processes Specify a pid as an argument to show a specific process and its descendants Specify a user name as an argument to show process trees owned by that user 12. Process Monitoring: top Shows full-screen, continuously-updated snapshots of process activity Waits a short period of time between each snapshot to give the illusion of real-time monitoring Processes are displayed in descending order of how much processor time they re using Also displays system uptime, load average, CPU status, and memory information
13. top Command-Line Options 14. top Interactive Commands Here is some command that may help in assisting you to complete the project: Command Ls Meaning Displays a list of files in the current working directory, like the dir command in DOS cd directory passwd file filename cat textfile change directories change the password for the current user display file type of file with name filename throws content of textfile on the screen
pwd exit or logout man command rm filename cp source_file target_file info command mv source_file target_file display present working directory leave this session read man pages on command (this will help much) remove files copy files read Info pages on command move files Exercise 1 (Linux System Administration) Write all the commands/procedure used in your answer sheet. 1. Create a directory and name it as myfolder. 2. Create a file that contain your name, date of birth, sex, marital status, home address, and phone number in the directory that you have created and name it as myinfo. 3. Create one more file that contain your student ID, IC number and name it as myid. 4. Copy myinfo file into new file name as mydetails.txt. 5. Verify how many files are in your folder now. 6. Delete myinfo file. 7. Now verify again how many files are in your folder now. 8. Get back to your home directories 9. Verify that you are in the home directory by using appropriate command. 10. Create new folder and name it as mynewfolder. 11. Now create two folders by using single command and name it with any name that you prefer. 12. Remove the two folders that you have created just by using a single command. 13. Copy all the files that exist in myfolder into mynewfolder. 14. Verify the files contain in mynewfolder.
15. Now remove myfolder. 16. What is the difference of using rmdir and rm r? 17. Verify that the folder has been removed. 18. Now by using GUI go to mynewfolder and open mydetails.txt file. 19. Edit the file by adding your name. 20. Save the file. 21. Verify the changes that you have made, in the terminal. 22. Use locate command to find the path of mydetails.txt file. 23. use cat command to display mydetails.txt file. 24. From your home directory list the files in mynewfolder. 25. Use top to show the processes running on your machine. 26. Make top sort by memory usage, so that the most memory-hungry processes appear at the top. 27. Restrict the display to show only processes owned by you. 28. Try killing one of your processes (make sure it s not an important process). 29. Display a list of all the processes running on the machine using ps (displaying the full command line for them). 30. Get the same listing as a tree, using both ps and pstree. 31. Have ps sort the output by system time used.
Programming using GNU C compiler A. Steps To Compile A Program 1. Click GNOME Menu icon (Start) > System Tools> Terminal 2. If you save your program in: a) the current directory, type gcc welcome.c OR b) a folder in the current directory, type cd foldername, then gcc welcome.c 3. Reminder You can compile and specify the output file name together with the compile command. gcc -o result welcome.c OR gcc welcome.c -o result B. Steps To Run A Program 1. Stay in the Terminal window 2. From Command Prompt, if you a) did not specify the output file name, type./a.out OR b) specify the output file name, type./result C. Logging Out To exit from the shell, use the exit command Pressing Ctrl+D at the shell prompt will also quit the shell Quitting all programs should log you out If in a text-only single-shell environment, exiting the shell should be sufficient
In a window environment, the window manager should have a log out command for this purpose After logging out, a new login prompt should be displayed Exercise 2 (Programming GNU C compiler) Write all the commands/procedure used in your answer sheet. 1. Write back all the coding in the text editor. 2. Save the file name as ilovelinux.c 3. Open the terminal and compile the coding. 4. Try to run the code. 5. Write the result after you run the code. #include <stdio.h> #include <stdlib.h> int prime(int number, int * primes); int main(void) { int primes[998] = { 3, 0 }; int n = 5, i; int count = 0, found; printf("%8d%8d%8d", 1, 2, 3); /* Print first 3 primes */ /* Find the next 997 */ while ( count < 997 ) { i = 0; found = 1; /* Test if number divides by any of the primes already found */ while ( primes[i] ) { if ( (n % primes[i++]) == 0 ) { /* If it does... */ found = 0; /*...then it isn't prime... */ break; /*...so stop looking */ } } if ( found ) { printf("%8d", n); /* If it's prime, print it... */ primes[i] = n; /*...and add it to the list */ ++count; /* Start a new line every 8 primes found */ if ( ((count + 3) % 8) == 0 ) putchar('\n'); }
n += 2; /*There's no point testing even numbers, so skip them*/ } putchar('\n'); return EXIT_SUCCESS; } Lab Discussion:- 1. Write down a simple and brief discussion of what you have learnt from this lab session. Complete all the exercises given. Submit your answer sheet to your PLV. Do not copy other students work. Make your own report.