Guided Exercise SSH
Before
- Clone your VM into
serveraandserverb -
Change
serverahostname intoserveraandserverbhostname intoserverb -
Create user
operator1by passwordredhatonserveraandserverb -
Create user
studentonserverb -
Add
tambahkan line berikut namun sesuaikan dengan ipaddress servera milik andaserveraat/etc/hosts/
-
From your
windows cmd, open an SSH session to serverb as student -
Use the
sucommand to switch to theoperator1user onserverb. Userredhatas the password ofoperator1 -
Use the
ssh-keygencommand to generate SSH keys. Do not enter a passphrase.[operator1@serverb ~]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/operator1/.ssh/id_rsa): enter Created directory '/home/operator1/.ssh'. Enter passphrase (empty for no passphrase): enter Enter same passphrase again: enter Your identification has been saved in /home/operator1/.ssh/id_rsa Your public key has been saved in /home/operator1/.ssh/id_rsa.pub -
Use the
ssh-copy-idcommand to send the public key of the SSH key pair tooperator1onservera. Useredhatas the password ofoperator1onservera.
[operator1@serverb ~]$ ssh-copy-id operator1@servera_ip_address /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/operator1/.ssh/id_rsa.pub" The authenticity of host '192.168.1.5 (192.168.1.5)' can't be established. ED25519 key fingerprint is SHA256:Xbp9Sh3L8OwG1pj62suEtSTM2CuJvcAty0GAaVdouHE. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys operator1@192.168.1.5's password: redhat -
Execute the
Notice that the preceding ssh command did not prompt you for a password because it used the passphrase-less private key against the exported public key to authenticate as operator1 on servera. This approach is not secure, because anyone who has access to the private key file can log in to servera as operator1. The secure alternative is to protect the private key with a passphrase, which is the next step.hostnamecommand onserveraremotely using SSH without accessing the remote interactive shell. -
Use the
ssh-keygencommand to generate another set of SSH keys with passphraseprotection. Save the key as/home/operator1/.ssh/key2. Useredhatpassas the passphrase of the private key
[operator1@serverb ~]$ ssh-keygen -f .ssh/key2 Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): redhatpass Enter same passphrase again: redhatpass Your identification has been saved in .ssh/key2 Your public key has been saved in .ssh/key2.pub The key fingerprint is: SHA256:sZJuY84PaRxyW87F501M0mwTlVAWDXbtgV9fe9goi9E operator1@serverb The key's randomart image is: - Use the
ssh-copy-idcommand to send the public key of the passphrase-protected key pair tooperator1onservera.[operator1@serverb ~]$ ssh-copy-id -i .ssh/key2.pub operator1@servera /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/key2.pub"The authenticity of host 'servera (172.16.70.27)' can't be established. ED25519 key fingerprint is SHA256:Xbp9Sh3L8OwG1pj62suEtSTM2CuJvcAty0GAaVdouHE. This host key is known by the following other names/addresses: ~/.ssh/known_hosts:1: 192.168.1.5 ~/.ssh/known_hosts:4: 172.16.70.27 Are you sure you want to continue connecting (yes/no/[fingerprint])? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'operator1@servera'" and check to make sure that only the key(s) you wanted were added. - Execute the
hostnamecommand onserveraremotely with SSH without accessing the remote interactive shell. Use/home/operator1/.ssh/key2as the identity file. Specifyredhatpassas the passphrase, which you set for the private key in the preceding step.
Notice that the preceding[operator1@serverb ~]$ ssh -i .ssh/key2 operator1@servera hostname Enter passphrase for key '.ssh/key2': serverasshcommand prompted you for the passphrase you used to protect the private key of the SSH key pair. This passphrase protects the private key. Should an attacker gain access to the private key, the attacker cannot use it to access other systems because the private key itself is protected with a passphrase. The ssh command uses a different passphrase than the one foroperator1onservera, requiring users to know both.
You can usessh-agent, as in the following step, to avoid interactively typing in the passphrase while logging in with SSH. Usingssh-agentis both more convenient and more secure in situations where the administrators log in to remote systems regularly. - Run
ssh-agentin your Bash shell and add the passphrase-protected private key (/home/ operator1/.ssh/key2) of the SSH key pair to the shell session.
- Execute the
hostnamecommand on servera remotely without accessing a remote interactive shell. Use /home/operator1/.ssh/key2 as the identity file
Notice that the preceding ssh command did not prompt you to enter the passphrase interactively. - Open another terminal on
CMDand open an SSH session toserverbasstudent - On
serverb, use the su command to switch tooperator1and invoke an SSH connection toservera. Use/home/operator1/.ssh/key2as the identity file to authenticate using the SSH keys.
Notice that the preceding[operator1@serverb ~]$ ssh -i .ssh/key2 operator1@servera Enter passphrase for key '.ssh/key2': redhatpass Last login: Wed Jul 24 10:02:16 2024 from 172.16.70.23 [operator1@servera ~]$ssh commandprompted you to enter the passphrase interactively because you did not invoke the SSH connection from the shell that you used to startssh-agent. - Exit all the shells you are using in the second terminal.