How do I scp between two remote hosts?

To scp between two remote hosts, you typically need to be logged into one of the hosts and execute the scp command there.

The general format is like this:

scp <user>@<source_host>:<source_file_path> <user>@<destination_host>:<destination_file_path>

Suppose, you are logged into host1, and you want to copy a file from host2 to host3.
First, make sure that the key-based ssh authentication is set up for host2 -> host1 and host1 -> host3. Then, on host1, you can execute:

scp user@host2:/path/to/source/file.txt user@host3:/path/to/destination/

This will copy file.txt from host2 to host3.

Keep in mind this command requires you to have proper SSH access and permissions for both source and destination hosts. If you do not have the necessary authentication set up, the command will ask for the password for each machine.

Set up key-based SSH authentication

To set up key-based SSH authentication, you’ll need to generate a key pair on host1, then copy the public key to host2 and host3. Here’s how you can do it:

  1. Step One — Create the RSA Key Pair on host1:

Open a terminal and run the following command:

ssh-keygen -t rsa

You will be asked to specify the file location and passphrase (optional). If you just press Enter through those prompts, it will create an RSA key pair with default settings.

  1. Step Two — Store the Keys and Passphrase:

When you are prompted to “Enter a file in which to save the key,” you can press Enter. This accepts the default file location.

At the prompt, type a secure passphrase or press enter to proceed without a passphrase.
After completing these steps, your new keys are available in your user home folder ~/.ssh/id_rsa for your private key and ~/.ssh/id_rsa.pub for your public key.

  1. Step Three — Copy the Public Key to host2 and host3:

Next, you’ll copy your public key to your host2 and host3 using the ssh-copy-id command. Like this:

ssh-copy-id user@host2
ssh-copy-id user@host3

Replace user with your username, and host2 or host3 with the IP address or hostname of your second and third machines. You will be prompted for the user password for host2 and host3 to copy the public key.

That’s it! You have set up the key-based ssh authentication. Now you can log into host2 and host3 from host1 without a password:

ssh user@host2

or

ssh user@host3

This method applies to any Linux or Unix system that uses SSH. Please refer to the documentation for Windows servers or any other non-Unix systems. Also note that the user must have ssh and shell access.

Warning: Be careful with your private key (~/.ssh/id_rsa). Don’t share your private key with anyone! In production environments, it’s a common practice to protect private keys with a strong passphrase.

Note: The scp command is not installed by default on some systems. You can install it using your system package manager (like apt, yum, etc.). Alternatively, you can use rsync or sftp depending on the systems and permissions involved.

Important: Remember about data security. Always ensure safe and secure data transfer, especially when dealing with sensitive data. Use encrypted channels for such transfers (which scp does by utilizing SSH). Make sure the user whose credentials are used for the transfer has only the necessary permissions and nothing more.

How do I install Calibri font in Ubuntu?

I need to create a Microsoft Word Mail Merge document in my Java Spring MVC application. But running it in Ubuntu server resulting in a document that missing the default font use in the document, which is the Calibri font. So I need to install the font in Ubuntu to make the document looks as expected.

Here what I need to do to install the font in my Ubuntu box. Starts by updating the repository package list to get latest packages information for upgrades or new package installation.

sudo apt-get update

Then install FontForge in our system. FontForge is a free and open source font editor, but in this case it will help to do the font conversion in the installation script on the upcoming step.

sudo apt-get install fontforge

Install the Microsoft Cabinet file un-packer. This is required for the next script to successfully install the fonts.

sudo apt-get install cabextract

The following script will install Microsoft Vista TrueType Fonts (TTF) in Ubuntu. It includes the following fonts, Calibri, Cambria, Candara, Consolas, Constantia, and Corbel.

wget https://gist.githubusercontent.com/maxwelleite/10774746/raw/ttf-vista-fonts-installer.sh -q -O - | sudo bash

Run the next command to see if the font successfully installed. You will see the Calibri fonts in the result if the fonts successfully installed.

fc-list | grep Calibri

Here are the list of installed Calibri fonts.

/usr/share/fonts/truetype/vista/calibriz.ttf: Calibri:style=Bold Italic
/usr/share/fonts/truetype/vista/calibrii.ttf: Calibri:style=Italic
/usr/share/fonts/truetype/vista/calibrib.ttf: Calibri:style=Bold
/usr/share/fonts/truetype/vista/calibri.ttf: Calibri:style=Regular

How do I clear the current command line in terminal?

Terminal

You have typed a long line of command in terminal. But now you want to clear or delete the entire line. Deleting each character in the command will take sometime and bored you. So are there any keyboard shortcuts that allow you to do this? Yes there are some hotkeys to the rescue.

Hotkeys Description
CTRL + u Delete the current command.
The deleted command will be stored into a buffer.
CTRL + w Delete a word.
CTRL + c Abort what you are typing.
CTRL + d Delete current character.

Other hotkeys that might help you work faster in the terminal.

Hotkeys Description
CTRL + e Move to the end of line.
CTRL + a Move to the start of line.
CTRL + k Cut text from the cursor to the end of line.
CTRL + y Paste the last cut text or buffer.
CTRL + - Undo.
CTRL + b Backward one character.
CTRL + f Forward one character.
ALT + Backward one word.
ALT + Forward one word.

How do I pass password to sudo commands?

If you want to run a sudo command without being prompted to input the password you can do the following command.

echo password | sudo -S rm -rf /opt/jetty/

In the command above we are trying to remove the /opt/jetty directory using the rm -rf command. The -S (stdin) option allow the sudo command to read password from a standard input instead of a terminal device.

If you want to store the password in a file you can use the cat command instead of echo like the following example.

cat password.txt | sudo -S rm -rf /opt/jetty/

How do I install Oracle Java in Ubuntu Server 14.04?

The easiest way to install Oracle Java (JDK) in Ubuntu is to use the WebUpd8 PPA. A PPA (Personal Package Archive) is a special software repository for uploading source packages to be build and published as an APT repository by Launchpad. This PPA will download the required files from Oracle and install JDK7 / JDK8 / JDK9.

The steps:

  • Login to Ubuntu server.
  • sudo apt-add-repository ppa:webupd8team/java

The apt-add-repository command add the ppa to the current repository. You will be prompted with information message about installation instructions with the detail url. You need to press ENTER key to continue the process to add the repository.

apt-add-repository

apt-add-repository

  • sudo apt-get update

This will update the package list from the repositories and update them to get the latest versions of the packages and their dependencies. It will update all repositories and PPAs.

apt-get update

apt-get update

  • sudo apt-get install oracle-java8-installer

This command will start the installation process, you will be prompted to accept the license agreement. Run sudo apt-get install oracle-java7-installer if you want to install JDK7 instead. This process will take sometime to finish depending on your connection speed. And if everything runs well you’ll get Java installed at the end of this process.

apt-get install oracle-java8-installer

apt-get install oracle-java8-installer

  • java -version

This command return the version of the installed JDK. In this case it should return something like:

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

The JDK8 will be installed in the /usr/lib/jvm/java-8-oracle directory. If you need to define the JAVA_HOME environment variable then it should be directed to this directory.