New terms from treehouse – Console Foundations Part 1 – Getting Started with the Console and Users and Permissions

Getting Started with the Console

The console is another way to interact with your computer. You type in commands and get text back, it’s not like a Graphical User Interface (GUI). Why learn it? Some tools you might need my not have a GUI. Unix is an OS from the 60s that a lot of modern ones are based on, like GNU/Linux and Apple’s Darwin. Most servers runs some kind of linux based OS. They share a standard called POSIX, Positive Operating System Interface.

Windows is not a POSIX compatible OS. It works differently, making it a less desirable choice for web development.


Running Commands

We type in commands, and it will print out info as a response. It goes from the top down, so the latest info is at the bottom. The command line is where you type your commands. You’ll see some text followed by a blinking rectangle. The text is the prompt, which can give you some contextual info. treehouse is who I’m logged in as, and the wiggly horizontal line is the tilde, and is the directory we’re in. The $ tells us where the prompt ends and where our input begins.

Screen Shot 2014-10-24 at 1.41.46 PM

The ls command is short for list, and lists files. Type it in, then click enter to run it.

Screen Shot 2014-10-24 at 1.45.37 PM

The output gives two things. It means in the folder we’re in right now, there’s a folder called documents (in blue), and a file called hello.txt. Colors aren’t necessary, but can be helpful. Now let’s write ls -l. -l prints the long form of the list, giving us more info.

Screen Shot 2014-10-24 at 1.48.56 PM

The total 8 means there are 8 files in this folder that are not being listed by default. The line of letters and dashes specifies the permissions of this particular file. For drwxrwxr-x, d means directory, then there are three triplets of rwx, for read, write and execute. Each triplet means a differential level of people you can access them. The first triplet is for the user who created it. The second is a group, which is like a user but let’s you have multiple users in a group, an each file has a group associated with it. The third is for public permissions, and in this case you can see the w is omitted, so they can’t write.

For -rw-r—–, no one can execute it because it’s a text file.

There are then two treehouse’s list, the first being the user, and the second being the group. The 4096 and 416 is the size, listed in bytes. However, the 4096 isn’t the size of its contents, but rather this is a number they give to directories. Then, the date of the file and its name.

So why are the other 6 files not showing? If you start a file name with a dot, it will by default not show up in an ls listing. This is useful because it let’s us create hidden files. The -a option let’s us see those too. They’re usually configuration files made by other programs, and you usually don’t need to modify them.

Screen Shot 2014-10-24 at 1.59.50 PM

We can pass an argument as well. Currently we’ve been listing our current directory, but we can do any other one as well. /etc is the name of another folder.

Screen Shot 2014-10-24 at 2.02.30 PM

If we wanted to see them long form, we’d type ls -l /etc

Screen Shot 2014-10-24 at 2.04.07 PM

The user root is the admin of our computer, which we normally don’t use so we don’t accidentally delete an important system file. To clear the backlog of commands and responses, type clear.


Moving around the filesystem

Folders are usually called directories here. You’re always at some place in the folder tree when you’re in the command line. The pwd (print working directory) command displays your current position in the file system.

Screen Shot 2014-10-24 at 2.20.44 PM

In linux, your home directory is listed as home/yourusername. In Mac is users/yourusername. This is a special directory and it has its own shortcut, the ~.

The cd (change directory) command lets you change where you are. Type cd, then where you want to go. Some consoles have tab completion, where you start typing something and hit tab to complete it. Below, I hit doc then tab. If there are multiple options you can tab through them with tab. The trailing slash is in case you want to go into a directory inside of documents.

Screen Shot 2014-10-24 at 2.25.58 PM

Notice now our prompt has changed, and now displays ~/documents. If we check pwd it tells us we’re at home/treehouse/documents, and if we check this folder with ls there’s a new text file in here.

Screen Shot 2014-10-24 at 2.26.57 PM

