In the last “quick tip,” I offered some basics for those new to the command line in Linux (which takes advantage of Amazon’s best pricing on EC2 machines), and in this post, I want to add a few more commands which will build up that foundation.
A graphical interface like a web UI shows you many things you can do. If you are willing to hammer away at the lists of options then you can eventually work out what to do. You won’t figure out everything and you will break a few things along the way, but you will make progress.
You can’t rely on your intuition when you use the Amazon Linux command line interface. It’s just you, a rectangle of abbreviated text to read, and your keyboard to write with. No mouse, no menu, and no clues about what is possible. If you watch the flashing cursor and wait for inspiration, you may find the whole experience as much fun as a wet weekend.
Follow these instructions to take a few more steps up the learning curve.
Log in.
Just getting to the command line is a great achievement for a new user. There’s a lot of knowledge required to make that first step.
- You used the clickety-click AWS web user interface to create an EC2 machine.
- You connected to your Amazon EC2 machine with a private key using PuTTY and Pageant.
- Your security works. The AWS security group is right, the RSA key fingerprint is right and you used the right private key.
Now you’re looking at the CLI.
<code> __| __|_ )</code>
<code> _| ( / Amazon Linux AMI</code>
<code> ___|\___|___|</code>
<code> </code>
<code>https://aws.amazon.com/amazon-linux-ami/2013.03-release-notes/</code>
<code>There are 10 security update(s) out of 17 total update(s) available</code>
<code>Run "sudo yum update" to apply all updates.</code>
<code>[ec2-user@ip-10-234-227-88 ~]$ </code>
What do I do next?
The first hint about what to do next is in that block of text.
<code>Run "sudo yum update" to apply all updates.</code>
Patch with sudo yum update
Your new VM has a few security loopholes that need patching up. That command sudo yum update will patch all the gaps and secure your EC2 machine. When you type in sudo yum update and hit the return key the display fills up with a prodigious amount of obscure text. It then asks you if it ‘s okay to download a whole bunch of files you won’t recognize, like this.
<code>Total download size: 34 M</code>
<code>Is this ok [y/N]:</code>
It’s best for now to go on trust and agree by entering y. Owning an unpatched virtual machine is riskier than owning a poorly understood machine. More screenfuls of information scroll by, with words like Installed, Updated and Replaced.
We are all used to downloading lots of files we don’t really understand to our personal tablets, phones, and laptops, but you really have to step up your levels of paranoia when working with servers on the Internet. Read the TechRepublic EC2 machine patching article for more information on what it all means.
Look around with uname, pwd and ls
Next, have a look around. Run through some command line basics for Amazon Web Services users. This takes you step by step through some really safe commands you can use to find out where you are: uname, pwd and ls. Here’s a quick reminder.
Use the command uname -r to find out what kind of kernel is powering my OS.
<code>[ec2-user@ip-10-48-131-147 ~]$ <strong>uname -r</strong></code>
<code>3.4.37-40.44.amzn1.x86_64</code>
<code>[ec2-user@ip-10-48-131-147 ~]$ </code>
The pwd command tells you where you are. When you log in with your ec2-user account, you start off in your home directory /home/ec2-user.
<code>[ec2-user@ip-10-48-131-147 ~]$ <strong>pwd</strong></code>
<code>/home/ec2-user</code>
<code>[ec2-user@ip-10-48-131-147 ~]<code>[ec2-user@ip-10-48-131-147 ~][ec2-user@ip-10-48-131-147 ~]$
lt;/code>lt;/code>
The ls -a command shows the hidden files in your home directory.
<code>[ec2-user@ip-10-48-131-147 ~]$ <strong>ls -a</strong></code><code>. .bash_history .bash_profile .ssh</code><code>.. .bash_logout .bashrc</code><code>[ec2-user@ip-10-48-131-147 ~]$ </code>Check a file type with file
It’s impossible to tell what a file contains by looking at the name. It might be text that you can read or a binary file that only the OS can read. The file command tells you the type of a file. Do you remember that all things in Linux are files? There are many types of file in Linux. Here are three.
There are text files.
<code>[ec2-user@ip-10-48-131-147 ~]$ <strong>file .bashrc</strong></code><code>.bashrc: ASCII text</code><code>[ec2-user@ip-10-48-131-147 ~]$ </code>It’s ASCII text. ASCII is a very limited set of English characters that has been around for fifty years.
There are directory files.
<code>[ec2-user@ip-10-48-131-147 ~]$ <strong>file /home/ec2-user/</strong></code><code>/home/ec2-user/: directory</code><code>[ec2-user@ip-10-48-131-147 ~]$ </code>A directory is a special kind of file: it’s a little spreadsheet of information including file names, locations, and dates.
There are executable files. The command ls is actually a binary file – a file that contains information written in machine language, not in human language.
<code> [ec2-user@ip-10-48-131-147 ~]$ <strong>file /bin/ls</strong></code><code>/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, BuildID[sha1]=0x083f0a5a25fb311dae23412bcc79f384a865e7d4, stripped</code><code>[ec2-user@ip-10-48-131-147 ~]$ </code>Blimey. That’s a lot of secret code only experts can read. The word executable shows this is a binary file.
Look at a file’s contents with cat
The cat command prints the text inside a file. The name cat is short for catenate (to join together). If you think that’s got nothing to do with displaying a file’s contents, you would be absolutely right (Microsoft DOS uses the slightly less silly name type). You can use cat to join together files – printing file contents is a side effect that turned out to be more useful than the original purpose.
Display the contents of a text file:
<code>[ec2-user@ip-10-48-131-147 ~]$ <strong>cat .bashrc</strong></code><code># .bashrc</code><code> </code><code># Source global definitions</code><code>if [ -f /etc/bashrc ]; then</code><code> . /etc/bashrc</code><code>fi</code><code> </code><code># User specific aliases and functions</code>[ec2-user@ip-10-48-131-147 ~]$
Well, it’s certainly not a shopping list. This file contains a few commands.
Here’s what happens if you try to display the contents of a directory:
<code>ec2-user@ip-10-48-131-147 ~]$ <strong>cat /home/ec2-user</strong></code><code>cat: /home/ec2-user: Is a directory</code><code>[ec2-user@ip-10-48-131-147 ~]$ </code>The OS stops you. A directory is a binary file, like an executable program. You wouldn’t see anything useful.
Display the contents of an executable file:
<code>[ec2-user@ip-10-48-131-147 ~]$ <strong>cat /bin/ls</strong></code><code>ELF>?'@@X?@@@@@@@?@@@@|?|? ??a?a@` ????a??a?@@DDP?tdh\h\Ah\A??Q?t/lib64/ld-linux-x86-64.so.2GNUGNUZ</code><code>? quz?M#?MB#??|CE???A???IH 2?F-???qX??????>K????y?c*+</code><code>Ը???j7?/????@ ?H??</code><code>...</code><code>~X!@X!yp!@p!p??'@?'H??(!A(!?@!A@!'; ?h\Ah\??cAc??a??a? ?a?@?a@?H</code><code> ?@?P?[ec2-user@ip-10-48-131-147 ~]?@?P?[ec2-user@ip-10-48-131-147 ~]$
lt;/code>
What? Hundreds of lines of garbage. That’s what happens when you try to view an executable file. The OS doesn’t stop you. Printing a binary file can break your command line session, but it won’t harm the virtual machine.
Repetition, repetition, repetition
These commands uname, pwd, ls, file, cat and the more complex sudo yum update are useful Linux commands. They have been around a long time and they will stay useful for years to come.
These commands are all safe to play around with. The more you use them the better they will stick in your memory. The more you remember, the less daunting the command line becomes.