Tag Archives: linux

SSH: How to Connect to Remote Server in Ubuntu

What Is SSH?

One essential tool to master as a system administrator is SSH.

SSH, or Secure Shell, is a protocol used to securely log onto remote systems. It is the most common way to access remote Linux and Unix-like servers.

Here, I shall discuss how to use SSH to connect to a remote system.

Basic Syntax

The tool on Linux for connecting to a remote system using SSH is called, unsurprisingly, ssh.

The most basic form of the command is:

<br />
ssh remote_host<br />

The remote_host in the example is the IP address or domain name that you are trying to connect to.

This command assumes that your username on the remote system is the same as your username on your local system.

If your username is different on the remote system, you can specify it by using this syntax:

<br />
ssh remote_username@remote_host<br />

Once you have connected to the server, you will probably be asked to verify your identity by providing a password.

Later, I shall cover how to generate keys to use instead of passwords.

To exit back into your local session, simply type:

<br />
exit<br />

How Does SSH Work?

SSH works by connecting a client program to an ssh server.

In the above commands, ssh is the client program. The ssh server is already running on the remote_host that we specified.

On Ubuntu, you can start the ssh server on the Droplet by typing:

<br />
sudo service ssh start<br />

On Ubuntu 16.04 and Debian Jessie, you can use systemctl, the systemd command for managing services:

<br />
sudo systemctl start ssh<br />

That should start the sshd server and you can then log in remotely.

How To Configure SSH

When you change the configuration of SSH, you are changing the settings of the sshd server.

In Ubuntu, the main sshd configuration file is located at /etc/ssh/sshd_config.

Back up the current version of this file before editing:

<br />
sudo cp /etc/ssh/sshd_config{,.bak}<br />

Open it with a text editor:

<br />
sudo nano /etc/ssh/sshd_config<br />

You will want to leave most of the options in this file alone. However, there are a few you may want to take a look at:


<br />
Port 22<br />

The port declaration specifies which port the sshd server will listen on for connections. By default, this is 22. You should probably leave this setting alone, unless you have specific reasons to do otherwise. If you do change your port, I shall show you how to connect to the new port later on.

host keys declarations


<br />
HostKey /etc/ssh/ssh_host_rsa_key<br />
HostKey /etc/ssh/ssh_host_dsa_key<br />
HostKey /etc/ssh/ssh_host_ecdsa_key<br />

The host keys declarations specify where to look for global host keys. We will discuss what a host key is later.

level of logging


<br />
SyslogFacility AUTH<br />
LogLevel INFO<br />

These two items indicate the level of logging that should occur.

If you are having difficulties with SSH, increasing the amount of logging may be a good way to discover what the issue is.

login configuration


<br />
LoginGraceTime 120<br />
PermitRootLogin yes<br />
StrictModes yes<br />

These parameters specify some of the login information.

LoginGraceTime specifies how many seconds to keep the connection alive without successfully logging in.

It may be a good idea to set this time just a little bit higher than the amount of time it takes you to log in normally.

PermitRootLogin selects whether root is allowed to log in.

In most cases, this should be changed to “no” when you have created user account that has access to elevated privileges (through su or sudo) and can log in through ssh.

strictModes is a safety guard that will refuse a login attempt if the authentication files are readable by everyone.

This prevents login attempts when the configuration files are not secure.

X11 Forwarding


<br />
X11Forwarding yes<br />
X11DisplayOffset 10<br />

These parameters configure an ability called X11 Forwarding. This allows you to view a remote system’s graphical user interface (GUI) on the local system.

This option must be enabled on the server and given with the SSH client during connection with the -Xoption.

Restart sshd server

If you changed any settings in /etc/ssh/sshd_config, make sure you restart your sshd server to implement your modifications:

<br />
sudo service ssh restart<br />

Or, on systemd systems such as Ubuntu 16.04 or Debian Jessie:

<br />
sudo systemctl restart ssh<br />

You should thoroughly test your changes to ensure that they operate in the way you expect.

It may be a good idea to have a few sessions active when you are making changes. This will allow you to revert the configuration if necessary.

