Linux Basics

Elvenware Logo

TOC

Linux Basics

This page covers some basic information about using Linux.

Linux Distros

There are many different versions of Linux. At this time, I am primarily interested in Ubuntu variants, including Mint and Ubuntu.

  1. Track the most popular distributions: http://distrowatch.com/
  2. Mint Linux
  3. Ubuntu Linux

Installing Ubuntu on a Machine with Windows 8

Windows locks down systems with UEFI. Here are instructions from Ubuntu on how to play nice with this system and install Linux to dual boot on the same machine:

Creating a Linux User

In the example shown below, I assume that you are using an EC2 instance of Ubuntu on the Amazon Web Services cloud. However, the information is equally valid on any Linux box, whether it is in the cloud, or on your local machine. The only caveat being that it is slanted toward Ubuntu or Ubuntu related distros such as Mint.

To create a new user on Linux:

To review, here are the three commands:

    sudo addgroup jsmith
    sudo adduser --ingroup jsmith jsmith
    su -l jsmith

Here is what the session for creating a new group might look like:

    ubuntu@domU-12-25-27-0B-60-D0:~$ sudo addgroup jsmith
    Adding group `jsmith' (GID XXXX) ...
    Done.
    ubuntu@domU-12-25-27-0B-60-D0:~$

Here is what the session for creating a new user might look like:

    ubuntu@domU-12-25-27-0B-60-D0:~$ sudo adduser --ingroup jsmith jsmith
    Adding user `jsmith' ...
    Adding new user `jsmith' (1002) with group `jsmith' ...
    Creating home directory `/home/jsmith' ...
    Copying files from `/etc/skel' ...
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully
    Changing the user information for jsmith
    Enter the new value, or press ENTER for the default
     Full Name []: Julie Smith
     Room Number []:
     Work Phone []:
     Home Phone []:
     Other []:
    Is the information correct? [Y/n] Y

Note that you entered a password and a full name. You can just hit enter for the Room Number all other prompts, until you get to the question about whether the information is correct. For that you must answer Y for yes. This command creates a new user, and adds him to the a new group called jsmith. Your group name, of course, will your first initial and last name, not mine.

Here is what the session to become a new user might look like:

    ubuntu@domU-12-25-27-0B-60-D0:~$ su -l jsmith
    Password:
    jsmith@domU-12-25-27-0B-60-D0:~$

Note that after you issue the command, your shell prompt has changed to include your new user name:

jsmith@domU-12-25-27-0B-60-D0:~$

You use this hint to help you confirm that you are signed in as a new user. Another technique is to issue the command: whoami.

Please note that when you become the new user you will not have as many rights on the system as you did when you were ubuntu. We could, of course, have given jsmith those rights, but we did not. The jsmith account will become our new outward facing interface to the site. As a result, we want it to be as secure as possible. The way things are set up now, even if the user were entirely compromised, the hacker could only do damage to the users account. They would find it relatively difficult, however, to use the account to gain control of the system.

Here is how to delete a user account if you want to start over:

sudo userdel jsmith

Set Up SSH

Now that you have created your new user account, you will probably want to set up SSH so that you can use Putty and Filezilla to sign in and copy files to this account's **public_html **directory or some other location on the server. Here is an overview of what we are going to do:

  1. Generate a new SSH key
  2. Copy the private key (pem file) back to our home machine
  3. Convert the PEM file to PPK file
  4. Set up Putty and Filezilla to work with your new user

NOTE: Another, and perhaps more secure, approach would be to create the new private/public key pair on your home system, and copy only the public key to the new users account. Though that technique is a best practice, I'm not doing that now because it is a bit more difficult for new comers. One step at a time!

Here is how to set up a key from the new users home directory:

ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

NOTE: Commands like those shown above are difficult to get right. You can, however, block copy both lines and paste the commands directly into the shell window. Here is how to proceed: On your home machine, copy the commands with Ctrl-C. Now switch to the Putty shell. Right click on the command line, and both lines should be fed into the command line window and executed, one right after the other. It is probably helpful to type in relatively simple commands by hand, as it will help you memorize them. But these commands are confusing for new comers, and you need not attempt to memorize them.

Because of their arcane complexity, it is probably best for new comers to think of the commands shown above as some kind of Harry Potter like incantation (ie Expecto Patronum) that creates the new key pair, and then copies the public key into a file called authorized_keys. The newly created public key is called id_rsa. At this stage, you need not know more -- except for one minor detail! Note the permissions for id_rsa:

-rw------- 1 jsmith jsmith 1675 Nov  9 21:21 id_rsa

As you can see, only the owner has permissions to read and write to the file. In theory, no one else can even see the file or do much useful with it. If you don't have read permissions, you can't even see that the file exists. (You will find, however, that root can manipulate the file.)

You now have to copy the key that you created from your Linux box back to your home machine.

Once you are ubuntu, here is how the process of copying the file from the jsmithdirectory to the **ubuntu **home directory looks:

sudo cp /home/jsmith/.ssh/id_rsa .

Now make the file visible to filezilla:

sudo chown ubuntu:ubuntu id_rsa

