Ssh examples in linux

SSH Tutorial With Command Examples

SSH is one of the most popular tools in the Linux and Unix world. SSH or Secure Shell as its name suggests creates secure shell connections to the remote systems. Most of the Linux system administrators prefer SSH to manage remotely. SSH creates encrypted channels to the remote system and transmits data through these secure channels.

Syntax

Basic ssh syntax is like below.

  • `OPTIONS` is used to specify ssh command options which can change auth type, compression, etc. most of them explained in this tutorial
  • `USERNAME` is the user name we want to use to authenticate on the remote system or host. USERNAME is optional where if it is not specified current user name is used.
  • `HOSTNAME` is the hostname or IP address of the remote system or host we want to connect.
  • `COMMAND` is optional where we can run command on the remote system without getting an interactive shell.

Connect To Remote

Simple usage of ssh is just providing hostname or IP address of remote system and connect. The user name is supplied from current user. Simply current users username is used for remote system.

Connect To Remote

Specify Username Explicitly

In the previous example username is supplied by the session. What is we need to use different username for the remote connection? Adding username to the remote system IP address or hostname with @ sign like below can solve this.

Specify Username

We can provide the username with -l parameter like below.

Specify Username

Specify Port Number

Ssh protocol uses TCP port 22 by default. Ssh clients also assumes remote system uses default port number. In some cases the port number can be different than 22 so the remote port number should be provided to the ssh explicitly with -p parameter like below.

Specify Port Number

Create Public-Private Keys/Certificates With SSH Keygen

SSH is very advanced and feature-full protocol. SSH provides different protocols for authentication. The default one is password-based authentication as we previously did. Key-based or certificate-based authentication is more secure than password-based authentication. To use key-based security public and private keys should be created for the user.

During key generation process some questions are asked. Here are steps.

  • By default, keys are stored in the users home directory. The exact path is /home/ismail/.ssh/ and keys are named id_rsa.pub, id_rsa . If we want to change default values we can provide them if not just press enter and skip.
  • Keys can be protected by encrypting them. Especially private key named id_rsa is important. If we want to encrypt then enter the password, if not just press enter and skip.
Читайте также:  Malware anti malware and windows 10

Key Based Authentication

We have previously created our keys. We can use these keys to automatically authenticate without providing any password. First, we should deploy our public key to the remote system. Deployment is easy because of SSH tool named ssh-copy-id . Remote username and host provided like below. Our public key is deployed to the remote users authorized keys database. By default, our public key is /home/ismail/.ssh/id_rsa.pub

Key Based Authentication

Explicitly Specify Keys

SSH behaivour is by default using keys /home/ismail/.ssh/id_rsa.pub and /home/ismail/.ssh/id_rsa but this is not practical some times. We can provide keys explicitly with the -i parameter like below.

Explicitly Specify Keys

Run Command Remotely

To run single line command on the remote hosts opening new shell and typing command may become hard work for us. SSH provides the ability to run commands remotely without opening an interactive bash shell. This will run command ip address show on the remote system.

Run Command Remotely

Explicit Configuration

SSH configuration generally resides on the /etc/ssh/ssh_config or

/.ssh/config . Alternative configuration can be specified with -F parameter like below.

Explicit Configuration

Port Forwarding

Port forwarding is useful feature provided by SSH. The general definition of port forwarding is tunneling local or remote system ports each other. For example I want to connect google.com from local system through a remote system. Port forwarding should be enabled while connecting to the remote system.

Local Port Forwarding

In local port forwarding local port will be forwarded to the remote system and then to the destination system host and port.

After we have connected to the remote system local port number 2222 is opened in the local system. We can check the local port 2222 with the following command.

Local Port Forwarding

Now if we try to connect local port 2222 this port will be forwarded to the host poftut.com and port 2222

Remote Port Forwarding

Remote port forwarding is the reverse of the local pot forwarding. Hostname provided for the forwarding will be tunneled from remote system through our local system.

Remote Port Forwarding

In the remote system with IP address 192.168.122.22 port 5900 is opened and this port is forwarded to the local systems port 5900.

Dynamic Port Forwarding

Dynamic port forwarding will use SOCKS which default port number is 1080. But another port number can be used. SOCKS generally used to proxy browsers like Chrome, Firefox, Opera. Proxy traffic will be forwarded to the remote system.

Dynamic Port Forwarding

X11 Forwarding

Linux systems use for GUI X11 server. One of the best feature of SSH is a remote application with GUI can be run on the local system. Application actually runs on a remote system but GUI or X11 protocol is forwarded to the local system and shown like a local application. To enable forwarding provide -X parameter.

X11 Forwarding

Just with a single shot

Compression

SSH can save from bandwidth and network usage by compressing its network traffic. Compression can provide benefits is the data is high compression rate like text. Compression can be enabled per session with -C parameter.

Compression

Specify Source Address

Some local systems may have multiple interfaces and/or multiple IP addresses. This can be a problem if remote system have IP address-based network restrictions. Setting a specific source IP address can overcome this problem.

Читайте также:  Комбинация вызова диспетчера задач linux

