1 | |
---|
2 | SSH Exercises - PacNOG 10 |
---|
3 | |
---|
4 | In these exercises, you can use your own laptop as the client computer. |
---|
5 | This means you can use either "Putty" or another SSH client if you have one. |
---|
6 | |
---|
7 | If you have a Unix machine, you can use the "ssh" command. Or if you would |
---|
8 | like, you can use the workshop pc as the client, and have the pc connect |
---|
9 | to itself, or have it connect to your neighbor workshop pc as the server. |
---|
10 | |
---|
11 | If you have a Windows machine, you can use putty. Download from: |
---|
12 | http://www.chiark.greenend.org.uk/~sgtatham/putty/ |
---|
13 | Use the "puttygen" tool to create keys. |
---|
14 | |
---|
15 | Things we'll practice in these exercises: |
---|
16 | |
---|
17 | -- automatic SSH key logins. |
---|
18 | -- using scp command. |
---|
19 | -- edit the sshd configuration and |
---|
20 | -- automatic logins as root. |
---|
21 | -- ssh-agent automatic logins. |
---|
22 | |
---|
23 | ------------------------------------ |
---|
24 | |
---|
25 | I. SSH User Keys |
---|
26 | |
---|
27 | a) Note, look at a regular simple SSH client login to start |
---|
28 | |
---|
29 | Login to your workshop pc with putty, or with |
---|
30 | your ssh client. Notice: this is a "system" login. It |
---|
31 | is using the shadow file/password file. So this is known |
---|
32 | as a simple "password" login. |
---|
33 | |
---|
34 | b) Generate User SSH Keys on Your Client |
---|
35 | |
---|
36 | If you are using a Unix client or workshop pc: |
---|
37 | |
---|
38 | % ssh-keygen # the default |
---|
39 | % ssh-keygen -t rsa -b 2048 # here's another way to do it |
---|
40 | |
---|
41 | NOTE: In these examples, just press <RETURN> instead of using a password on the key. |
---|
42 | |
---|
43 | Look in the .ssh directory to verify the new keys have been created. |
---|
44 | |
---|
45 | % ls -ld .ssh |
---|
46 | % ls -l .ssh/* |
---|
47 | |
---|
48 | What Unix permissions are set on the SSH directory? |
---|
49 | What Unix permissions are set on the SSH keys? |
---|
50 | |
---|
51 | ------------------------------------ |
---|
52 | II. authorized_keys |
---|
53 | |
---|
54 | Now let's do some automatic logins. |
---|
55 | |
---|
56 | a) Copy your public key to the machine you want to login into. |
---|
57 | |
---|
58 | % cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys |
---|
59 | |
---|
60 | NOTE: if you are using "dsa" keys, use the "dsa" name. |
---|
61 | NOTE: If you are using "rsa" keys, use the "rsa" name. |
---|
62 | NOTE: We use >> to append to the file, so we don't wipe |
---|
63 | out the file every time, rather we add to the end of the file. |
---|
64 | |
---|
65 | Now try it. ssh to the same machine. |
---|
66 | |
---|
67 | % ssh localhost |
---|
68 | % exit |
---|
69 | |
---|
70 | Did you have to type a password? Turn on debugging to |
---|
71 | watch the SSH client make decisions: |
---|
72 | |
---|
73 | % ssh -v -v -v localhost |
---|
74 | |
---|
75 | b) Pick a partner machine, and add your public keys to their |
---|
76 | ~sysadmin/.ssh/authorized_keys file |
---|
77 | Do this between one machine and other machine, for |
---|
78 | example: pc1 -> pc2 and pc1 -> pc2 |
---|
79 | |
---|
80 | Here is pc1 installing on pc2: |
---|
81 | |
---|
82 | % cat ~/.ssh/id_rsa.pub | ssh sysadm@pc2 'cat >> .ssh/authorized_keys' |
---|
83 | |
---|
84 | Now try an ssh login to pc2: |
---|
85 | |
---|
86 | % ssh sysadm@pc2 |
---|
87 | % exit |
---|
88 | |
---|
89 | Did you have to type a password? |
---|
90 | You now can automatically run commands on the remote system: |
---|
91 | |
---|
92 | % ssh sysadm@pc2 w |
---|
93 | % ssh |
---|
94 | |
---|
95 | ------------------------------------ |
---|
96 | |
---|
97 | III. scp commands |
---|
98 | |
---|
99 | Now that we have automatic login, we can automatically |
---|
100 | copy files from one system to another. |
---|
101 | |
---|
102 | Make a directory to test with. |
---|
103 | |
---|
104 | % cd ; mkdir myjunk ; echo "HI" > myjunk/myfile |
---|
105 | |
---|
106 | Now let's copy that to another system: |
---|
107 | |
---|
108 | % scp -rp myjunk sysadm@pc2: |
---|
109 | |
---|
110 | *** WARNING *** |
---|
111 | For "scp", use the ":" on the end of the command. This is required |
---|
112 | in order to tell the "scp" that it is the end of the command, not |
---|
113 | that the target is a local filename. If you said "sysadm@pc2" instead, |
---|
114 | it would create a file locally called "sysadm@pc2", instead of try to |
---|
115 | connect to the remote machine pc2. |
---|
116 | |
---|
117 | Now let's check for files: |
---|
118 | |
---|
119 | % ssh pc2 -l sysadm ls -rl myjunk |
---|
120 | |
---|
121 | What does it mean when we used "-rp" on the scp command? |
---|
122 | |
---|
123 | % man scp |
---|
124 | |
---|
125 | ------------------------------------ |
---|
126 | |
---|
127 | IV. SSHD configuration |
---|
128 | |
---|
129 | Look at the configuration file. |
---|
130 | Does your system permit root logins via ssh? |
---|
131 | |
---|
132 | % cd /etc/ssh |
---|
133 | % less sshd_config |
---|
134 | |
---|
135 | If you the "PermitRootLogin" option is set to "no", |
---|
136 | edit the file and change the setting to "yes". |
---|
137 | |
---|
138 | % sudo service ssh restart |
---|
139 | |
---|
140 | ------------------------------------ |
---|
141 | |
---|
142 | V. root automatic login |
---|
143 | |
---|
144 | Now let's try do do this as root. NOTE: we are generating |
---|
145 | automatic root access. Be careful with commands like "rm". |
---|
146 | |
---|
147 | a) first try it one your own machine |
---|
148 | |
---|
149 | % sudo - |
---|
150 | # su - |
---|
151 | # pwd |
---|
152 | |
---|
153 | NOTE: the su command was used to get into the root directory. |
---|
154 | You should now be in the "/root" home directory. |
---|
155 | Geneate some keys to create the directory for ssh automatically. |
---|
156 | |
---|
157 | # ssh-keygen |
---|
158 | |
---|
159 | Now, exit back to yourself and copy in your public key: |
---|
160 | |
---|
161 | % sudo cat ~/.ssh/id_rsa.pub >> /root/authorized_keys |
---|
162 | |
---|
163 | Now, give it a try. |
---|
164 | |
---|
165 | % ssh root@localhost |
---|
166 | |
---|
167 | Did it work? Do you see the "#" root prompt? |
---|
168 | |
---|
169 | b) now let's try and get automatic root on your partner machine |
---|
170 | |
---|
171 | First make sure the partner has sudo and a .ssh directory. |
---|
172 | |
---|
173 | % ssh pc2 -l sysadm |
---|
174 | % sudo -s |
---|
175 | # ls -ld /root/.ssh |
---|
176 | # exit |
---|
177 | |
---|
178 | Let's be careful this time about moving the file. |
---|
179 | Let's copy it to a /tmp file, then login and move |
---|
180 | the file in place on the remote system. |
---|
181 | |
---|
182 | % scp ~/.ssh/id_rsa.pub sysadm@pc2:/tmp/pc1key.pub |
---|
183 | % ssh pc2 -l sysadm |
---|
184 | % sudo cat /tmp/pc1key.pub >> /root/.ssh/authorized_keys |
---|
185 | exit |
---|
186 | |
---|
187 | Now give it a try. |
---|
188 | |
---|
189 | % ssh root@pc2 w |
---|
190 | % ssh root@pc2 id |
---|
191 | |
---|
192 | ------------------------------------ |
---|
193 | |
---|
194 | VI. ssh-agent |
---|
195 | |
---|
196 | We can load keys into memory on the local machine, and |
---|
197 | use those keys automatically. This is helpful if you have |
---|
198 | a password on your key and you don't want to type the |
---|
199 | password all the time. It's also helpful if you have multiple |
---|
200 | identities and want to load them all. |
---|
201 | |
---|
202 | If you are doing this with "putty" on Windows, the |
---|
203 | "pageant" tool can be used instead of ssh-agent. |
---|
204 | |
---|
205 | a) wipe out your old keys |
---|
206 | |
---|
207 | % cd |
---|
208 | % rm .ssh/id_rsa.pub |
---|
209 | % rm .ssh/id_rsa |
---|
210 | |
---|
211 | b) generate a new key, but this time, enter a password |
---|
212 | when it requests a password. Now when you use this key, |
---|
213 | you will have to type the password for the key. |
---|
214 | |
---|
215 | % ssh-keygen -t rsa -b 2048 |
---|
216 | |
---|
217 | (It will force you to pick a good password.) |
---|
218 | |
---|
219 | Now start the ssh-agent and add a key to the agent. |
---|
220 | NOTE: By default ssh-agent will add the default name keys. |
---|
221 | NOTE: You have to have the environment variables set so |
---|
222 | that ssh can find the ssha-agent socket, so.... |
---|
223 | |
---|
224 | % ssh-agent -s > sshenv |
---|
225 | % source sshenv |
---|
226 | |
---|
227 | Now make sure your authorized_keys file is correct: |
---|
228 | |
---|
229 | % cat ./ssh/id_rsa.pub >> ./ssh/authorized_keys |
---|
230 | |
---|
231 | Now you can add your key: |
---|
232 | |
---|
233 | % ssh-add |
---|
234 | (or) |
---|
235 | % ssh-add .ssh/id_rsa |
---|
236 | |
---|
237 | To list keys that are in the agent: |
---|
238 | |
---|
239 | % ssh-add -l |
---|
240 | |
---|
241 | And you can login to localhost now without the |
---|
242 | having to type the private-key password again. |
---|
243 | |
---|
244 | % ssh localhost |
---|
245 | |
---|
246 | You can do the same thing with loggin onto remote systems. |
---|
247 | You only enter the private-key password once to load the key |
---|
248 | into memory. From then on, the password is given for you |
---|
249 | by ssh-agent. |
---|
250 | |
---|
251 | --------------------------------------------- |
---|
252 | |
---|