LinuxIntro: commands-exercises.txt

File commands-exercises.txt, 5.7 KB (added by b.candler, 6 years ago)
Line 
1Linux System Administration and IP Services
2
3Linux Commands
4--------------
5
6# Notes
7
8* Commands preceded with "$" imply that you should execute the command as
9  a general user - not as root.
10* Commands preceded with "#" imply that you should be working as root with
11  "sudo"
12* Commands with more specific command lines (e.g. "RTR-GW>" or "mysql>")
13  imply that you are executing commands on remote equipment, or within
14  another program.
15
161.      Log in as the sysadm user using ssh
17
18        username: sysadm
19        password: <given in class>
20
212.      Become the root user
22
23At the command prompt type the following command:
24
25        $ sudo -s
26
27        Enter your own password when prompted
28
29Now that you are root the command prompt will change. We indicate this using the “#”
30symbol.
31
32You are now the super user - be careful!
33
34Ok, exit the root account:
35
36        # exit
37        $
38
393. Look at the network configuration of your host
40
41        $ cat /etc/network/interfaces
42
43Notice that configuration of your host is done using DHCP.
44"cat" is for "concatenate" and is one way to view what is in a file.
45
464.      List files:
47
48Use ls to list files:
49
50        $ cd                            [go to your home directory]
51        $ ls
52
53Do you see anything? Try this instead:
54
55        $ ls -lah
56
57What's inside one of these files?
58
59        $ cat .profile
60        $ less .profile
61        $ clear
62
63Press “q” to get out of the less display.
64
65If you don't understand what cat, clear or less do, then type:
66
67        $ man cat
68        $ man clear
69        $ man less
70
715.      Working with the command prompt:
72
73You can recall previous commands by using the up-arrow and down-arrow keys. Give this a try now.
74
75Alternately, try typing this command:
76
77        $ history
78
79If you wish to execute one of the commands in the list you saw type:
80
81         $ !nn
82
83Where “nn” is the number of the command in the history list. This is useful if you want to run a past command that was long and/or complicated.
84
85Command completion:
86
87With the bash shell you can auto-complete commands using the tab key. This means, if you type part of a command, once you have a unique string if you press the TAB key the command will complete. If you press the TAB key twice you'll see all your available options. Your instructor will demonstrate this, but give it a try by doing:
88
89$ hist<TAB>
90$ del<TAB><TAB>
91$ rm <TAB><TAB>         [Include the space after the “rm”]
92
93
946.      Working with pipes:
95
96We saw an example of using pipes when we sorted the contents of our /sbin directory during the presentation. What if you wanted to have this information available in a file and sorted?
97
98        $ cd
99        $ ls /sbin | sort > sbin.txt
100
101Now view the contents of what is in sbin.txt to verify that this worked.
102
103        $ less sbin.txt
104
105Press the "q" key to quit viewing the contents.
106
107
1087.      Finding text strings:
109
110Use the command grep to print lines matching a pattern in a data stream (such as a file). For example, view the entry for the sysadm account in the system passwd file:
111
112        $ grep sysadm /etc/passwd
113
114You should see something like:
115
116sysadm:x:1000:1000:System Administrator,,,:/home/sysadm:/bin/bash
117
118The previous items above are:
119
120        userid:passwd:uid:gid:Name,extrastuff,,:HomeDir:LoginShell
121
122grep is often used with a pipe to FILTER the output of commands. For instance:
123
124        $ history | grep ls
125
126Will display your previous use of the ls command from exercise 2.
127
128Now try this:
129
130        $ grep sysadm /etc/shadow
131
132Why doesn't it work? Change it to the following, why does it work now?
133
134        $ sudo grep sysadm /etc/shadow
135
136
1378.      Stopping and starting a service
138
139In a web browser go to your machine's home page:
140
141        http://vmN.ws.nsrc.org/
142
143You should see something like "It Works!" on the page. Now, let's stop the web server (Apache) that is installed on your virtual machine. To do this you can do:
144
145        $ sudo service apache2 stop
146
147Now reload the web page for your machine. It should indicate that no web server was found. Now let's start the service again:
148
149        $ sudo service apache2 start
150
151You can see if a service is running by typing:
152
153        $ sudo service apache2 status
154
155If a process ID is displayed, then the service is running, but our next exercise will show you another way to verify this.
156
157
1589. Finding and stopping processes
159
160If you wish to find something that is running and then stop it you can use the "ps" (process) command with "grep" and "kill". Let's do this by opening two connections to your virtual machine.
161
162        a. Using SSH open two terminal connections to your Linux server (user: sysadm machine: vmN.ws.nsrc.org)
163        b. One you have opened two terminals go in to one terminal and type:
164
165        $ tail -f /var/log/syslog
166
167This will let you look at the end of the syslog log file in real time. If events take place that are logged you will see them as they happen. Now, in your other terminal let's look for this process:
168
169        $ ps auxwww | grep tail
170
171The "auxwww" are options to the ps (process) command. The options mean display all process running that belong to you and to other users and provide information about who owns what process. The three "www"'s mean display the entire command line regardless of how long it is and wrap it in your window.
172
173You will likely see something like this:
174
175root      6903  0.0  0.0   7200   612 pts/0    S+   03:28   0:00 tail -f /var/log/syslog
176root      6986  0.0  0.0   9388   924 pts/1    S+   03:28   0:00 grep --color=auto tail
177
178You could press "CTRL-C" in the terminal window where the tail command is running, or to stop the process right now you can use the kill command. You need to replace the Process ID (PID) with the process ID number of the tail command running on your machine. In this example the number is "6903". At the command prompt type:
179
180        $ kill NNNN
181
182Where NNNN is the PID of your tail process. Once you do this return to the other terminal screen. The "tail -f" process should now have exited and you should see something like:
183
184        Terminated
185        sysadm@vmN:~$
186
187 
188
189
190
191
192
193
194
195