Specify Source Address

Log File

SSH client can produce logs. Normally these logs will be printed out to the console. But they can be written to a file with -E option.

Verbose Mode and Debug

Some times we can have trouble to connect remote system with SSH. In this situations the best solution is to debug connection steps. SSH client can provide verbosely and debug output with -v . Adding more like -vvv will increase debug level and output.

Verbose Mode and Debug

Version

To display the SSH client version and exit use option -V

Version

Источник

SSH Command — Usage, Options, Configuration

Practically every Unix and Linux system includes the ssh command. This command is used to start the SSH client program that enables secure connection to the SSH server on a remote machine. The ssh command is used from logging into the remote machine, transferring files between the two machines, and for executing commands on the remote machine.

Contents

SSH Command in Linux

The ssh command provides a secure encrypted connection between two hosts over an insecure network. This connection can also be used for terminal access, file transfers, and for tunneling other applications. Graphical X11 applications can also be run securely over SSH from a remote location.

Other SSH Commands

There are other SSH commands besides the client ssh . Each has its own page.

ssh-keygen — creates a key pair for public key authentication

ssh-copy-id — configures a public key as authorized on a server

ssh-agent — agent to hold private key for single sign-on

ssh-add — tool to add a key to the agent

scp — file transfer client with RCP-like command interface

sftp — file transfer client with FTP-like command interface

Using the Linux client

Linux typically uses the OpenSSH client. The ssh command to log into a remote machine is very simple. To log in to a remote computer called sample.ssh.com, type the following command at a shell prompt:

If this is the first time you use ssh to connect to this remote machine, you will see a message like:

Type yes to continue. This will add the server to your list of known hosts (

/.ssh/known_hosts ) as seen in the following message:

Each server has a host key , and the above question related to verifying and saving the host key, so that next time you connect to the server, it can verify that it actually is the same server.

Once the server connection has been established, the user is authenticated. Typically, it asks for a password. For some servers, you may be required to type in a one-time password generated by a special hardware token.

Once authentication has been accepted, you will be at the shell prompt for the remote machine.

Specifying a different user name

It is also possible to use a different username at the remote machine by entering the command as:

The above can also be expressed with the syntax:

Executing remote commands on the server

The ssh command is often also used to remotely execute commands on the remote machine without logging in to a shell prompt. The syntax for this is:

Читайте также:  Драйвер не поддерживается этой версией windows как установить

For example, to execute the command:

on host sample.ssh.com, type the following command at a shell prompt:

After authenticating to the remote server, the contents of the remote directory will be displayed, and you will return to your local shell prompt. -x Disables X11 forwarding.

SSH client configuration file

The ssh command reads its configuration from the SSH client configuration file

/.ssh/config . For more information, see the page on SSH client configuration file .

Configuring public key authentication

To configure passwordless public key authentication , you may want to create an SSH key and set up an authorized_keys file. See the pages on ssh-keygen and ssh-copy-id for more information.

Configuring port forwarding

Command-line options can be used to set up port forwarding. Local fowarding means that a local port (at the client computer) is tunneled to an IP address and port from the server. Remote forwarding means that a remote port (at the server computer) is forwarded to a given IP address and port from the client machine. See the page on configuring port forwarding on how to configure them.

OpenSSH also supports forwarding Unix domain sockets and IP packets from a tunnel device to establish a VPN (Virtual Private Network).

SSH command line options

Some of the most important command-line options for the OpenSSH client are:

-1 Use protocol version 1 only.

-2 Use protocol version 2 only.

-4 Use IPv4 addresses only.

-6 Use IPv6 addresses only.

-A Enable forwarding of the authentication agent connection.

-a Disable forwarding of the authentication agent connection.

-C Use data compression

-c cipher_spec Selects the cipher specification for encrypting the session.

-D [bind_address:] port Dynamic application-level port forwarding. This allocates a socket to listen to port on the local side. When a connection is made to this port, the connection is forwarded over the secure channel, and the application protocol is then used to determine where to connect to from the remote machine.

-E log_file Append debug logs to log_file instead of standard error.

-F configfile Specifies a per-user configuration file. The default for the per-user configuration file is

-g Allows remote hosts to connect to local forwarded ports.

-i identity_file A file from which the identity key (private key) for public key authentication is read.

-J [user@] host [:port] Connect to the target host by first making a ssh connection to the pjump host[(/iam/jump-host) and then establishing a TCP forwarding to the ultimate destination from there.

-l login_name Specifies the user to log in as on the remote machine.

-p port Port to connect to on the remote host.

-V Display the version number.

-X Enables X11 forwarding.

A little history

SSH replaced several older commands and protocols in Unix and Linux the 1990s. The include telnet , rlogin , and rsh .

SSH runs at TCP/IP port 22. This is right between ftp and telnet, which are 20 years older. Read the story of how SSH got port 22 .

The following video summarizes how and why SSH was originally developed.

Together with our customers, our mission is to secure their digital business on on-premises, cloud, and hybrid ecosystems cost-efficiently, at scale, and without disruptions to their operations or business continuity.

Источник

Оцените статью