{"id":242,"date":"2012-09-07T01:00:00","date_gmt":"2012-09-07T00:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=242"},"modified":"2012-09-07T00:23:21","modified_gmt":"2012-09-06T23:23:21","slug":"bashing-at-the-keyboard-part-i-2","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=242","title":{"rendered":"Bashing at the Keyboard (part I)"},"content":{"rendered":"<p>The command line. The feared enemy of anyone who started using a computer after 1995 (ish). It\u00e2\u20ac\u2122s got a bad reputation, and for day to day computer tasks it\u00e2\u20ac\u2122s certainly not necessary. However, if you are a programmer or a systems administrator then you\u00e2\u20ac\u2122ll benefit enormously from knowing how to get things done on a computer using only a keyboard and a terminal.<\/p>\n<p>I\u00e2\u20ac\u2122d better take a step back at this point, and define my terms. When a Linux system boots, you (probably) see a text mode screen showing lots of gobbledygook (people peering over my shoulder usually wittily opine \u00e2\u20ac\u0153it\u00e2\u20ac\u2122s like the Matrix\u00e2\u20ac\u009d). That gobbledygook does have useful information, but I don\u00e2\u20ac\u2122t want to burden you with that at present. Suffice it to say: what you\u00e2\u20ac\u2122re seeing is all the background plumbing of your computer starting up. The same sort of thing happens on Windows, but you don\u00e2\u20ac\u2122t see it happening, you just see a progress bar or some kind of animated spinning arrow.<\/p>\n<p>This text appears on what is called the <em>console<\/em>. It\u00e2\u20ac\u2122s a very simple screen and keyboard implementation provided to get you access to your system when all else fails. Once the boot is completed, a login prompt gets shown on the console, and you could login with your normal user and password. However, on a modern system what probably happens, before you get time to look at the login prompt, or type anything, is that the graphics environment starts up and shows you a graphical login prompt. Should you wish, you can return to the text mode console screen by pressing Ctrl-Alt-F1. To get back to the graphical login you can press Ctrl-Alt-F7 (sometimes F8, depending on your distribution; and actually the Ctrl is unnecessary when going from text to graphics, but don\u00e2\u20ac\u2122t worry too much about that).<\/p>\n<p>So; we can choose to log in graphically, or textually. Let\u00e2\u20ac\u2122s do textually to start, just so you\u00e2\u20ac\u2122ve got an idea of what you would see (this is what I see, yours will be similar, with the differences depending on what distribution you use).<\/p>\n<pre><code>Debian GNU\/Linux squeeze\/sid host tty1\n\nhost login: user\nPassword:\n\nNo mail.\nuser@host:~$\n<\/code><\/pre>\n<p>Your password doesn\u00e2\u20ac\u2122t get echoed to the screen, for obvious reasons, and you get very little noise after logging in, you are simply given a <em>command prompt<\/em>, in this case \u00e2\u20ac\u0153<code>user@host:~$<\/code>\u00e2\u20ac\u009d. Remember that for now, and we\u00e2\u20ac\u2122ll go back to the graphical login with <code>Ctrl-Alt-F7<\/code>.<\/p>\n<p>Login as normal. Now, find in your application menu something like \u00e2\u20ac\u0153Terminal\u00e2\u20ac\u009d, \u00e2\u20ac\u0153Console\u00e2\u20ac\u009d, \u00e2\u20ac\u0153gterm\u00e2\u20ac\u009d, \u00e2\u20ac\u0153xterm\u00e2\u20ac\u009d, \u00e2\u20ac\u0153rxvt\u00e2\u20ac\u009d, \u00e2\u20ac\u0153Konsole\u00e2\u20ac\u009d \u00e2\u20ac\u201d exactly what it\u00e2\u20ac\u2122s called is dependent (as usual) on your distribution and your chosen graphical environment (GNOME or KDE probably) \u00e2\u20ac\u201d I like <code>xterm<\/code> myself (and I\u00e2\u20ac\u2122ll say <code>xterm<\/code> from now on, but whichever you pick is fine). These are all so-called \u00e2\u20ac\u0153terminal\u00e2\u20ac\u009d programs. What they do is provide a graphical equivalent of the console we\u00e2\u20ac\u2122ve just seen. The name \u00e2\u20ac\u0153terminal\u00e2\u20ac\u009d is a holdover from the days when computing was done with one huge single computer, with a dumb terminal (basically a screen and keyboard with a long wire connecting them to the computer) on the users desk. These terminal programs are the modern day equivalent, you open one and it provides a screen and keyboard in a window. What you\u00e2\u20ac\u2122ll see when you run it is a window with the following in it.<\/p>\n<pre><code>user@host:~$\n<\/code><\/pre>\n<p>This is, for all intents, exactly the same as the command prompt you got when you logged in on a console. Anything you can do in text mode, you can do in a graphical terminal supplied by xterm (<em>et al<\/em>).<\/p>\n<p>It\u00e2\u20ac\u2122s worth pointing out here that it is not xterm that is providing this prompt \u00e2\u20ac\u201d it\u00e2\u20ac\u2122s merely supplying a screen and keyboard for another program. Terminal programs, once they have started, then proceed to run another program that has its input and output redirected to use the window that the terminal program is providing. By default, they run a program called <code>bash<\/code>. It is of a class of programs called <em>shells<\/em> and is the most commonly used. <code>bash<\/code> is the program that has output this command prompt \u00e2\u20ac\u0153<code>user@host:~$<\/code>\u00e2\u20ac\u009d. In exactly the same way, when we logged in on the console we were logging in using a program called (unsurprisingly) <code>login<\/code>; which upon accepting the login credentials you give it runs a copy of <code>bash<\/code> as well. It should be no surprise then to see that we get an identical prompt in both cases.<\/p>\n<p>Shells are programs that are used to run other programs. At first hearing, it sounds like a silly thing to want. However, it\u00e2\u20ac\u2122s use is so implicit that often people don\u00e2\u20ac\u2122t even notice that they\u00e2\u20ac\u2122ve been using it. As an example: many people think that the task bar, explorer and start menu in Windows <em>are<\/em> Windows \u00e2\u20ac\u201d when actually they are just a graphical shell, and a surprisingly small part of the operating system. Whatever graphical system you just used to find <code>xterm<\/code> is a graphical shell is simply one program that is used to start another. <code>bash<\/code> is a textual shell, but has the same job \u00e2\u20ac\u201d it lets you start other programs on the system. Let\u00e2\u20ac\u2122s run a few programs now in our shell to get the idea.<\/p>\n<pre><code>user@host:~$ ls -a\n.  ..  .bash_history  .bash_profile  .bashrc\nuser@host:~$ cat .bash_profile\n# include .bashrc if it exists\nif [ -f ~\/.bashrc ]; then\n    . ~\/.bashrc\nfi\n\n# set PATH so it includes user's private bin if it exists\nif [ -d ~\/bin ] ; then\n    PATH=~\/bin:&quot;${PATH}&quot;\nfi\n<\/code><\/pre>\n<p>We ran a program called <code>ls<\/code>, which lists the contents of the current directory (the <code>-a<\/code> means list <em>all<\/em> files, including hidden files). Then we ran a program called <code>cat<\/code>, which reads a file and outputs it to the terminal \u00e2\u20ac\u201d in this case, one of the bash startup files, <code>.bash_profile<\/code>. The startup files for <code>bash<\/code> are a list of commands that are run automatically when <code>bash<\/code> starts. They are pretty much entirely devoted to setting up a workable command line, and configuring <code>bash<\/code> to suit the user (the default versions are fine for you for now).<\/p>\n<p>Notice what I said about that first command, that it lists the contents of the <em>current<\/em> directory. <code>bash<\/code> maintains the idea of a current directory, and whenever you run another program with it, it passes that current directory to it. If you\u00e2\u20ac\u2122re here from an entirely Windows-based computer upbringing, the terms \u00e2\u20ac\u0153directory\u00e2\u20ac\u009d and \u00e2\u20ac\u0153folder\u00e2\u20ac\u009d are entirely synonymous. Let\u00e2\u20ac\u2122s look again at the command prompt that <code>bash<\/code> has been giving us.<\/p>\n<pre><code>user@host:~$\n<\/code><\/pre>\n<p>This prompt is configurable, and your distribution might have picked something different, but this is a common default form. There are three parts to this, separated by three punctuation characters.<\/p>\n<ul>\n<li>\u00e2\u20ac\u0153<code>user<\/code>\u00e2\u20ac\u009d this is the username active in this session of <code>bash<\/code> \u00e2\u20ac\u201d in this case, the username you logged in with.<\/li>\n<li>\u00e2\u20ac\u0153<code>@<\/code>\u00e2\u20ac\u009d is punctuation, separating the first two fields<\/li>\n<li><code>host<\/code> is the name given to this computer by the system administrator during installation. If you are interested, it is simply read from the file <code>\/etc\/hostname<\/code> during boot.<\/li>\n<li>\u00e2\u20ac\u0153<code>:<\/code>\u00e2\u20ac\u009d is punctuation<\/li>\n<li>\u00e2\u20ac\u0153<code>~<\/code>\u00e2\u20ac\u009d is the current directory, with the notation that the special case that the user\u00e2\u20ac\u2122s own home directory is shortened to <code>~<\/code>. You can read that as <code>\/home\/user<\/code>. It is purely for brevity.<\/li>\n<li>\u00e2\u20ac\u0153<code>$<\/code>\u00e2\u20ac\u009d marks then end of the prompt, and the cursor is left after this ready for you to type your commands.<\/li>\n<\/ul>\n<p>Now that we know what <code>~<\/code> means, let\u00e2\u20ac\u2122s go somewhere else and see what happens to it.<\/p>\n<pre><code>user@host:~$ cd ..\nuser@host:\/home$ cd \/\nuser@host:\/$ cd \/etc\nuser@host:\/etc$ cd init.d\nuser@host:\/etc\/init.d$ cd\nuser@host:~$\n<\/code><\/pre>\n<p>Key points to note:<\/p>\n<ul>\n<li>\u00e2\u20ac\u0153<code>cd<\/code>\u00e2\u20ac\u009d is a builtin bash command which means <em>change directory<\/em>. With no arguments, it defaults to changing to the current user\u00e2\u20ac\u2122s home directory (which we see as \u00e2\u20ac\u0153~\u00e2\u20ac\u009d in the example above).<\/li>\n<li>\u00e2\u20ac\u0153\/\u00e2\u20ac\u009d on its own is the root directory. If you are from a Windows background, you might be surprised at the lack of a drive letter, and the change of direction of the slash. You would be used to having multiple root directories on each drive, labelled \u00e2\u20ac\u0153&quot; rather than\u00e2\u20ac\u009d\/\u00e2\u20ac\u0153, for example\u00e2\u20ac\u009dC:&gt;&quot; would be your command prompt.<\/li>\n<li>\u00e2\u20ac\u0153\/\u00e2\u20ac\u009d is also the directory separator. <code>\/etc\/init.d<\/code> is a directory called <code>init.d<\/code> inside a directory called <code>etc<\/code> which exists in the root directory.<\/li>\n<li>Notice the difference between an absolute path, which starts at the root directory (\u00e2\u20ac\u0153<code>\/etc<\/code>\u00e2\u20ac\u009d) and a relative path which doesn\u00e2\u20ac\u2122t (\u00e2\u20ac\u0153<code>init.d<\/code>\u00e2\u20ac\u009d).<\/li>\n<li>A relative path can specify \u00e2\u20ac\u0153up a level\u00e2\u20ac\u009d by giving the name \u00e2\u20ac\u0153..\u00e2\u20ac\u009d; in the example above \u00e2\u20ac\u0153<code>cd ..<\/code>\u00e2\u20ac\u009d is used to go up from <code>\/home\/user<\/code> to <code>\/home<\/code>.<\/li>\n<\/ul>\n<p>The guides I write will almost universally be using the command line for the examples. There is an advantage to the command line for guide writers in that it\u00e2\u20ac\u2122s much easier to give repeatable instructions, and give sample output than with a graphical environment. Describing which buttons and in what sequence a set of operations should be performed just gets in the way of the information.<\/p>\n<p>Another important advantage to becoming familiar with the command line is that you can administer a remote system with a program called <code>ssh<\/code>. <code>ssh<\/code> is short for <em>secure shell<\/em>. It\u00e2\u20ac\u2122s power is most easily shown with an example.<\/p>\n<pre><code>user@host:\/etc\/init.d$ ssh someotherhost\nThe authenticity of host 'host (192.168.1.1)' can't be established.\nRSA key fingerprint is 3c:71:ac:13:5f:f6:04:2b:a0:c7:2a:b5:56:4e:ce:fd.\nAre you sure you want to continue connecting (yes\/no)? yes\nWarning: Permanently added 'host,192.168.1.1' (RSA) to the list of known hosts.\nPassword: \n\nuser@someotherhost:~$ \n<\/code><\/pre>\n<p>This was the first connection, so we get a warning about recording a new host fingerprint; we won\u00e2\u20ac\u2122t see this for subsequent connections, and the recorded fingerprint will be automatically compared with the remote fingerprint to prevent man-in-the-middle attacks. We supply a remote host to connect to, and are asked for a password. What has happened though? <code>ssh<\/code> has established an encrypted connection from our system (host) to a different system (someotherhost), that let us type in our password without the possibility that it could be captured by someone monitoring the network, and then over that authenticated, encrypted connection, <code>ssh<\/code> runs <code>bash<\/code>. The power of this is subtle: we have a command line from the remote machine \u00e2\u20ac\u201d <em>anything<\/em> we can do on our local command line, we can now do on the remote command line. Supplying a command line is very low bandwidth, so this can be done over the worst of connections.<\/p>\n<p>I use exactly this facility to look after my family\u00e2\u20ac\u2122s computers, and to administer a VPS (virtual private server) I hire. Being able to use a command line, and hence a VPS gives me access (for a price comparable to simple web hosting services) to a very powerful machine, with lots of bandwidth and the ability to install whatever server software I might choose. If you rely on graphical interfaces, you are stuck with whatever your hosting provider feels like giving you both in services and in browser-based management interfaces.<\/p>\n<p>If you want to do any programming on a Linux system, you will gain an enormous advantage by not being afraid of the command line (it\u00e2\u20ac\u2122s almost a prerequisite in fact).<\/p>\n<p>There is more to say about command lines; the above is nothing more than an introduction to the concept; and an explanation for where it is useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The command line. The feared enemy of anyone who started using a computer after 1995 (ish). It\u00e2\u20ac\u2122s got a bad reputation, and for day to day computer tasks it\u00e2\u20ac\u2122s certainly not necessary. However, if you are a programmer or a systems administrator then you\u00e2\u20ac\u2122ll benefit enormously from knowing how to get things done on a\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=242\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[7,8,6],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/242"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=242"}],"version-history":[{"count":5,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":707,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions\/707"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}