Linux Questions

From CS Support
Revision as of 13:13, 25 November 2014 by Sgsax (talk | contribs)
Jump to: navigation, search

NOTE: This document borrows heavily from the excellent documentation found in the Beocat wiki. Please have a look there for more details and in-depth examples.

How do I get started with Linux?

Getting Connected

To login remotely to CIS Linux systems, you first need an "SSH Client". SSH, short for "secure shell", is a protocol that allows secure communication between two computers. We recommend the following.

  • Windows
    • PuTTY is by far the most common SSH client, both for CIS and in the world.
    • MobaXterm is a fairly new client with some nice features, such as being able to SCP/SFTP (see below), and running X (Linux graphical desktop).
    • Cygwin is for those that would rather be running Linux but are stuck on Windows. It's purely a text interface.
  • Macintosh
    • OS-X has SSH a built-in application called "Terminal". It's not great, but it will work for most CIS users.
    • iTerm2 is the terminal application we prefer.
  • Others
    • There are many SSH clients for many different platforms available. While we don't have experience with many of these, any should be sufficient for access to CIS Linux systems.

You'll need to connect your client (via the SSH protocol, if your client allows multiple protocols) to cislinux.cis.ksu.edu.

For command-line tools, the command to connect is

ssh username@cislinux.cis.ksu.edu

Your username is your CIS login (usually the same as your K-State eID) and the password is your CIS password (which is only the same as your eID password if you set it manually or using our password reset page).

Note: When you type your password, nothing shows up on the screen, not even asterisks.

The first time you log into a new host, you will be asked to confirm adding the SSL signature to your local repository. Just say "yes" in whatever appropriate form when asked.

You'll know you are successfully logged in when you see a prompt that says

username@hostname:~$

where hostname is the name of the machine you've logged into (currently either 'cougar' or 'viper') and username is your eID username

Transferring files (SCP or SFTP)

Usually, one of the first things people want to do is to transfer files into or out of their CIS account. To do so, you need to use SCP (secure copy) or SFTP (SSH FTP or Secure FTP). Again, there are multiple programs that do this.

  • Windows
    • Putty (see above) has PSCP and PSFTP programs (both are included if you run the installer). It is a command-line interface (CLI) rather than a graphical user interface (GUI).
    • MobaXterm (see above) has a built-in GUI SFTP client that automatically changes the directories as you change them in your SSH session.
    • FileZilla (client) has an easy-to-use GUI. Be sure to use 'SFTP' mode rather than 'FTP' mode.
    • WinSCP is another easy-to-use GUI.
    • Cygwin (see above) has CLI scp and sftp programs.
  • Macintosh
    • FileZilla is also available for OS-X.
    • Within terminal or iTerm, you can use the 'scp' or 'sftp' programs.
  • Linux
    • FileZilla also has a GUI linux version, in additon to the CLI tools.

Using a Command-Line Interface (CLI)

You can safely ignore this section if you're using a graphical interface (GUI).

Example

Transfer a file called myfile.txt in your current folder to your home directory on CISLinux.

I am using my username ("sgsax") here. You should replace this with your own when you run these commands.

Using SCP:

scp myfile.txt sgsax@cislinux.cis.ksu.edu:
Password: (type your password here, it will not show any response on the screen)
myfile.txt                                                                            100%    0     0.0KB/s   00:00

Note the colon at the end of the 'scp' line.

Using SFTP:

sftp sgsax@cislinux.cis.ksu.edu
Password: (type your password here, it will not show any response on the screen)
Connected to cislinux.cis.ksu.edu.
sftp> put myfile.txt
Uploading myfile.txt to /home/s/sgsax/myfile.txt
myfile.txt                                                                            100%    0     0.0KB/s   00:00
sftp> exit

SFTP is interactive, so this is a two-step process. First, you connect to CISLinux, then you transfer the file. As long as the system gives the sftp> prompt, you are in the sftp program, and you will remain there until you type 'exit'.

For more examples of advanced usage, please visit the Beocat wiki.

Linux Lingo

Here are a few basic terms we use when referring to "things" on a Linux system.