Note that if we were to try and type cd documents now, it wouldn’t make any sense, because there’s no documents folder where we currently are in the directory. If we try, we get a response telling us there is none. -bash is the program we’re interacting with now, and is called our shell.

Screen Shot 2014-10-24 at 2.29.30 PM

That would be an example of a relative path. We can move around with an absolute path though. It starts with a / or a tilde, meaning we’re starting at the top, or the root, of our document tree. cd /home/treehouse takes us home.

Screen Shot 2014-10-24 at 2.32.50 PM

If we were at /home, and wanted to go to documents. We could write /home/treehouse/documents, or just use a tilde instead and do ~/documents.

Screen Shot 2014-10-24 at 2.35.19 PM

To go back to home we can just type cd ~.

Screen Shot 2014-10-24 at 2.36.19 PM

To go back down to documents form here, we can do cd documents. But what if we want to go back up? Simply type cd ...

Screen Shot 2014-10-24 at 2.37.59 PM

You can string this together, doing ../.. makes you go up two directories.

Screen Shot 2014-10-24 at 2.39.10 PM

If you typed home/treehouse/.., you’re actually saying you want to go up a level from the treehouse directory, which would be home again.

Screen Shot 2014-10-24 at 2.40.23 PM


Reading Files

So how do we see what’s inside that hello.txt file? We can use less, a program that displays the contents of a file. It takes as its argument the file you want to view. Notice we’re using a relative path since we’re where the file is at. We could do an absolute path as well and list out home/treehouse etc.

Screen Shot 2014-10-28 at 1.57.09 PM

Screen Shot 2014-10-28 at 1.57.55 PM

Notice how less takes up our entire screen/console. You can use the arrows to move up and down. The cursor has changed, there are now some commands we can use to move around. Spacebar will scroll one page at a time. (END) tells us it’s the end of the file. Pressing q at any point will quit and take you back to the command line.

Screen Shot 2014-10-28 at 1.59.36 PM

Fun fact – since less is based of the more program from the original Unix, you can type that as well, and it will run like less, but will try to emulate the older more program. These programs are called pagers, because they show one page at a time. The cat program will print the content of one or more files to the contents, and can concatenate files together. Notice how it just printed it out and took us back to the command line.

Screen Shot 2014-10-28 at 2.05.58 PM Screen Shot 2014-10-28 at 2.05.29 PMTo concatenate just list the files you want to do that to.

Screen Shot 2014-10-28 at 2.07.17 PMScreen Shot 2014-10-28 at 2.07.22 PM


Editing Files

nano is a text editor that works on the console, and is named after the old Unix program pico. Just type nano then the text you want to edit.

Screen Shot 2014-10-28 at 2.26.41 PMScreen Shot 2014-10-28 at 2.26.45 PM

It’s another full screen program. You can move around with arrows, type and click enter to make spaces. There are shortcuts at the bottom. The carrot means the control key. To get help, press control+G, which takes you to another screen with more commands.

Screen Shot 2014-10-28 at 2.31.34 PM

If you exit the editor and have made changes, it will ask you if you want to save.

Screen Shot 2014-10-28 at 2.32.22 PM

vim and Emacs are other, more powerful options, but have a steeper learning curve.


Moving and Deleting Files

Let’s say we want to rename a file hello.txt to hi.txt. Instead of thinking of it as renaming it, think of it as moving it from the hello.txt location to the hi.txt location. mv is the move command, and takes two arguments, from where we want to move something, and where we want to move it.

Screen Shot 2014-10-28 at 2.38.56 PM

To move it from one directory (remember, in the console that means a folder) to another), it works the same way. To move it into documents, just write that as the second argument with a / after it. It will move it from the current directory to the new one with the same file name.

Screen Shot 2014-10-28 at 2.41.22 PM

Note that ls documents/ didn’t move us into the documents directory, but rather just listed what was in there. To move it back, just switch the arguments. We can use tilde, since we’re going home/treehouse, .., to go up a directory, or finally ., which is the current directory we’re in.

