Bootcamp: commands-exercises.txt

File commands-exercises.txt, 4.6 KB (added by dean, 5 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
1288.      Editing the command line revisited:
129
130It is particularly useful to realize that you can edit a command just as you would a line of text in a file. For instance, you can:
131
132- Use your back-arrow (left) and forward-arrow (right) keys to change text in a command.
133- Use the Home and End keys to go to the start and the end of a command:
134        + ctrl-a = start
135        + ctral-e = end
136- NOTE: you do not need to go to the end of a command before pressing <ENTER> to execute the command.
137- You can use the history command with grep to find a previous command. You can copy and paste this command, then edit it to make adjustments. For long commands this can save considerable time.
138- To terminate a command without executing it press ctrl-c
139
140- Alternatively you can use the reverse-search feature of bash:
141 1.) Press <CTRL>-R
142 2.) type the term you are searching for.
143 3.) Press <CTRL>-R again to cycle through all occurrences of the term in your history.
144 4.) Press the right or left-arrow, HOME or END key to start editing the command.
145
146Let's give some of these editing rules a try:
147
148        $ ls -lah /usr/lib/ | grep postfix
149
150Then, let's look for postfix
151
152<CTRL>-R, type “postfix”, then press left arrow. Edit the previous command (which you should now have) and change “/usr/lib/” to “/usr/sbin/”. Use the left+right arrow key to move, and backspace to erase. You should now have:
153
154        $ ls -lah /usr/sbin/ | grep postfix
155
156With your cursor just past the “/” in “/sbin/”, press <ENTER> to execute the command.
157