0% found this document useful (0 votes)
41 views9 pages

SSH Remote Access and Key Management

The document provides an overview of using Secure Shell (SSH) for remote access to Linux systems, detailing its secure authentication methods, including password and key-based authentication. It explains how to establish SSH connections, execute commands remotely, and manage SSH keys for secure logins. Additionally, it covers configuring SSH settings, using SCP for file transfers, and includes practical examples for users to follow.

Uploaded by

AlthaS SajeeB
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views9 pages

SSH Remote Access and Key Management

The document provides an overview of using Secure Shell (SSH) for remote access to Linux systems, detailing its secure authentication methods, including password and key-based authentication. It explains how to establish SSH connections, execute commands remotely, and manage SSH keys for secure logins. Additionally, it covers configuring SSH settings, using SCP for file transfers, and includes practical examples for users to follow.

Uploaded by

AlthaS SajeeB
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SSH (Secure Shell) 22/TCP

Linux users and administrators often need to get shell access to a


remote system by connecting to it over the network. In a modern
computing environment, many headless servers are actually virtual
machines or are running as public or private cloud instances. These systems
are not physical and do not have real hardware consoles. They might not
even provide access to their (simulated) physical console or serial console.

In Linux, the most common way to get a shell prompt on a remote system
is to use Secure Shell (SSH). Most Linux systems (including Red Hat
Enterprise Linux) and macOS provide the OpenSSH command-line program
ssh for this purpose.

In this example, a user with a shell prompt on the machine host uses ssh to
log in to the remote Linux system remotehost as the user remoteuser:
[user@host ~]$ ssh remoteuser@remotehost
remoteuser@remotehost's password: password
[remoteuser@remotehost ~]$

SSH is a secure remote login networking protocol which facilitates virtual


terminal connection. By default ssh is a TCP protocol service and, default
port number is 22. Telnet is also a remote login TCP protocol service but
telnet is not a secure service.

The ssh command encrypts the connection to secure the communication


against eavesdropping or hijacking of the passwords and content.
SSH uses RSA (asymmetric algorithm) for data encryption, and Diffie-
Hellman key exchange algorithm which is the one-way function. It is not for
encryption.

Some systems (such as new cloud instances) do not allow users to use a
password to log in with ssh for tighter security. An alternative way to
authenticate to a remote machine without entering a password is through
public key authentication.

With this authentication method, users have a special identity file


containing a private key, which is equivalent to a password, and which they
keep secret. Their account on the server is configured with a matching
public key, which does not have to be secret. When logging in, users can
configure ssh to provide the private key and if their matching public key is
installed in that account on that remote server, it will log them in without
asking for a password.
SSH authentication files

➢ The private key is used as authentication credentials and it resides in


"~/.ssh/id-rsa" file.

➢ The public key is used to verify the private key and it resides in
"~/.ssh/[Link]"

➢ The “~/.ssh/known_hosts” file lets the client authenticate the server,


to check that it isn't connecting to a fake server.

Logging Out
When you are finished using the shell and want to quit, you can choose one
of several ways to end the session. You can enter the exit command to
terminate the current shell session. Alternatively, finish a session by
pressing Ctrl+D.

The following is an example of a user logging out of an SSH session:


[remoteuser@remotehost ~]$ exit (or Ctrl + D)
logout
Connection to remotehost closed.
[user@host ~]$
[Link] [Link]
[Link]/24 [Link]/24

1. From servera, open an SSH session to serverb.

1.1. remote ssh login through hostname

[root@servera ~] # ssh [Link]


root@[Link]’s password: password
[root@serverb ~] # hostname
[Link]
[root@serverb ~] # exit
(OR)

1.2. remote ssh login through IP address

[root@servera ~] # ssh [Link]


root@[Link]’s password: password
[root@serverb ~] # exit

2. From servera, open an SSH session to serverb as student.

[root@servera ~] # ssh student@[Link]


student@[Link]’s password: password
[student@serverb ~] $ id
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[student@serverb ~] $ exit
3. Run a Single command in remote host.

3.1. Execute the hostname command on serverb remotely using


SSH without accessing the remote interactive shell.

[root@servera ~] # ssh [Link] hostname


