Daily Archives: 23 April 2018


Ubuntu: install / start/stop enable/disable ssh server 2

OpenSSH is a freely available version of the Secure Shell (SSH) protocol family of tools for remotely controlling, or transferring files between, computers.

Install SSH server

To install the openssh-server on an Ubuntu, you need execute the following command as root or using sudo:

apt-get install openssh-server -y;

Disable SSH server

To disable the ssh service, execute the following command as root or using sudo:

systemctl disable ssh;

Enable SSH server

To enable the ssh service, execute the following command as root or using sudo:

systemctl enable ssh;

Stop SSH server

To stop (or deactivate) the ssh service, execute the following command as root or using sudo:

systemctl stop ssh;

Start SSH server

To start (or activate) the ssh service, execute the following command as root or using sudo:

systemctl start ssh;

Status of SSH server

To check the status of the ssh service, execute the following command as root or using sudo:

systemctl status ssh;

CONCEPTS

In a nutshell:

  • enabled is a service that is configured to start when the system boots
  • disabled is a service that is configured to not start when the system boots
  • active is a service that is currently running
  • inactive is a service that is currently stopped and may be disabled, but it can be started and become active

In much more detail:

systemd provides a dependency system between various entities called “units” of 12 different types. Units encapsulate various objects that are relevant for system boot-up and maintenance. The majority of units are configured in unit configuration files, whose syntax and basic set of options is described in systemd.unit(5), however some are created automatically from other configuration, dynamically from system state or programmatically at runtime. Units may be “active” (meaning started, bound, plugged in, …, depending on the unit type, see below), or “inactive” (meaning stopped, unbound, unplugged, …), as well as in the process of being activated or deactivated, i.e. between the two states (these states are called “activating”, “deactivating”). A special “failed” state is available as well, which is very similar to “inactive” and is entered when the service failed in some way (process returned error code on exit, or crashed, or an operation timed out). If this state is entered, the cause will be logged, for later reference. Note that the various unit types may have a number of additional substates, which are mapped to the five generalized unit states described here.
— From man systemd

 


Ubuntu SSHD listen to multiple ports

Recently, we’ve setup an Ubuntu server behind CloudFlare that needed to listen for SSH connections.
Unfortunately, CloudFlare does not allow connections to the default SSH port which is 22.
So, to achieve what it was needed we either had to change the port that the SSH service was listening to or add an additional port.
We decided to go with the option of listening to multiple ports for SSH connections, this way users that were also behind the CloudFlare CDN could still continue to use their SSH clients without being forced to define the connection port manually.

The port listening setting is available in /etc/ssh/sshd_config, using sudo we edited the file with a text editor and searched for the following lines:

# What ports, IPs and protocols we listen for
Port 22

Right after the line that contains Port 22, we added another line for the new port (to see the list of all available open ports on CloudFlare, check this post)

And the file became as follows:

# What ports, IPs and protocols we listen for
Port 22
Port 2053

Afterwards, we restarted the SSHD service to apply the changes by executing the following command by using sudo:


systemctl restart ssh;


How we create bootable GNU/Linux USB flash drives from terminal

A very important tool in our everyday life are the LiveUSB GNU/Linux flash drives.
We keep an updated collection of several GNU/Linux flavors/distributions (Fedora, CentOS, (L/X)Ubuntu, Kali etc.) that are used depending on the scenario.

The command we use is the following:


sudo dd bs=4M if=path/to/OS.iso of=/dev/sdX conv=fdatasync;

dd allows you to convert and copy a file and we use it to copy the ISO file of the operating system onto the USB flash drive.

Notes:

  1. You need to unmount the USB flash drive before formatting it, e.g.:
    sudo umount /dev/sdXY;
  2. You need to use the device filename and not a partition filename:
    e.g. You need to use /dev/sdX and NOT /dev/sdX1
  3. You need to use either the root account or execute the command with sudo
  4. If you do not know the filename associated with your flash drive, use an application like the following ones to determine which /dev file is mapped to the USB flash drive:
    gnome-disks; or
    lsblk; or
    sudo fdisk -l;

