Creating aliases (i.e. shortcuts) to launch programs from Terminal

As part of a computer programming course (which utilizes Java) that I started taking, we had to install a program called JGRASP.  In order to launch the program, I have to type the following in Terminal:

$java -jar jgrasp.jar

This is long and difficult to remember.  Luckily, there’s a way to shorten this up and use a custom name that’s easier to remember.

It turns out Ubuntu is set up to handle this type of thing.  By creating a file in the Home directory called:

.bash_aliases

Upon starting Terminal, Ubuntu will check for the existence of this file. If it exists, it will use any aliases present.

So, for this specific example, I added the following to my .bash_aliases file:

alias jgrasp=’java -jar jgrasp.jar’

The format of the alias command is rather straightforward:
alias – Invokes the alias Terminal command.
jgrasp – The name of my alias that I decided upon.
=’ ‘ – The equals sign assigns the following path and/or command(s) that are contained in single quotation marks to the alias.

So, now to launch JGRASP, all I have to type in Terminal is:

$jgrasp

Sweet! And, with the ability to assign a path to a specific alias, that means I also don’t have to change directories to launch programs!

Installing a version control system (SmartGit)

I’ve been taking a computer programming course and finally decided I’d like to have some sort of version control so that I can revert to older versions of the programs I’m writing in case I totally jack something up.

I selected SmartGit from the Git website’s list of Linux version with a graphical user interface (GUI).

Downloaded SmartGit and moved the tarball gzip to my Home directory and upacked/extracted the files by double-clicking.

Ironically, since I selected SmartGit based on it having a GUI, after unpacking the file in my Home directory, I couldn’t figure out how to start/install the program (no GUI for that, I guess!). So, I took a look at the readme.txt file in the SmartGit folder. Here’s what it reads:

Installation

SmartGit itself does not need to be installed; just unpack it to your preferred
location and launch the bin/smartgit.sh script. It might be necessary to define
the SMARTGIT_JAVA_HOME environment variable pointing to the used Java home.

Well, this doesn’t seem like the best start for selecting a program that should make using easier, due to the existence of a GUI. I guess I’ll have to launch it the “old fashioned” way…

Change to the directory containing the smartgit.sh script:
$cd /smartgithg-6_0_5/bin

Launch SmartGit:
$./smartgithg.sh

SmartGit launched and here are the steps that I went through to get it configured:

1.See Git Executable location – Didn’t change anything.

Setup SmartGit-Hg _031

2.Select SSH Client – Didn’t change anything.

Setup SmartGit-Hg _032

3. Enter username/e-mail – Didn’t enter anything here.

Setup SmartGit-Hg _033

4a. Select a hosting provider for online repositories – I already have a GitHub account, so I selected that.

Setup SmartGit-Hg _034

4b. Selected GitHub…

Setup SmartGit-Hg _035

4c. Generated a GitHub API token on the GitHub website…

Authorized applications - Mozilla Firefox_037

4d. Continued API token stuff…

New personal access token - Mozilla Firefox_038

5.SmartGit detected “existing” local Git repositories – Not sure how/why these get detected as Git repositories, since I haven’t used them as such.

Setup SmartGit-Hg _039

6.New or existing repository – Selected to create a new repository.

Welcome to SmartGit-Hg _040

7.Weird message – I just hit “Initialize”

Add or Create Repository _041

Now, I want to make an alias so that I can launch the program without having to remember the location all the time. So, going back to my post on creating aliases in the .bash_aliases file, I’ll add the following to the .bash_aliases file so that I can easily launch SmartGit without having to remember where it is on my system:

alias smartgit=’/home/samb/smargithg-6_0_5/bin/smartgithg.sh’

Now, to launch SmartGit, all I have to type into Terminal is this:

$smartgit

Very nice!

Secure Shell (SSH) SSHure iSSH SSHweet!

SSH allows you to connect to a remote computer and run task remotely. In my situation, this is great for remotely logging in to one of our lab computers that is designed for intensive computing tasks (24GB of RAM!).

Using SSH is also fairly straightforward. To get started logging in to a remote computer/server that you have access to, just type the following in Terminal (and substitute your own username and the address of your target computer):

$ssh username@remotecomputeraddress

Enter your password for the remote computer.