student@[Link]’s password: password
[Link]
[root@servera ~] #

3.2. Execut the following command on serverb remotely using SSH


without accessing the remote interactive shell.

[root@servera ~] # ssh [Link] cat /etc/os-release


[root@servera ~] # ssh [Link] useradd redhat
[root@servera ~] # ssh [Link] passwd redhat

[root@servera ~] # ssh student@[Link] id


[root@servera ~] # ssh student@[Link] lsblk

[root@servera ~] # ssh [Link] mkdir ~/Desktop/dir{1..5}


[root@servera ~] # ssh student@[Link] uname -n ; id
SSH Key-based Authentication
You can configure an SSH server to allow you to authenticate without a password by
using key- based authentication. This is based on a private-public key scheme.

To do this, you generate a matched pair of cryptographic key files. One is a private key,
the other a matching public key. The private key file is used as the authentication
credential and, like a password, must be kept secret and secure. The public key is
copied to systems the user wants to connect to, and is used to verify the private key.
The public key does not need to be secret.

You put a copy of the public key in your account on the server. When you try to log in,
the SSH server can use the public key to issue a challenge that can only be correctly
answered by using the private key. As a result, your ssh client can automatically
authenticate your login to the server with your unique copy of the private key. This
allows you to securely access systems in a way that doesn't require you to enter a
password interactively every time.

Generating SSH Keys

To create a private key and matching public key for authentication, use the ssh-keygen
command. By default, your private and public keys are saved in your ~/.ssh/id_rsa and
~/.ssh/id_rsa.pub files, respectively.

Sharing the Public Key

Before key-based authentication can be used, the public key needs to be copied to the
destination system. The ssh-copy-id command copies the public key of the SSH keypair
to the destination system. If you omit the path to the public key file while running ssh-
copy-id, it uses the default /home/user/.ssh/id_rsa.pub file.

After the public key is successfully transferred to a remote system, you can
authenticate to the remote system using the corresponding private key while logging
in to the remote system over SSH. If you omit the path to the private key file while
running the ssh command, it uses the default /home/user/.ssh/id_rsa file.
Lab Exercise

1. From servera, open an SSH session to serverb as root.

[root@servera ~]# ssh root@[Link]

2. Use the su command to switch to the student user on serverb.

[root@serverb ~]# su - student


Password: *****
[student@serverb ~]$

3. Use the ssh-keygen command to generate SSH keys.

[student@serverb ~]$ ssh-keygen


Enter file in which to save the key (/home/operator1/.ssh/id_rsa): Enter
Enter passphrase (empty for no passphrase): Enter
Enter same passphrase again: Enter

4. Use the ssh-copy-id command to send the public key of the SSH
key pair to student on servera.

[student@serverb ~]$ ssh-copy-id student@servera


Are you sure you want to continue connecting (yes/no)? Yes
student@servera's password: *****

5. Execute the hostnamectl command on servera remotely using


SSH without accessing the remote interactive shell.

[student@serverb ~]$ ssh student@[Link] hostnamectl


To change the default port (22)
[root@servera ~] # vim /etc/ssh/sshd_config
#Port 22
Port 2222
#AddressFamily any
Esc:wq!

[root@servera ~]# systemctl restart [Link]


[root@servera ~]# systemctl status [Link]

[root@servera ~]# semanage port -l | grep ssh


[root@servera ~]# semanage port -a -t ssh_port_t -p tcp 2222

[root@servera ~]# firewall-cmd --permanent --add-port=2222/tcp


[root@servera ~]# firewall-cmd --reload

[root@servera ~]# systemctl restart [Link]


[root@servera ~]# systemctl status [Link]

[root@servera ~]# ssh [Link] (connection refused)


[root@servera ~]# ssh -p 2222 [Link]

To disable Root login


[root@servera ~] # vim /etc/ssh/sshd_config
PermitRootLogin no
:wq
[root@servera ~] # systemctl restart sshd

To disable password authentication


[root@servera ~] # vim /etc/ssh/sshd_config
PasswordAuthentication no
:wq
[root@servera ~]# systemctl restart sshd
SCP (Secure Copy)
SCP copies files between hosts on a network. It uses ssh for data transfer,
and uses the same authentication and provides the same security as ssh.
SCP will ask for passwords if they are needed for authentication.