The parameters we use are the following:

  • bs=SIZE_IN_BYTES defines up to how many bytes should be read and written at a time.
    In our case we used 4 Megabytes (4M).
  • if=INPUT_FILE defines the file to be read, we use this parameter to point to the OS ISO file that we want to write on the USB drive.
  • of=OUTPUT_FILE defines the filename where the data is to be written in.
    In GNU/Linux, devices are accessible like files as well so we used /dev/sdX here that happened to be the device file assigned to our USB device.
  • conv=CONVS converts the file as per the comma separated symbol list
    fdatasync physically writes output file data before finishing, we use this parameter to be sure that all I/O operations are done well before dd terminates, this way we are certain that our USB device will be ready to use as soon as the application is done.

How we sync files between two drives

The rsync command is a powerful tool for file synchronization and transfer in Linux and Unix-like operating systems. It provides a robust and efficient way to copy, backup, and mirror files and directories both locally and remotely. In this article, we will explore the technical details of the following rsync command:

rsync -avh --delete --progress "path/to/source/" "path/to/destination/";

We will explain each option and argument used in the command and their respective functionality.

Options and Arguments

The rsync command is a versatile tool with many options and arguments to customize its behavior. The options used in the above command are:

  • “-a”: This option enables the archive mode, which is a shorthand for several options such as -rlptgoD, -l, -p, -t, -g, -o, and -D. It ensures that rsync preserves file permissions, ownership, timestamps, and symbolic links during the synchronization process.
  • “-v”: This option enables verbose mode, which displays detailed output of the rsync operation, including the transferred files and their sizes.
  • “-h”: This option enables human-readable mode, which displays file sizes in a more readable format, such as “1K” for 1 kilobyte, “1M” for 1 megabyte, etc.
  • “–delete”: This option tells rsync to delete any files at the destination that do not exist at the source. This ensures that the destination is an exact copy of the source.
  • “–progress”: This option displays real-time progress information during the rsync operation, including the percentage of completion and the estimated time remaining.

The arguments used in the command are:

  • “path/to/source/”: This argument specifies the source directory or file that we want to sync. It can be a local path or a remote path using the ssh protocol.
  • “path/to/destination/”: This argument specifies the destination directory or file where we want to copy the source files. It can also be a local or remote path using the ssh protocol.

Explanation

The rsync command is a powerful tool that synchronizes the source and destination directories or files. The -a option enables the archive mode, which ensures that the file metadata is preserved during the transfer, including permissions, ownership, timestamps, and symbolic links. The -v option enables the verbose mode, which provides detailed output of the rsync operation, including the transferred files and their sizes.

The -h option enables the human-readable mode, which displays file sizes in a more readable format. The –delete option tells rsync to delete any files at the destination that do not exist at the source, which ensures that the destination is an exact copy of the source. Finally, the –progress option displays real-time progress information during the rsync operation, including the percentage of completion and the estimated time remaining.

Conclusion

The rsync command is a powerful tool for file synchronization and transfer in Linux and Unix-like operating systems. The command discussed in this article synchronizes the source and destination directories or files, preserves file metadata, displays detailed output, deletes any files that do not exist at the destination, and displays real-time progress information. With the proper use of rsync options and arguments, file synchronization and transfer can be made easy, efficient, and reliable.

Other

We have two external hard disks that we use to keep backups of our data.
The way we do that is by using the command rsync that makes our life easy.

Specifically, we use the following command to synchronize the first hard disk with the second one: rsync -avh –delete –progress “path/to/source/” “path/to/destination/”;

rsync is a fast, versatile, remote (and local) file-copying tool, it is available in almost every system (GNU/Linux, Unix (MacOS as well) and Windows).

The parameters we use are the following:

  • -a, --archive enables archive mode which is equal to -rlptgoD (no -H,-A,-X)
    In more detail it enables all of the following options
    -r, --recursive recurse into directories
    -l, --links copy symlinks as symlinks
    -p, --perms preserve permissions
    -t, --times preserve modification times
    -g, --group preserve group
    -o, --owner preserve owner (super-user only)
    -D same as --devices --specials
    --devices preserve device files (super-user only)
    --specials preserve special files
  • -v, --verbose it increases verbosity of the output
  • -h, --human-readable outputs numbers in a human-readable format
  • --delete deletes extraneous files from destination directories
  • --progress shows progress during transfer