Archivo de la categoría: Linux

Using multiple SSH keys for same host in Ubuntu

Any developer has at least one SSH key, mostly used for login through SSH to remote machines or working with GIT repositories.

When we try to connect through SSH to another machine or pulling from a GIT repository, our system is going to use the first SSH key we created by default.

So, what happens if we want to use different SSH Keys for same server or same repository? How we deal with this trouble?.

We have to make a config file in our SSH folder and specify to the system which key has to use given host.

Let’s see an example where we have two different repositories hosted in Github.com and we want to use different SSH keys for each repository.

First we have to create our ~/.ssh/config file:

Host project_1
    HostName github.com
    IdentityFile ~/ssh/dev_key

Host project_2
    HostName github.com
    IdentityFile ~/ssh/deploy_key

For example, it is common having multiple SSH keys in our production server, where each SSH key have access to specific repositories.

Now in our production server, if we want to pull changes for our project_1 we have to do run this command:

git pull git@project_1:user/project.git

For project_2, will be the same command with the host in the repository url modified:

git pull git@project_2:user/project.git

Analyzing the config file, Host it’s the alias for our hostname, in our case github.com, and in the IdentityFile we link to our SSH key path.

That’s all, easy and powerful.

Creating alias for Linux command line

Command-line alias is a quick and useful tool for those repetitive commands which we are continuously executing in our terminal. We can transform large and painful commands into a one word length alias.

Let’s see how to make it:

First of all, we have to open a new terminal in our favorite Linux distribution.

Now edit the .bashrc file which is located in our home directory. To do this we have to type the next command in the terminal:

sudo nano ~/.bashrc

Then nano editor (you can use your favorite text editor) will be open and we can add a new alias (one per line).

When defining a new alias it must have the following structure:

alias ALIAS_NAME='OUR_ALIAS_COMMAND'

For example, we are going to add this new alias:

# Download all servers backups
alias remote_backups='python /var/www/scripts/remote_backups.py'
 

After edit our .bashrc file, we have to save the file changes.

Last step is loading the .bashrc file into the current command prompt executing the following command:

source ~/.bashrc

Now we are ready to execute our aliases in the terminal.

# Alias execution example (in a terminal)
$ remote_backups
 

Sincronizar los archivos de un proyecto automáticamente en Sublime Text utilizando un directorio compartido con Samba

Actualmente, para mis desarrollos web trabajo sobre Windows 10 con Sublime Text y un directorio compartido con Samba que conecta con un servidor Ubuntu en local, donde tengo instaladas todas mis herramientas necesarias: PHP 7, MySQL/MariaDB, Composer, GIT, Bower, etcétera.

Cuando te encuentras en Windows y añades tu directorio como proyecto en Sublime Text, a la hora de generar nuevos archivos o directorios desde fuera del editor, ya bien sea directamente desde el explorador de Windows en un directorio de tu proyecto, o utilizando algún generador como la consola de Symfony, nos damos cuenta rápidamente de que estos nuevos archivos que han sido generados no aparecen en nuestro proyecto de Sublime Text, por lo menos hasta que volvemos a reiniciar Sublime Text, algo que es un verdadero rollo ya que Sublime Text a veces tarda demasiado en arrancar si tenemos varios proyectos en nuestro menú izquierdo.

En el foro oficial de Sublime Text, nos enteramos de que los directorios compartidos con Samba no envían por defecto el evento de notificación ReadDirectoryChangesW de la Win32API, por lo que a Sublime Text no le llega la notificación de que algún archivo ha cambiado.

Para solucionar este problema, debemos añadir el siguiente valor a nuestro archivo smb.conf.

En una distribución de Ubuntu, habría que seguir los siguientes pasos:

  1. Abre un terminal
  2. Edita el archivo de configuración
    sudo nano /etc/samba/smb.conf
  3. Añade la siguiente línea debajo de la línea que pone [global]
    change notify = yes
  4. Guarda los cambios y cierra el archivo
  5. Reinicia los servicios de Samba
    sudo service smbd restart
    sudo service nmbd restart
  6. Elimina tu proyecto en Sublime Text y vuelve a añadirlo.
  7. Voila. Tus proyectos ya se sincronizan en tu editor favorito.