This command sets the owner and group for id_rsa to ubuntu. This means that ubuntu now owns the file, and hence has rights to it. This is a crucial step since Filezilla is attached to your Linux box as the user ubuntu. **If the file were were still owned by **jsmith or by root, then ubuntu, and hence Filezilla, would not have the rights to copy it to your home machine.

Now copy it over with filezilla. Once you have it safely on your machine, the wisest thing to do might be to delete the file from your ubuntu and jsmith folders:

rm id_rsa

Windows feeds on file file extensions, and we have only a limited internal memory capacity. As a result, on your home machine I would rename id_rsa to something more friendly: ec2_jsmith.pem

Now use PuttyGen to convert the PEM file to a PPK file, as described earlier. Add the file to Pageant. Set up Putty and see if you can connect. Everything is as before, but of course this time in the Data page of Putty, you should set the user name to your first initial and last name:

Putty01.png

Figure 01: Fill in your user name in the Auto-login field.

You have now completed the process of creating a new user on Linux with minimum privileges. If you would like to learn how to give the user privileges to serve up HTML files from a public_html directory, then go here:

A command I use a lot is reverse-i-search. At the command prompt, type Ctrl-r. Start typing. For instance type git. You will see the last command you issued that began with those letters. Much like what Ben points out above. Now press Ctrl-r again, you will see the previous command you issued that began with those three letters. And so, on pressing Ctrl-r over and over till you find the command you wanted.

When you find the chosen command, press enter.

This whole thread is perhaps worth reading:

Using sed

The best thing to do, I think is to break the command in two:

find . -iname "*.js" -not -path "**/node_modules/**"

sed -i 's/isit-code-calvert/isit-code-lastname/g' *.js

Learning to use find is not too tricky. But sed is hard, at least for me.

Create a simple text file called learn-sed.txt with three words in it:

one two three

Maybe like this:

echo 'one two three' > learn-sed.txt

Now start running some tests:

$ cat learn-sed.txt   
one two three  
charlie@rohan-elf:~/temp  
$ sed 's/one/sed-did-this/g' learn-sed.txt   
sed-did-this two three  
charlie@rohan-elf:~/temp  
$ sed 's/two/sed-did-this/g' learn-sed.txt   
one sed-did-this three  
charlie@rohan-elf:~/temp  
$ sed 's/three/sed-did-this/g' learn-sed.txt   
one two sed-did-this

Do you see that sed replaced one of the words in the file? In this case, think of sed as a search and replace tool. In the first example, we replace the word one with sed-did-this. Then we replace the word two with the same string. And finally, we replace the word three.

When we give the command s, we are saying, replace the first expression with the second:

The word we want to replace is here:

The word or phrase to replace it with:

And finally, we can ask **sed **to do this globally throughout the document:

If we add -i at the beginning, we are saying "do it in-place". That is, don't just echo the result to the screen, instead write the result to disk by replacing the original file:

$ cat learn-sed.txt   
one two three  
charlie@rohan-elf:~/temp  
$ sed -i 's/three/sed-did-this/g' learn-sed.txt   
charlie@rohan-elf:~/temp  
$ cat learn-sed.txt   
one two sed-did-this

You can see that this time the actual contents of learn-sed.txt was changed.

I should add that this is a gross over-simplification, in large part because I'm not bringing in regular expressions. Nevertheless, this is enough information to help you understand the code that I want you to use.

Tips on Setting up Users

Notice that we have not given the new user you create permission to run sudo. If you try to run a sudo command as the new user, the sudo nazi's will come get you:

    ccalvert@domU-12-25-27-0B-60-D0:~$ sudo ls /var/www/
    [sudo] password for jsmith:
    jsmith is not in the sudoers file. **This incident will be reported.**
    jsmith@domU-12-25-27-0B-60-D0:~$

Of course, you can always become ubuntu again if you want to run a sudo command. In the meantime, it is best run as a normal user that does not to even have the rights to be root. That makes your system much more secure.

NOTE: Windows users have the ability to use Putty to sign in to your server either as ubuntu or as your newly created user. Consider opening up two Putty shell windows, one as Ubuntu, the other as your new user. That way you can do 99 percent of your work as the normal user, and resort to the power of being ubuntu only on rare occasions.

Some links with additional information that you may or may not find useful:

Installing Ubuntu Server Under Hyper-V

The Key Points

The install works more or less the way it would under VirtualBox, with a few minor variations. But getting the hyper-v extensions (which actually ship with Ubuntu!) and the networking going can be more complex.

Start by using the nano (or pico) editor, or some editor of your choice, to modify the modules file:

sudo nano /etc/initramfs-tools/modules

In the file, add these four lines which initializae the hyper-v extensions:

hv_vmbus
hv_storvsc
hv_blkvsc
hv_netvsc

Exit the editor and then type:

sudo update-initramfs –u
sudo reboot

Now do this:

sudo nano /etc/network/interfaces

Yes the pico editor to add these lines to your file:

Auto eth0
iface eth0 inet dhcp

Now you need to restart networking and reboot:

sudo /etc/init.d/networking restart
sudo reboot