doc - IT 210

advertisement

Using the command line in Linux (Unix)

A few exercises

Ref to “Intro to the Command Line” (in Lab 1), which you have already seen

Some command-line fun

Startup:

Everybody start booting up Linux on your computer (Lab computer/Virtualbox, or if you have finished lab 1, on your own Linux system).

Experts?

If you are already adept at the command line solve this problem. In 15 minutes we will ask you to present your working solution. Winner gets a candy bar

PROBLEM:

Every time you log on, your computer is assigned an IP address. Sometimes you need to know what this

IP address is. Write a (set of) command(s) to display your current IP address (and nothing else). Make this happen automatically every time you log on.

For the rest of the class:

If you don’t understand that problem then let’s take a tour of the command line and try some exercises, entertainments and tricks.

Is everyone logged on?

Navigation

Firstly: whoami and where am I (pwd)

What’s here? Use ls but also ls –al (set up alias ll=”ls –al”)

[what’s the difference? Hidden files start with’.’, long ‘l’ gives more info] also tree (if installed--apt-get install tree)

Now navigate to root with cd / (NOTE FORWARD-SLASH IN Linux)

… and look around with ls, cat, less etc.

Many standard directories here (/dev—all your devices like HDD, DVD, ramdisks etc.)(/bin – lots of executables, /home- users folders) See links on class page for more detail. /proc is very interesting to explore)

… also tree (USE OF Ctrl-c TO ABORT A COMMAND-LINE COMMAND)

Use of clear to clean up the screen

Play with sudo

Who is allowed to use sudo? The list is in /etc/sudoers, so display with less /etc/sudoers (Permission denied)

Why is permission denied? ls –l /etc/sudoers

PERMISSIONS and ATTRIBUTES - r- - r- - - - - root root <other stuff>

Format -rwx rwx rwx Only the owner and the group have read permissions. No-one has write or execute permissions.

So give ourselves root permissions with sudo  sudo less /etc/sudoers <requires password>

Combining Commands

cd /proc <lots of interesting stuff in /proc – explore carefully with cat>

cat meminfo Now we just want to pull out the bit we’re interested in

cat meminfo | grep Mem (note case-sensitive) <brief comment on grep>

Let’s do this for network stuff

netstat tells us lots about the system but ifconfig tells us about our connections

ifconfig eth1 | grep addr

Clean it up a bit

ifconfig eth1 | grep inet | grep –v inet6 | cut -d ":" -f 2 | cut -d " " -f 1 (research cut and grep –v on your own)

Go home: cd ~ ( DISCUSS USE OF TILDE as a folder reference)

USING SECURE COPY TO COPY TO A REMOTE COMPUTER.

Create a junk file with some random text: echo “really important source code” > my.c

Now copy it to your J: drive with secure copy (scp): scp my.c rhelps@ssh.et.byu.edu:/auto/fsa/rhelps

MONITORING AND KILLING PROCESSES

xeyes <note the ./>

Note that we can kill a running proves with Ctrl-C – but there is a more elegant way

Open a second terminal and find it with: ps –ax | grep xeyes

<Note the pid. Note also the grep command is a process too>

Kill it: kill nnnn <where nnnn is the pid from the previous grep>

OTHER STUFF

Ctrl-C is your escape hatch.

Can pass stuff from one command to another. EG netstat shows network status. Grep parses text (or searches for stuff), ‘|’ is a “pipe” netstat | grep ESTABLISHED

Download