Screen Shot 2014-10-28 at 2.43.55 PM

If we were to do this:

Screen Shot 2014-10-28 at 2.45.01 PM

It would not only move it to the documents directory but also rename the file to hello.txt.

mv also works for directories.

Screen Shot 2014-10-28 at 2.46.43 PM

Once again, to move it back we just switch them.

Screen Shot 2014-10-28 at 3.55.06 PM

The cp command copies things for you, leaving the original one in place. One again it takes two arguments, the first being what you want to copy, the second being want you want to name it/move it to.

Screen Shot 2014-10-28 at 3.57.27 PM

cp only works for files though. To copy a directory, you need to type -r, which will recursively copy all the things within the directory.

Screen Shot 2014-10-28 at 3.59.43 PM

We can see that it copied the file within it.

Screen Shot 2014-10-28 at 4.00.38 PM

The rm command removes a file or directory. Be very careful, there isn’t a way to undo this. It’s not like your OS that has a recycle bin.

Screen Shot 2014-10-28 at 4.03.15 PM

rm can’t remove a directory by itself. We need to use -r again.

Screen Shot 2014-10-28 at 4.06.06 PM

To make a directory, use the mkdir command, which stands for make directory. The argument it takes is the name of the directory/path you want to make.

Screen Shot 2014-10-28 at 4.07.30 PM

To make a directory inside of other ones. mkdir documents/notes/console wouldn’t work because there isn’t a notes directory yet. You can either make notes then console:

Screen Shot 2014-10-28 at 4.09.56 PM

Or, you can create a nested directory, however levels deep, by using -p with mkdir.

Screen Shot 2014-10-28 at 4.11.13 PM


Users and Permissions

Creating Users

You can set up multiple users, each with their own login, password and home directory. It’s a place for each user to put their stuff. There are files outside of the home directory that are used by the computer and typically not accessible to users.

Had to do some code to get this to work apparently.

Screen Shot 2014-10-28 at 4.18.20 PM

You can learn which user you are by looking at the command prompt (right now I’m treehouse), or by typing the command whoami.

Screen Shot 2014-10-28 at 4.19.51 PM

The adduser command let’s you add a new one. The argument it takes is the name of the user you want to add. However, you also need to be running in a special mode called sudo. It will then ask you for the password of the person you’re currently logged in as. In the example below we forgot the name argument the first time. It didn’t ask for the p/w again though because it was recently typed in.

Screen Shot 2014-10-28 at 4.23.39 PM

So it created a user mike, then a group mike, then a home directory for him, then copied some files to get it started. Now we put in the p/w for that user, in this case html5.

Screen Shot 2014-10-28 at 4.25.16 PM

Now we put some info in about the user, which isn’t mandatory. Then you can review the info to see if it’s correct and press Y for yes, n for no. Y is capitalized, which is a convention meaning it’s the default option if we don’t hit anything. So, if we want yes we don’t have to type it, we can just press enter.

Screen Shot 2014-10-28 at 4.27.50 PM

To switch to this new user, you can use the su (switch user) command. You enter their p/w, and then you’ll be then. Notice how the prompt has changed. We can use whoami to see that we’re mike, and pwd to see we’re in the home directory. When you switch to a new user, you remain in the same directory that you were in before, in this case home/treehouse, which is NOT mike’s home directory. To go there we type cd.

Screen Shot 2014-10-28 at 4.32.20 PM

So how do we switch back to the treehouse user? We can use su treehouse, or the exit command, which takes us out of the switched user that we were in before. Notice how we’re still in home/treehouse – where we were as mike has not affected where the treehouse user was.

Screen Shot 2014-10-28 at 4.34.37 PM


File Permissions

Read, Write and Execute (rwx) are self explanatory. Execute only comes into play with programs. They are defined for three different entities:

1. User (u) – who made it

2. Group (g) – each user has a primary group that matches their username, but can be a part of many different groups.

3. Other users (o) – for those who are not the user owner, or in the group.

They are abbreviated u, g and o, in that order, giving you 9 permissions for each file. ls -l let’s you see those permissions.

Screen Shot 2014-10-28 at 4.44.44 PM

The first digit tells you whether it’s a directory. Then u, g and o’s permissions in triplets. A dash means they don’t have that permission. The chmod command changes the permissions of a file or directory. Then the user and group names are listed. The txt file doesn’t have an x permission because it’s a text file.

It takes a few arguments, first who we’re changing this for (u,g,o). Then whether add or remove the specified permission, using a + for add or a – for remove. Then, the permission you want to change. Finally, the file or directory you want to change this for. Here, we gave o the permission to write on hello.txt.

Screen Shot 2014-10-28 at 4.49.44 PM

To add a permission for every, omit the first argument for ugo, then the change.

Screen Shot 2014-10-28 at 4.51.17 PM

To remove one:

Screen Shot 2014-10-28 at 4.52.20 PM

Permissions are usually represented using octal notation, which is base 8 instead of 10. When you add one to 7, the answer is still 8, but is represented by 10.

Screen Shot 2014-10-28 at 4.53.52 PM

It’s used because there are 8 possible combos of rwx. You’ll see three numbers written, corresponding to u, g, then o.

Screen Shot 2014-10-28 at 4.55.13 PM

To remember the values, each is assigned a value, just add them up. – is zero.

Screen Shot 2014-10-28 at 4.56.17 PM

You can use this with chmod. Right now hello.txt adds up to 666. To add x to each ugo, we can do 777.

Screen Shot 2014-10-28 at 4.58.56 PM

Another example.

Screen Shot 2014-10-28 at 5.00.46 PM


File Ownership

The chown command can change the owner of a file or directory. The arguments you put in are the new owner, then the file or directory. It won’t work in this case unless we put sudo, which will override any permissions. Here, we’ve reassigned hello.txt to mike.

Screen Shot 2014-10-28 at 5.17.49 PM

Since we’re signed in as treehouse, and now only have the group permissions, we can only read it now, and not write or execute it. So the cat command would work, but the nano one would tell us there’s no write permission, so we could change but wouldn’t be able to save with control+o.

Screen Shot 2014-10-28 at 5.21.02 PM

Now, when you try to exit it will still ask you if you’d like to save, but you can’t, so just choose No.

To change the user and the group, put a colon between them.

Screen Shot 2014-10-28 at 5.23.03 PM

If you wanted to read or write to it now you’d just have to su as mike, or reassign it back to treehouse.


Sudo

What if you want to change things but don’t have permission? There is a special user on every computer called root, who overrides every permission on every file and can do anything. If you know their password, you could su as them, but this is less than ideal, because it requires three steps, and being root can be dangerous if you do something by accident. It’s better to work as a normal user and only use the permissions when needed. It’s better to use the sudo command, which lets you run a command with the privileges of root. This is also better because you can give certain people permission to use sudo, rather than let them know the password to root.

Example: before when we were changing the ownership of hello.txt. When you first use sudo, it will require a p/w to be used. It’s not the password of the root user, but rather the users password. It’s a system that allows certain users to use it. This helps ensure that someone might not be logged in as you, and gives you a moment to think about whether you want to do the command. It won’t prompt you for the p/w for a specified amount of time.

Screen Shot 2014-10-28 at 5.33.01 PM

Fun shortcut – !! represents the last command that was run. It will print the command that was run, and also it’s output. Sometimes referred to as a bang. Good use case: If you forgot sudo for a command, you just can type sudo !! rather than having to recall it with up arrow and move the cursor over.

Screen Shot 2014-10-28 at 5.39.52 PM

Note that you can only use sudo if it’s been enable for you as a user.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s