Term Definition
Directory A "Folder" in Windows or OS-X terms. A location where files or other directories are stored. The current directory is sometimes represented as . and the parent directory can be referenced as ..
Home Directory
Shell The interface or environment under which you can run commands. For most users, your shell is bash
SSH Secure Shell. A protocol that encrypts data and can give access to another system, usually by a username and password
SCP Secure Copy. Copying to or from a remote system using part of SSH
path The list of directories which are searched when you type the name of a program. There is a section below on this
ownership Every file and directory has an user and a group attached to it, called its owners. These affect permissions.
permissions The ability to read, write, and/or execute a file. Permissions are based on ownership
switches Modifiers or options to a command-line program, usually in the form of -(letter) or --(word). Multiple single-letter switches may frequently be combined if needed for a command
pipes and redirects Changes the input (often called 'stdin') and/or output (often called stdout) to a program or a file

Basic Linux Commands

Here are some of the most common commands you will be using:

Command Function
ls list files
cd change directory
mkdir <dirname> make a directory called <dirname>
rmdir <dirname> remove or delete a directory called <dirname>
cat <filename> print the contents of <filename> to the screen
cp <source> <destination> copy from <source> to <destination>
mv <source> <destination> move or renamce from <source> to <destination>
touch <filename> create an empty file if it doesn't exists or update the last modified timestamp if it does
file <filename> show some information about <filename>, such as what type of file it is
nano edit files (probably the easiest text-based editor on our systems)
man user manuals

If you need more details on how to use any of these, you can type man cmd where "cmd" is the name of the command you want more information about.

