Suppressing the password request on ssh

When from a machine one needs to often access another one via ssh, it may be annoying to have to give the password every time.

It is however simple (just three commands!) to let a machine trust another one by using a pair of public and private keys. How to do it is nicely explained in a post by Mattias Kettner. The provided Linux solution works also for the Mac. (We copy the solution here because on the Internet things may suddenly disappear at any time…)

First, on the localmachine requesting remote access to remote you need to generate a pair of public and private key (accept all the defaults provided):

localuser@localmachine:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa): 
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/localuser/.ssh/id_rsa.
Your public key has been saved in /home/localuser/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A

Second, you use ssh to create on remotemachine, in the remoteuser home, a .ssh directory (it may already exist, which is ok)

localuser@localmachine:~> ssh remoteuser@remotemachine mkdir -p .ssh
remoteuser@remotemachine's password:

Third, you have to append the public key which was generate locally to the .ssh/authorized_keys on the remote machine (if the file does not exist, it will be created).

localuser@localmachine:~> cat .ssh/id_rsa.pub | 
        ssh remoteuser@remotemachine 'cat >> .ssh/authorized_keys'
remoteuser@remotemachine's password: 

(Note that the first two lines above must be a single command)

Done! From now on you can log into B as b from A as a without providing a password anymore:

localuser@localmachine:~> ssh remoteuser@remotemachine
Posted in Admin

Mirroring an ipad on a mac

Just bought a new iPad Pro with its fantastic (but really expensive) pencil.

The best jotting tool I found is Notability. The next step is to be able to project what’s being written on the ipad on a projector. Of course there is a (again, really expensive) adapter to VGA or HDMI. However, there are other solutions.

1 – wired

connect the USB cable to the mac, start QuickTime on the mac, choose “record a video” and near to the record button choose as source the iPad. Put it on full screen: perfect, although on a leash!

2 – wireless

There is a software solution: install AirServer on the mac (free one week trial, then an inexpansive licence) and AirServer Connect on the iPad (free). Start AirServer on the mac and AirServer Connect on the ipad, follow the easy two-step instructions there – there you go. Impressive speed, excellent performance… as long as the network has bandwith. In a 10 minutes test, it worked perfectly until something on the net went (probably) wrong. Restarting it was far from quick. So, I’m afraid I cannot trust the wireless solution. What a pity!

A second software solution: install Reflector2 on the mac, and connect to it from the iPad (swipe up from bottom, in the Airport section at the bottom chose your mac). Again, it is a 7 days trial (with a watermark, licence more expensive than AirServer but still less than 20 €). Works well, I was not able to get it in trouble on the same network and in the same conditions I had for AirServer). The only shortcoming: it is not possible to scale the iPad in full screen.

Tagged with:
Posted in Apps, Multimedia, Utilities

Using resources in a java program

Setting the correct path for resources in a Java program so that they both work from command line and from inside a jar can be confusing and daunting. We show a simple, step by step working example, which uses both available syntaxes (i.e. by calling the ClassLoader or not).

We will assume that we have the following development path:

src
 |- sample
       |- UsingResources.java
       |- res
           |- file.txt

This is our java class:

package sample;
public class UsingResources {
    public static void main(String a[]) {
        new UsingResources();
    }
    UsingResources() {
        ClassLoader cl = this.getClass().getClassLoader();
        System.out.println(cl.getResource("sample/res/file.txt"));
        System.out.println(getClass().getResource("res/file.txt")); 
    } 
} 

As we can see,

  • the getClass().getClassLoader().getResource() method  requires the path specification starting from the directory which contains the package (src),
  • the getClass().getResource() method requires a path relative to the class location (src/sample)

we can test by compiling the code:

cd (full-path-to/)src
javac sample/UsingResources.java
java sample/UsingResources

and the output will be

file:/Users/ronchet/DemoResources/src/sample/res/file.txt
file:/Users/ronchet/DemoResources/src/sample/res/file.txt

showing that everything works.

Now let us try with the jar. First, let us create it:

echo "Main-Class: sample/UsingResources" > MainClass.txt
jar cmf MainClass.txt UsingResources.jar sample/*.class sample/res/*

Let’s run it:

java -jar UsingResources.jar

the output is

jar:file:/Users/ronchet/DemoResources/src/UsingResources.jar!/sample/res/file.txt
jar:file:/Users/ronchet/DemoResources/src/UsingResources.jar!/sample/res/file.txt

showing that everything works correctly.

Tagged with:
Posted in Uncategorized

Docker tutorials 7: installing Apache2 on docker (on a Mac) – part 2: using a Dockerfile

We can now apply what we learned in Tutorial 6 by using the Dockerfile techniques we discussed in Tutorial 3. We had the following Dockerfile:

FROM ubuntu:latest
MAINTAINER myname <myname@mydomain.com>
RUN apt-get update
RUN apt-get -y upgrade
CMD /bin/bash

We now need to extend it by adding the port declaration, the Apache installation, file system mapping and the right command at the end. Our file becomes:

FROM ubuntu:latest
MAINTAINER myname <myname@mydomain.com>
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get -y install apache2
EXPOSE 80
ADD mySharedDirectory /var/www/html
CMD …

First we install apache2, then we EXPOSE port 80, then we map mySharedDirectory on the host onto /var/www/html on the container.

What do we write in the CMD?
If we declared /bin/bash as before, after running our machine we’d need to manually start apache2ctl. Probably is not what we want.

We might try to CMD /usr/sbin/apache2ctl, but it would not work: the command returns immediately, and our container would stop!

We can try to fix this behavior by declaring

CMD /usr/sbin/apache2ctl -D FOREGROUND

because in this way the command would not return immediately. However, we could not do anything else on the container (like e.g. examining the log files from a shell while the container is running).

A possible solution is to declare CMD bash -c ‘apache2ctl start ; bash’which means that we open a shell, in that we launch in a subshell apache2ctl and another shell. In this way, the web server starts but we still have a shell allowing us to perform operations on the machine.

So this is our preferred option:

CMD bash -c 'apache2ctl start ; bash'

Of course, having written the Dockerfile, we need now to create the image and then run it, bit that’s what we also did in Tutorial 3:

docker build -t myname:myLinux2 --rm=true .
docker run -it myname:myLinux2

Tagged with: ,
Posted in Docker

Docker tutorials 6: installing Apache2 on docker (on a Mac) – part 1

We need to

  • install apache2 on the container
  • start it
  • access its port (80)

Now things get a bit complex: remember that on a Mac we need to run a VirtualBox to have a Linux VM onto which docker acts. So we need the container to expose the port to its host (VirtualBox), and VirtualBox has to expose it to the Mac. To find out the IP address of VirtualBox, we execute

docker-machine ls
NAME     ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER   ERRORS
default   *       virtualbox   Running   tcp://192.168.99.100:2376           v1.10.2

and we discover that the Mac sees the VirtualBox as 192.168.99.100: so this is the address we’ll need to use to access our Apache once that’s installed.

As a first step, when we activate the container onto which we want to install apache2, we need to make the port 80 visibile from the host machine

docker run --name myLinux2 -p 0.0.0.0:8000:80 -it ubuntu:latest bash

The switch -p 0.0.0.0:8000:80 maps the port 80 in the container to port 8000 exposed by the VirtualBox on the host machine: so now we have the full info about where to find Apache once that’s installed: it will be on 192.168.99.100:8000.

NOTE: it is really important that we use 0.0.0.0.on a Mac. If we had a Linux PC instead, we’d use 127.0.0.1, and to see the page from the browser we’d use localhost:8000.

The rest is easy. On our running “myLinux2” container we just need install Apache, so (after the usual to execute apt-get update and apt-get –y upgrade that we found in Tutorial 1) we execute

apt-get install -y apache2

Once it is done, we need to start it.

We say

apache2ctl start

then we run a browser on our Mac pointing at 192.168.99.100:8000, et voilà, we get the standard home page.

WATCH OUT: it is apache2ctl, NOT apachectl (since we’ve installed the most recent Apache versione: 2.4).

We’ll need to do some customization. Surely we want to customize the directory where we serve from, and most likely also the Apache configuration.

That is simply done, by using the file system sharing we introduced in Tutorial 5.

As we had to plan in advance that we wanted to share a port, and we declared it when starting the container, we’ll have to plan in advance the file system sharing.

So we’ll need to add to the docker run command the following switch:

-v /full/path/to/mySharedDirectory:/var/www/html

if we keep the default, that the web home directory is /var/www/html.

If we want to change the something in the configuration, we can do it inside the container (editing the files in /etc/apache2/) , or outside, by preparing a mapping of the files we want to change onto our host machine file system.

All this was done by hand. Of course it is much more practical to do it via Dockerfile, and this is what we are going to discuss in next tutorial.

 

 

Tagged with: ,
Posted in Docker

Docker tutorials 5: sharing a portion of the file system with docker

1) Create a directory that will be shared with the docker machine, and let su create a file in it

mkdir mySharedDirectory
cat > mySharedDirectory/myFile
hello
^D

2a) Let us assume we have an image like the one created in Tutorial 3

docker images
REPOSITORY                    TAG                 IMAGE ID           CREATED             SIZE
myname                         myLinux2           50ee9e4f504d       13 minutes ago     215.9 MB

We can now run that image, mounting mySharedDirectory on /containerDir on the container

docker run --name myLinuxBox -v /full/path/to/mySharedDirectory:/containerDir -it myname:myLinux2

We can now find myFile in the docker machine at /containerDir/myFile. If we change it, the changes apply to myFile in mySharedDirectory in the host machine.

2b) In alternative. we can embed the mounting in the image creation process. We add to the Dockerfile we discussed in Tutorial 3 the “ADD” line

FROM ubuntu:latest
MAINTAINER myname <myname@mydomain.com>
RUN apt-get update
RUN apt-get -y upgrade
ADD mySharedDirectory /var/www/site
# By default, simply start a shell
CMD /bin/bash

Now, whenever we run this image, the /var/www/site directory will be mapped into the mySharedDirectory located in the place where we run the build.

References:

Docker volumes:
https://docs.docker.com/engine/userguide/containers/dockervolumes/

Tagged with:
Posted in Docker

Docker tutorials 4: image management

In tutorial 3 we have seen how to build an image, now let’s see how to manage them.

Let’s assume we have some images at hand:

docker images
REPOSITORY                     TAG                 IMAGE ID           CREATED             SIZE
myname                        myLinux2           50ee9e4f504d       13 minutes ago     215.9 MB

Start an image:

let’s start the image, running the default command and attaching the terminal:

docker run -it myname:myLinux2
#

Alternatively, we could have started the image, running an alternate command and attaching the terminal:

docker run -it myname:myLinux2 /bin/ls
bin   dev home lib64  mnt proc run  srv tmp var
boot etc lib media    opt root sbin sys usr

We get back to the host machine, since after running ls the docker machine has finishes

Rename an image:

If we wanted to rename an image, we could use the old name or the old id in the form docker tag [oldname|old_id] newname, as in:

docker tag myname:myLinux2 myrepository:mytag

docker tag 3583bb668676 myrepository:mytag

Delete an image:

If we want to get rid of the image, we can run

docker rmi myname:myLinux2

References:

Docker images:

Tagged with:
Posted in Docker