Alternatively, instead of dealing with passwords every time you log in to a remote computer, generate some SSH keys!  Not only can you eliminate the need to use a password and automatically log in when you type your ssh command, but by using keys you can virtually eliminate people being able to use a brute force password attack to break in to your computer/server!

First, generate your key set.  The following command will generate a private and a public key.  The public key can be placed on any server you want SSH key access to.  You can just send the public key to anyone who has the capabilities (both the know-how and authorization) to install it in the correct location on the computer/server you’d like to connect to.  The private key on your computer will then be able to match with your public key on any computer that the public key has been installed on!  No passwords needed for connection!

Generate the keys:

$ssh-keygen -t rsa


Feel free to use an empty password when you are prompted; just hit the “Enter” button and then confirm by hitting the “Enter” button again. This password is only used when physically using your computer to initiate a SSH session. For most people, having a password to initiate a SSH from their computer becomes more of a hassle than it’s worth. However, if you anticipate someone else using your computer, and you’d like to prevent them from easily using SSH to remotely login to servers that you’ve installed SSH keys on, then it would be advised to enable a password for your SSH sessions.

Looking in your

~/.ssh


folder reveals the following:

$ls ~/.ssh
id_rsa  id_rsa.pub  known_hosts


The “id_rsa.pub” file is your public key file. This is the file that can be transferred to other computers to enable password-free SSH capabilities on those computers.

Now that we have our keys, we need to transfer the public key to the server. Assuming you have administrative privileges for the server, there are two options for putting the public key on the server. If it’s the first key, we can use the following command:

$ssh-copy-id username@remotecomputeraddress


That will not only copy the public key from the computer to the server, but it will also create the proper directories if they don’t already exist on the server.

Otherwise, if you have the appropriate permissions, you can also use the following command to append your public key to an existing “authorized_keys” file on the server:

cat ~/.ssh/id_rsa.pub | ssh username@remotecomputeraddress 'cat >> .ssh/authorized_keys'


But, in order for the value of SSH keys to be fully realized, the destination computer/server should have password authentication disabled.  Doing so means that only computers with authorized SSH keys will be allowed access

I’m using SSH keys to lock down my home Synology server.  To do this, I SSH’d into the Synology as user “root”, since “root” is the only user authorized to make system changes.

$ssh root@SynologyIPaddress


By default, Synology only seems to have the text editing program “vi”. Let me tell you, it is NOT intuitive how to use it.  For example, to delete characters, you have to use the ‘x’ key!  Luckily the University of Washington has a nice tutorial on how to use “vi” for editing documents.

$vi /etc/ssh/sshd_config


Once you’re in the file, remove the “#” from in front of the two lines shown below AND change “yes” to “no” in the line “PasswordAuthentication”.  Then, be sure to save the file.
samb@Mephistopheles: ~_018

After quitting (don’t forget to save changes!), we need to restart the SSH service. I ended up doing this via the GUI since some of the common command line suggestions for restarting SSH didn’t work.

Now, when trying to SSH in, you’ll only be allowed in if you’re doing trying to do so from an authorized computer that has a public key installed on the server. On that note, it would be prudent to backup your private key so that if your computer dies, you’ll still be able to authenticate with the remote computer by installing your private key on a new client computer.

Changing the computer (host) name in Ubuntu

You’d think this would be easy.  Using the GUI, just go to “System Settings” > “Details”.  See the box with my computer’s name (Device name) in it?

Details_017

In theory, it seems like I should just be able to click on that and type a new name in.

Nope.

Instead, for some unknown reason, I have to open up Terminal and perform this from the command line.  And, it requires modification of two different files!  Why?  I guess one of the continuing quirks of using Ubuntu.

Anyway, here’re the files that need to be edited and here’re the commands to do so:

1.  Edit the /etc/hostname file

$gksudo gedit /etc/hostname

Remember to use gksudo to open gedit. I discovered this not too long ago. Once the file opens, just replace the existing Device name with the new one. In my file, there was no other text; only the current Device name.

2. Edit the /etc/hosts file

$gksudo gedit /etc/hosts

Once this file opens, find your current Device name and replace it with the new one that you entered in Step 1 above.

So, that’s it, I guess. It certainly isn’t the most intuitive way to accomplish this, but I guess it’s the only way to accomplish the task.