General CLI Notes

  • Linux interprets everything you type in a case-sensitve manner. This means upper- and lower-case letters matter, both in commands and filenames. e.g. "Documents" is not the same as "documents".
  • Your shell does not deal with spaces in filenames very nicely. I would strongly discourage you from doing so. If you must use spaces in your filenames, you will need to enclose them with double-quotes (") to interact with them on the command line. Other special characters you should stay away from in filenames include $, #, ;, \, ~, <, >, |, `, [, ], {, }, (, ), and &. These all have special meanings in bash (and other shells) and could result in unexpected (or damaging) behavior of your commands.
  • Tab-completion is your friend. No need to type that very long directory or filename, just hit the <Tab> key once you've typed enough unique letters and the shell will complete the name as it matches in the current directory. You can even do this multiple times for any filename or path as you type it.
  • Linux has no concept of "file extensions". Most GUI desktops will try to interpret them if you use typical ones (.pdf, .doc, .txt, etc), but there are no "rules" about what you name files. Use your own discretion. As with all things, consistent naming will keep you out of trouble later.

Some Practical Examples

Output is truncated in some of these. More can be found on the Beocat wiki.

Navigating and listing directories

  • What files and directories are in the directory I am currently in?

testacct@viper:~$ ls
Application Data  Favorites  My Music     Public     Videos
Cookies          foo        My Pictures   Sent       workspace
Desktop          Ghost.pif  Pictures      steamtest.vmf  xauth
desktop.ini      Maildir    procmail-log  Templates
Documents        Music      procmailrc    Trash

  • No, really, show me all the files.

testacct@viper:~$ ls -a
.                  .gnome2                         .nx
..                 .gnome2.old                     .openwin-menu
.AbiSuite          .gnome2.old.old.old             .phoenix
.acrobat           .gnome2_private                 Pictures
.adobe             .gnome2_private.old             .pinerc
Application Data   .gnome2_private.old.old.old     .pki
.bash_history      .gnome.old.old.old              procmail-log
.bash_profile      .gnupg                          procmailrc
.bash_profile.bak  .gstreamer-0.10                 .profile
.bashrc            .gstreamer-0.8                  .profile.bak
.bashrc.bak        .gtk-bookmarks                  Public
.cache             .gtkrc-1.2-gnome2               .pulse
<...>

-a is for "all files"
  • Can I have some more useful info about those files?

testacct@viper:~$ ls -la
total 380
drwxr-xr-x  70 testacct testacct_users  4096 Oct  2 15:56 .
drwxr-xr-x 199 root     root            8192 Aug 26 18:10 ..
drwx------   2 testacct testacct_users    28 Aug 28  2008 .AbiSuite
drwxr-x---   2 testacct testacct_users    18 Jul 23  2004 .acrobat
drwx------   5 testacct testacct_users   115 Aug 28  2008 .adobe
drwxrwx---   4 testacct testacct_users    39 Aug 21  2007 Application Data
-rw-------   1 testacct testacct_users  6608 Aug 25 14:23 .bash_history
-rw-r--r--   1 testacct testacct_users   533 Sep  3  2009 .bash_profile
-rw-r-----   1 testacct testacct_users   279 Sep  3  2009 .bash_profile.bak
-rw-r--r--   1 testacct testacct_users  1733 Sep  3  2009 .bashrc
-rw-r-----   1 testacct testacct_users  2532 Sep  3  2009 .bashrc.bak
drwx------  34 testacct testacct_users  4096 Aug 25 14:32 .cache
drwxr-xr-x   7 testacct testacct_users    72 Aug  8  2004 .cfagent
drwx------   3 testacct testacct_users    20 Aug 28  2012 .compiz
drwxr-xr-x  22 testacct testacct_users  4096 Jul 24 16:40 .config
drwx------   7 testacct testacct_users   155 Jan  8  2010 .config.bak
drwxrwx---   2 testacct testacct_users    22 Aug 21  2007 Cookies
<...>

-l is for "long listing"
-la is the equivalent of -l -a
  • Can I get that with human-readable file sizes in bytes?

testacct@viper:~$ ls -lah
total 380K
drwxr-xr-x  70 testacct testacct_users 4.0K Oct  2 15:56 .
drwxr-xr-x 199 root     root           8.0K Aug 26 18:10 ..
drwx------   2 testacct testacct_users   28 Aug 28  2008 .AbiSuite
drwxr-x---   2 testacct testacct_users   18 Jul 23  2004 .acrobat
drwx------   5 testacct testacct_users  115 Aug 28  2008 .adobe
drwxrwx---   4 testacct testacct_users   39 Aug 21  2007 Application Data
-rw-------   1 testacct testacct_users 6.5K Aug 25 14:23 .bash_history
-rw-r--r--   1 testacct testacct_users  533 Sep  3  2009 .bash_profile
-rw-r-----   1 testacct testacct_users  279 Sep  3  2009 .bash_profile.bak
-rw-r--r--   1 testacct testacct_users 1.7K Sep  3  2009 .bashrc
-rw-r-----   1 testacct testacct_users 2.5K Sep  3  2009 .bashrc.bak
drwx------  34 testacct testacct_users 4.0K Aug 25 14:32 .cache
drwxr-xr-x   7 testacct testacct_users   72 Aug  8  2004 .cfagent
drwx------   3 testacct testacct_users   20 Aug 28  2012 .compiz
drwxr-xr-x  22 testacct testacct_users 4.0K Jul 24 16:40 .config
drwx------   7 testacct testacct_users  155 Jan  8  2010 .config.bak
drwxrwx---   2 testacct testacct_users   22 Aug 21  2007 Cookies
<...>

  • Change directory to `Documents`

testacct@viper:~$ cd Documents
testacct@viper:~/Documents$

  • Change directory to `foo` that is in `Documents`

testacct@viper:~$ cd Documents/foo
testacct@viper:~/Documents/foo$

  • Change back to the parent directory of the one I am currently in

testacct@viper:~/Documents/foo$ cd ..
testacct@viper:~/Documents$

  • Change back to my home directory

testacct@viper:~/Documents/foo$ cd
testacct@viper:~$

or

testacct@viper:~/Documents/foo$ cd ~
testacct@viper:~$

or

testacct@viper:~/Documents/foo$ cd $HOME
testacct@viper:~$

  • Make a directory here called `bar`

testacct@viper:~$ mkdir bar

  • Make a directory here called `bar` and contains another directory called `baz`

testacct@viper:~$ mkdir -p bar/baz

-p is for "parents", or "create parent subdirectories as needed to get this full path created"
  • Remove an empty directory here called `bar`

testacct@viper:~$ rmdir bar

  • Remove a directory here called `bar` and all of its contents, no matter how many sub-directories or files it contains (Potentially very dangerous as it will not ask you for confirmation).

testacct@viper:~$ rmdir -rf bar

  • -r for "recursive" and -f for "force"

Working with files

  • Show me the contents of the file `lorem_ipsum.txt`

testacct@viper:~$ cat lorem_ipsum.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus placerat,
odio vel ultricies commodo, lectus mi ornare enim, in ornare risus lacus ac
ipsum. Sed ac dui nec risus maximus porttitor. Quisque at lacinia sem, at
blandit ex. Cras gravida felis in pretium tincidunt. Etiam nec hendrerit orci,
eget scelerisque est. Pellentesque ultricies non neque interdum euismod.
Quisque ac ante eu mauris eleifend commodo vitae id erat.

  • Create an empty file named `empty.txt`

testacct@viper:~$ touch empty.txt

  • Copy the file `foo.c` to a new file called `bar.c`

testacct@viper:~$ cp foo.c bar.c

  • Rename or move the file `bar.c` to `baz.c`

testacct@viper:~$ mv bar.c baz.c

  • Delete or remove the file `baz.c`

testacct@viper:~$ rm baz.c

  • What the heck kind of file is `win-cis-64`

testacct@viper:~$ file win-cis-64
win-cis-64: PNG image data, 64 x 64, 8-bit/color RGBA, non-interlaced

What are "Linux file permissions"?

All student and faculty files in the CIS department are stored on a Linux file server. However, not everyone needs or should have access to every file. Linux file permissions are how the file server decides who can access which files and what kind of access is permitted.

Ownership

Every file in Linux is assigned an user owner and a group owner. These form the basis of file security.

Each group may contain zero or more users. In the CIS department, every user has a special group which contains only their user (e.g., if your username were `foobar`, your group would be `foobar_users`).

You may not change the user ownership of your files. If you find that some of your files are owned by the wrong user, you will need to contact the systems staff to get that problem correct.

You may, however, change the group ownership of your files (or directories). You are only able to change a file's group ownership to another group to which you belong. You may change group ownership with the `chgrp` program:

chgrp foobar_users file.txt
chgrp foobar_users dir

File Mode

The second component of Linux file ownership is called "file mode." The file mode determines what access the user owner, group owner, or other user has to a file.

There are three kinds of access: read, write, and execute. The access granted by each of these depends upon whether they are applied to a directory or to a file. For files, read access gives the user permission to see the contents of the file, write access gives the user permission to change the contents of the file, and execute access gives the user permission to run the file as a program. For directories, read access gives the user permission to see what files are stored in that directory, write access gives the user permission to create files in that directory, and execute access allows the user to enter that directory (note that directory execute is required for read or write to mean anything).

Viewing File Permissions

To view the permissions on a file or directory you can use the `-l` option to the file list command, `ls`::

foobar@viper projects$ ls -l
total 4
drwx------   3 foobar foobar_users 4096 Mar 16 19:51 bin/
drwxrwxr-x   4 foobar foobar_users 4096 Mar 16 19:51 lib/
-rw-r--r--   1 foobar foobar_users  667 Mar 16 19:51 foo.txt
-rwxrwxr-x   1 foobar foobar_users  408 Mar 16 19:51 a.out

Here we see two directories and two files. The permissions are shown in the first column of the listing. The user owner is the third column and the group owner is the fourth column. The permissions are shown via the 10-character string. The first character (either `d` or `-` here) tells us what kind of thing is shown: `d` for directory and `-` for regular file (there are other kinds of "things" in a Linux file system, but those are beyond the scope of this discussion). The rest of the columns show the file mode.

The file mode is divided into three sections. The first applies to the user onwer, the second to the group owner, and the third to all other users. These fields are then divided into three letters, one for each permission granted: `r` for read, `w` for write, and `x` for execute.

The first row above shows a directory that is only accessible by user `foobar`. This user is allowed to change to the `bin` directory, see what files are in that directory, and allowed to put new files into that directory. The second row shows a publically accessible directory that is both user and group writable. Here anyone can change to and see the contents of the `lib` directory. However, only `foobar` and members of `foobar_users` are allowed to create files in that directory.

The third and forth rows show files. The first file is readable by everyone, but only writeable by `foobar`. The second file is readable and executable by everyone, but is only writable by `foobar` or members of `foobar_users`.

Changing File Mode

If you need to change the mode of a file or directory, you may do so with the `chmod` command. The `chmod` command takes two or more options. The first option is the permission changes you wish to make and the rest of the arguments are the files to change. (The actual command is a lot more flexible, but you may find out more about that by typing `man chmod`.)

The permission changes are specified in three parts: (1) whose permissions are being changed, (2) stating that permissions are being added or removed, and (3) what access is being granted or revoked.

The first, or "who", part of the options are specified with one or more letters: `u` for user, `g` for group, `o` for other, and `a` for all.

The second part is specified with either the plus (`+`) sign to grant permission or the minus (`-`) sign to revoke permission.

The third part is specified with the same letters as were used by `ls` when viewing permissions: `r` for read, `w` for write, and `x` for execute.

For example:

chmod a+rx script.pl
chmod og-w file.txt
chmod ug+rw source.c

The first line above would grant read and execute permission to all users on the file named `script.pl`. The second line would revoke write permissions on `file.txt` for other users and the group owner. The third would grant read and write permissions to the user owner and group owner on `source.c`.

Notice that this doesn't say anything about the existing permissions. For example, in the second line above, the user may not be allowed to write to the file either if the user was never granted that permission.

Absolute File mode

Advanced users may want to specify the mode quickly and exactly. This can be done by specifying the numeric file mode directly. The file mode may be specified as an octal number where the last three digits represent each of the three levels of access, user, group, and other, respectively. Each digit then specifies all three read, write, and execute for each of the three fields. Read is specified by the digit 4, write by 2, and execute by 1. Adding these together will get the overall permission.

For example:

chmod 755 script.pl
chmod 644 file.txt
chmod 660 source.c

After running these commands, we would see something like the following listing (note the first column especially):

foobar@viper src$ ls -l
total 3
-rwxr-xr-x   1 foobar foobar_users   432 Mar 10 12:40 script.pl
-rw-r--r--   1 foobar foobar_users  2590 Mar  8 17:22 file.txt
-rw-rw----   1 foobar foobar_users   596 Mar 12  7:04 source.c

Again see the man-page for `chmod` for details.

General Note About Directories

Linux requires that directories have the "execute" permission enabled. Without this permission, you will not be able to display the contents of a directory, even if you have "read" permissions enabled.

What about the /tmp directory?

Whenever working on a Linux system in the department, please be aware of the `/tmp` directory since it allows you to store files for a short time without affecting your quota. If you need to download a large file or work with some large files that you don't need to work with for long and can't fit into your home directory, you can place these files in `/tmp`, which is stored on the local disk of the current machine.

However, there are some things you should be aware of when using the the `/tmp` folder. First, filling up all space in the `/tmp` folder is a bad idea. You can cause certain programs on the system to freeze up if there is no room left on the drive, which may make working on that computer difficult. Second, the temporary folder is temporary and is cleaned out automatically once every hour. You should not rely on this directory for anything important.

I can't login to the Linux lab

Problem: When you try to login to a Linux lab desktop, the screen goes black and then reverts immediately to the login screen. No errors are displayed.

Cause: This is due to corruption or incompatibility of one of your X.org configuration files. The solution is to remove this file.

Resolution:

  1. Press <Ctrl>-<Alt>-<F1> to switch to a local virtual console and login using your regular credentials.
  2. At the prompt (ending with a $ character), type the following and press <Enter>
    rm .Xauthority
  3. Logout using either logout or exit
  4. Switch back the the GUI login screen by pressing <Ctrl>-<Alt>-<F7>

You should now be able to login as usual.