# Mac Help

This page has some help on random tasks that you may need on a Mac.

# Terminal

You can open Terminal in a folder to enter commands from that folder. In the Finder, right-click a folder and choose New Terminal at Folder.

# SSH connection

You use SSH connections to connect to your server or remote host from your workstation. You can open a terminal at the remote host to enter commands, you can download or upload files, and you can use services at private ports using SSH tunnels.

For an SSH connection you need:

  • The host name or IP address of your server.
  • The username and the private key or password of a user.

If you only have a password, we suggest to create a private key for extra security. Some SSH servers do not even allow logins with a password.

# First connection

  • Open Terminal.
  • If you have your private key at ~/.ssh/id_rsa or if you only have a password, then enter this command:
    % ssh <USERNAME>@<HOSTNAME>
    
    If you have a private key at another location, then enter:
    % ssh -i path/to/private_key <USERNAME>@<HOSTNAME>
    
  • The first time you need to indicate that you trust the host. Enter “yes”.
  • If you don’t have a private key, enter your password.

If you are installing an Ubuntu server and you just logged in with the initial ubuntu or root user, you should go back to the Ubuntu installation and create a normal user now.

# Set up a private key

If you are logging in with a password, we recommend to set up a private key instead. You can also use these instructions to set up a new private key, for example for another workstation. First we create a private / public key pair.

  • Open a new terminal window on your computer and enter this command:
    % ssh-keygen
    
  • When asked for the file location and passphrase, just press Enter.
  • You should now have two files at the shown location (directory .ssh in your home directory):
    • id_rsa: your private key
    • id_rsa.pub: your public key
  • Copy the files to a safe location so you can restore them if needed.

Upload the public key to your server.

  • Still in the terminal on your computer, if you only have a password, then enter this command:
    % scp .ssh/id_rsa.pub <USERNAME>@<HOSTNAME>:~
    
    If you have another private key, for example from another workstation, then enter this command:
    % scp -i path/to/private_key .ssh/id_rsa.pub <USERNAME>@<HOSTNAME>:~
    
  • Enter your password if needed.
  • Now the public key is at your server.

Now we will open a terminal to your server.

  • In the terminal on your computer, if you only have a password, enter:
    % ssh <USERNAME>@<HOSTNAME>
    
    If you have another private key, enter:
    % ssh -i path/to/private_key <USERNAME>@<HOSTNAME>
    
  • Enter these commands:
    $ mkdir -p .ssh
    $ cat id_rsa.pub >> .ssh/authorized_keys
    $ rm id_rsa.pub
    
  • Close the connection with exit.

# Open terminal

You can connect to your server any time with these steps:

  • Open Terminal on your computer.
  • Enter this command:
    % ssh <USERNAME>@<HOSTNAME>
    
    Or if your private key is not at ~/.ssh/id_rsa:
    % ssh -i path/to/private_key <USERNAME>@<HOSTNAME>
    
  • If you don’t have a private key, enter your password.

# Download or upload files

Downloading and uploading files works similarly to copying files on your machine in the terminal.

  • Open Terminal on your computer in the directory to which you want to download files, or from which you want to upload files.
  • Upload a file with this command:
    % scp myfile.txt <USERNAME>@<HOSTNAME>:/path/to/remote-dir
    
    Or if your private key is not at ~/.ssh/id_rsa:
    % scp -i path/to/private_key myfile.txt <USERNAME>@<HOSTNAME>:/path/to/remote-dir
    
  • Download a file with this command:
    % scp <USERNAME>@<HOSTNAME>:/path/to/remote-dir/myfile.txt .
    
  • For uploading to the remote home directory, use path ~:
    % scp myfile.txt <USERNAME>@<HOSTNAME>:~
    

# SSH tunnel

An SSH tunnel allows you to access a port on a server that is otherwise not accessible outside the server because it is blocked by a firewall. You commonly use an SSH tunnel to access private development or administration services. The SSH tunnel maps a local port on your workstation to a destination that is accessible from the server. Then you can access it on localhost at your workstation.

For example you can map local port 28080 to remote destination localhost:8080. Here localhost is from the perspective of the server, and you could also use another hostname that is accessible from inside the server. When you have set it up, you can access localhost:28080 from your workstation and it will be the same as accessing localhost:8080 from inside the server.

  • Open Terminal.
  • Following our example, enter this command:
    % ssh -L 28080:localhost:8080 <USERNAME>@<HOSTNAME>
    
    Here the local port is 28080 and the remote destination is localhost:8080.
  • If your private key is not at ~/.ssh/id_rsa, then enter:
    % ssh -i path/to/private_key -L 28080:localhost:8080 <USERNAME>@<HOSTNAME>
    

When you are connected, the SSH tunnel is effective and you can connect to localhost:28080 on your workstation.

# Add to PATH

Some programs need to be in your “PATH”, so they can be run from any working directory. You add the directory where the program is located. For example use the following steps to add this directory: /usr/local/lib/someapp/bin

  • Open Terminal.
  • Enter this command:
    echo 'export PATH="/usr/local/lib/someapp/bin:$PATH"' >> ~/.zshrc
    
  • Close the terminal and re-open it, so the new PATH becomes effective.