servera serverb serverc


[Link]/24 [Link]/24 [Link]/24

1. From servera, copy files from serverb to serverc as the student user.
[root@servera ~]# scp student@serverb:~/Documents/*.txt student@serverc:~/

2. From servera, copy a directory from serverc to serverb as the student


user.
[root@servera ~]# scp -r student@serverc:/soften student@serverb:~/Desktop

eg:
[root@serverb ~]# scp serverb:~/Pictures/* student@serverc:~/Pictures/
[root@serverb ~]# scp serverc:~/Desktop/* . ( “.” --> present location)

[root@serverc ~]# scp [Link]:~/[Link] [Link]:~/Desktop


[root@serverc ~]# scp -r test_dir [Link]:/mnt/

Common questions

Powered by AI

You can remotely execute a command on a Linux server using SSH by appending the desired command to the SSH command line. For example, to execute the 'hostname' command, you would use 'ssh user@remote_system_hostname hostname'. This executes the command remotely without opening an interactive shell, leveraging SSH for secure remote execution .

SSH tunneling provides a method to securely tunnel network traffic through an SSH connection, allowing for the forwarding of multiple protocols or services securely. It is especially useful for creating secure VPNs or accessing restricted services. SCP, meanwhile, is specifically used for secure file transfer between hosts. While SSH tunneling offers flexibility in securing various types of data traffic, SCP is optimized for secure file copying with ease, providing built-in authentication and encryption of both the command and data .

To change the default SSH port, edit the '/etc/ssh/sshd_config' file to specify a new port number, then restart the SSH service. Additionally, to disable root login, the same configuration file should be modified with 'PermitRootLogin no', followed by another SSH service restart. Both changes should be supported by updating firewall settings and verifying changes with 'firewall-cmd' and service management commands .

To generate SSH keys, use the 'ssh-keygen' command which creates a private-public key pair, typically saved in '~/.ssh/id_rsa' and '~/.ssh/id_rsa.pub' files, respectively. The public key must then be copied to the remote server using the 'ssh-copy-id' command. Once the public key is on the server, you can log in using the 'ssh' command, which will use the private key for authentication by default .

SSH public key authentication enhances security as it uses a matched pair of cryptographic keys: a private key kept secret and a public key that doesn't need to be kept secret. The public key is installed on the remote server, allowing the server to issue a challenge that can only be correctly answered with the private key. This removes the need for transmitting passwords over the network, reducing the risk of interception .

SSH, or Secure Shell, provides encrypted connections which secure the communication against eavesdropping or hijacking of the passwords and content. It uses RSA, an asymmetric algorithm for data encryption, and the Diffie-Hellman key exchange algorithm. Telnet, on the other hand, is not a secure service as it transmits data, including passwords, in plaintext, making it vulnerable to interception .

If the SSH private key is compromised, attackers can gain unauthorized access to any servers where the corresponding public key is installed, bypassing password protection. To mitigate this risk, private keys should be protected with strong passphrases, stored securely with file permissions set to limit access, and regular key rotations should be performed. Additionally, using hardware security modules (HSMs) or a secure key management system can enhance security .

Allowing password-based SSH logins can make a system vulnerable to brute force attacks, as passwords can potentially be intercepted or guessed. These vulnerabilities can be mitigated by using key-based authentication, disabling password authentication entirely through the SSH configuration, employing firewalls and fail2ban to limit login attempts, and ensuring strong, complex passwords are used if unavoidable .

To transfer a directory from one remote host to another using SCP, the command 'scp -r source_directory user@destination_host:destination_path' is used. SCP leverages SSH's authentication and encryption features to securely transfer data, ensuring that files are copied over the network while maintaining confidentiality and integrity, as it encrypts both the authentication credentials and data in transit using SSH protocols .

The known_hosts file in SSH plays a crucial role in authenticating the SSH server to the client. It contains a list of known public host keys, allowing the client to verify the authenticity of the server being connected to, helping prevent man-in-the-middle attacks. When connecting to a server, if its host key is not found in known_hosts, the client prompts for user confirmation, thereby making the connection more secure against spoofing attempts .

You might also like