Skip to content

Bash and SSH

dwolfschlaeger edited this page Apr 26, 2017 · 46 revisions

.bashrc

Your bash environment is configured via the file ~/.bashrc (or sometimes ~/.bash_profile).

On NAF machines the default is a zsh shell. If you want to change to bash by default, add exec bash to ~/.zprofile

If these files to not exist, you can just create them. It is recommended to make use of shared configurations in artus-analysis/bashrc.

screen

The window manager screen is a very useful tool for multiplexing terminals. Start a screen session with

screen

If a screen screen session already exists, you can reattach it with

screen -r [-d]

Inside a screen session you need the following commands:

Ctrl-a c # create a new window
Ctrl-a n # change to next window
Ctrl-a p # change to previous window
Ctrl-a d # detach session, which continues running in the background

sed

Replace all strings matching search in file file with replace:

sed -i -e "s/search/replace/g" file

The delimiter (here /) can be (almost) freely choosen, but must not be part of search or replace.

Delete lines matching search in file file:

sed -i -e "/search/d" file

SSH Keys

You can generate your own key for SSH authentification using one of the two commands

ssh-keygen -t rsa
ssh-keygen -t dsa

This has to be done only once. Then the public key is then installed on any the remote machine with the corresponding command

ssh-copy-id -i ~/.ssh/id_rsa.pub <user>@<address of remote machine>
ssh-copy-id -i ~/.ssh/id_dsa.pub <user>@<address of remote machine>

in order to simplify the authentification at this machine. A passwort is then only needed to unlock the key.

SSH Config

If not already existing you create a text file in ~/.ssh/config. In this file you can then define settings for connections to remote machines. The Host defines an alias for your SSH connections, such that you can easily connect to a remote machine via ssh naf, for example. Here is a list of settings that is usually needed in our group:

  • NAF@DESY

     Host naf
     	Hostname = naf-cms.desy.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host naf1
     	Hostname = nafhh-cms01.desy.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host naf2
     	Hostname = nafhh-cms02.desy.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host naf3
     	Hostname = nafhh-cms03.desy.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host naf4
     	Hostname = nafhh-cms04.desy.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host naf5
     	Hostname = nafhh-cms05.desy.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host naf6
     	Hostname = nafhh-cms06.desy.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
  • EKP@KIT

     Host ekplx*
     	Hostname = %h.physik.uni-karlsruhe.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host ekpcms*
     	Hostname = %h.physik.uni-karlsruhe.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host ekpams*
     	Hostname = %h.physik.uni-karlsruhe.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
  • RWTH Aachen

     Host lx3b*
     	Hostname = %h.physik.rwth-aachen.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
     	ProxyCommand = ssh -l <user> portal.physik.rwth-aachen.de "nc %h 22"
    
     Host rwthportal
     	Hostname = portal.physik.rwth-aachen.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
     Host rwthtunnel
     	Hostname = portal.physik.rwth-aachen.de
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    
  • LXPLUS@CERN

     Host lxplus	
     	Hostname = lxplus.cern.ch
     	Compression = yes
     	User = <user>
     	ForwardX11 = yes
    

Access to Remote Directories in File Browser

  • Nautilus

Go to menu File and then Connect to server and type one of the following lines in the dialog that opens.

ssh://<user>@<remote machine>
ssh://<user>@<remote machine>:/<path>
ssh://<host alias from SSH config>

Then create a bookmark and you can easily edit your files with your favourite local and graphical editor.

  • Finder

In order to mount a remote folder on your Mac, you need to install OSXFuse and sshfs at first. Both programmes can be downloaded here:

https://osxfuse.github.io/

Create a small script

vim ~/bin/mount_remote_drive

in your ~/bin/ directory on your Mac.

# give your drive a nice name and location
mkdir /Volumes/SomeNiceName

#mount it
sshfs -o volname=SomeNiceName -o follow_symlinks -o allow_other,defer_permissions,IdentityFile=~/.ssh/id_rsa <user>@remotehost.com:/full/path/to/your/home_directory/ /Volumes/SomeNiceName

After that you need to chmod -x the file to make it executable.

  • -o volname: specifies the name of the remote drive in your Finder.
  • -o follow_symlinks: Enables you to use symlinks created on the remote machine.
  • -o IdentityFile identifies you with your public rsa key.

In order to mount the remote drive you now simply have to type

sudo ~/bin/mount_remote_drive

in your terminal. Find your remote drive in the Finder and add it to the sidebar. Please note: When you reboot your Mac, you need to mount the folder again.

Clone this wiki locally