If you run into problems, remember that you can log in through the Console link on your Droplet page.

How To Log Into SSH with Keys

While it is helpful to be able to log in to a remote system using passwords, it’s a much better idea to set up key-based authentication.

How Does Key-based Authentication Work?

Key-based authentication works by creating a pair of keys: a private key and a public key.

The private key is located on the client machine and is secured and kept secret.

The public key can be given to anyone or placed on any server you wish to access.

When you attempt to connect using a key-pair, the server will use the public key to create a message for the client computer that can only be read with the private key.

The client computer then sends the appropriate response back to the server and the server will know that the client is legitimate.

This entire process is done in the background automatically after you set up keys.

How To Create SSH Keys

SSH keys should be generated on the computer you wish to log in from. This is usually your local computer.

Enter the following into the command line:

<br />
ssh-keygen -t rsa<br />

Press enter to accept the defaults. Your keys will be created at ~/.ssh/id_rsa.pub and ~/.ssh/id_rsa.

Change into the .ssh directory by typing:

<br />
cd ~/.ssh<br />

Look at the permissions of the files:

<br />
ls -l<br />


<br />
-rw-r--r-- 1 demo demo  807 Sep  9 22:15 authorized_keys<br />
-rw------- 1 demo demo 1679 Sep  9 23:13 id_rsa<br />
-rw-r--r-- 1 demo demo  396 Sep  9 23:13 id_rsa.pub<br />

As you can see, the id_rsa file is readable and writable only to the owner. This is how it should be to keep it secret.

The id_rsa.pub file, however, can be shared and has permissions appropriate for this activity.

How To Transfer Your Public Key to the Server

You can copy the public key to the remote server by issuing this command:

<br />
ssh-copy-id remote_host<br />

This will start an SSH session, which you will need to authenticate with your password.

After you enter your password, it will copy your public key to the server’s authorized keys file, which will allow you to log in without the password next time.

Client-Side Options

There are a number of optional flags that you can select when connecting through SSH.

Some of these may be necessary to match the settings in the remote host’s sshd configuration.

For instance, you if you changed the port number in your sshd configuration, you will need to match that port on the client-side by typing:

<br />
ssh -p port_number remote_host<br />

If you only wish to execute a single command on a remote system, you can specify it after the host like so:

<br />
ssh remote_host command_to_run<br />

You will connect to the remote machine, authenticate, and the command will be executed.

As we said before, if X11 forwarding is enabled on both computers, you can access that functionality by typing:

<br />
ssh -X remote_host<br />

Providing you have the appropriate tools on your computer, GUI programs that you use on the remote system will now open their window on your local system.

'Coz sharing is caring

How to find Files that are Modified today in Unix / Linux

To find all files that was modified since a specific time ago (i.e an hour ago, a day ago, 24 hours ago, a weeks ago, a month ago and so on) in Unix and Linux environment, the find command will come in handy. The command syntax is:

To find all files modified in the last 24 hours (last full day) in current directory and its sub-directories:

<br />
find . -mtime -1 -print<br />

Flag -mtime -1 option tells find command to look for files modified in the last day (24 hours). Flag -print option will cause find command to print the files’ location. -print can be replaced with -ls if you want a directory-listing-type response.

To find all files modified in the last 24 hours (last full day) in a particular specific directory and its sub-directories:

<br />
find /directory_path -mtime -1 -print<br />

The command is basically the same with the earlier command, just that now you no need to cd (change directory) to the directory you want to search.

To find all files with regular file types only, and modified in the last 24 hours (last full day) in current directory and its sub-directories:

<br />
find /directory_path -type f -mtime -1 -print<br />

Find files Modified 4 days ago on linux

To find all files that are modified today only (since start of day only, i.e. 12 am), in current directory and its sub-directories:

<br />
touch -t `date +%m%d0000` /tmp/$$<br />
find /tmefndr/oravl01 -type f -newer /tmp/$$<br />
rm /tmp/$$<br />

The first command can be modified to specify other date and time, so that the commands will return all files that have changed since that particular date and time.

'Coz sharing is caring