Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Linux - Network: SSH

Back


SSH Protocol


openssh Package


Components


Package Commands

Command Description
rpm -qa \| grep openssh Check if the openssh package installed
dnf list installed openssh Check if the openssh package installed
dnf list installed \| grep openssh Check if the openssh package and dependencies
apt list --installed \| grep openssh Check if the openssh package installed
dnf install -y openssh Install openssh package
apt install -y openssh Install openssh package

Service Commands

Command Description
systemctl list-units \| grep sshd.service Check the ssh service status
systemctl list-unit-files \| grep sshd.service Check the ssh service unit file
systemctl list-unit-files \| grep sshd.service Check the ssh service unit file
sudo systemctl status sshd Check the sshd status
sudo systemctl start sshd Start the sshd status
sudo systemctl enable sshd Enable the sshd status at startup

Configuration Files


Common Command

Command Description
ssh user@ip Connect remote instance using username and ip address
ssh user@ip -p portNum SSH connection using custom port number

SSH Configurations

conf01

Common Configuration

Directives Default Desc
Port 22 the port on which SSH listens
ListenAddress 0.0.0.0 local addresses the sshd service should listen on
AuthorizedKeysFile ~/.ssh/authorized_keys the location of the file containing a user’s authorized keys.
PermitRootLogin yes Whether enabling root login
PubkeyAuthentication yes Whether enable users to use key pair to login
PasswordAuthentication yes Whether enable clients to login with a username and password
PermitEmptyPasswords no Whether enablethe use of null passwords.
ChallengeResponseAuthentication yes Whether enable challenge response authentication mechanism
UsePAM yes Whether enable user authentication via PAM.
X11Forwarding No Whether enable remote access to graphical applications
SyslogFacility AUTH Defines the facility code to be used
LogLevel INFO the level of criticality for the messages to be logged.
# Set PermitRootLogin to no to prohibit root from logging in with SSH. Then, elevate a user's privileges after logging in.
PermitRootLogin no

PasswordAuthentication no

Idle Timeout Interval

# sets the timeout interval for an SSH session
# 600 secs = 10 min
ClientAliveInterval 600
# the number of client alive messages that can be sent before the client disconnects and the session is terminated
ClientAliveCountMax 0

Disable root login

# sets the timeout interval for an SSH session
PermitRootLogin no

conf01


Disable Empty Passwords

PermitEmptyPasswords no

Limit Users’ SSH Access

# AllowUsers user1 user2

Using a Custom Port Number

Port 22

Access without Password (SSH-Keys)


Lab: Access with ssh-keys

# Generate the key
ssh-keygen
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/rheladmin/.ssh/id_rsa):
# Enter passphrase (empty for no passphrase):
# Enter same passphrase again:
# Your identification has been saved in /home/rheladmin/.ssh/id_rsa.
# Your public key has been saved in /home/rheladmin/.ssh/id_rsa.pub.
# The key fingerprint is:

# copy the key
ssh-copy-id root@server_ip
# /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/rheladmin/.ssh/id_rsa.pub"
# /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
# root@server_ip's password:

# Number of key(s) added: 1

# Now try logging into the machine, with:   "ssh 'root@server_ip'"
# and check to make sure that only the key(s) you wanted were added.

# login without password
ssh root@server_ip
ssh root@192.168.204.156

Lab: Install openssh on Redhat

# list packages to verify installation
dnf list installed | grep ssh

# if needed to install
sudo dnf install openssh-server

sudo systemctl start sshd   # Start the sshd service
sudo systemctl enable sshd    # configure it to start following a system reboot

# start
sudo systemctl start sshd
# restart deamon after new configuration
sudo systemctl restart sshd
# status
sudo systemctl status sshd

# configure firewall settings
sudo firewall-cmd --permanent --add-service=ssh
# success

# reload the firewall to enable the new settings
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

Lab: Install openssh on Ubuntu

sudo apt-get -y update  # update packages
# update the local package index. It downloads package details from all set sources to refresh the package cache.
# sudo: Superuser Do,
# apt: package manager
# sudo apt: allows a root user to perform operations in the apt repository.
# sudo apt update: downloads package details from all set sources which are commonly listed in the /etc/apt/sources.list file and other files found in the /etc/apt/sources.list.d directory. As a result, apt package cache will be updated ensuring your system has the latest package information.
sudo apt-get -y install openssh-server  # install
sudo systemctl status ssh   # check status
sudo systemctl enable ssh --now   # enable and start the ssh service immediately
ssh localhost   # build connection to localhost using SSH, pwd is required.

curl ifconfig.me
ssh username@ip

Lab: Generate Key

# on the client
# Generate RSA keys without a password (-N) and without detailed output (-q).
ssh-keygen -N "" -q

# confirm
# private key
cat ~/.ssh/id_rsa

# public key
cat ~/.ssh/id_rsa.pub

# copy public key to server
ssh-copy-id 192.168.128.50
# confirm the client know the host
cat ~/.ssh/known_hosts

# connect
ssh 192.168.128.50

# Confirm login attempt on server
tail /var/log/secure

# run server command in the client
ssh 192.168.128.50 hostname
ssh 192.168.128.50 nmcli c

TOP