Lab Exercise 2
create a "command" then use chmod, chown and continue working with other Real World commands
create a command - use chmod, chown, which, locate, cnf, zypper, cat, echo and command
Script started on Mon 08 Dec 2014 12:21:15 AM PST
Open a Linux or Cygwin Session and
type the items after the "-->"
(if your output differs then try to figure out why)
-----------------------------------------------
--> mkdir LAB; cd LAB
--> echo "this is a command"
you should see: this is a command
--> echo "this is a command" > tstcmd
--> cat tstcmd
this is a command
If you were to execute "tstcmd" would it do the same thing as the command line above?
In order for it to actually print out the statement, the file tstcmd will need to include the echo and quotes.
the echo command is the executable part of tstcmd to produce the statement.
----------------------------------------------------------------------------
to include the entire string, try:
--> echo "echo \"this is a command\"" > tstcmd
the \ escapes the shell's understanding of the special character " and allows it to be echoed instead of executed
now look at the file again:
--> cat tstcmd
echo "this is a command"
now the file will produce the statement "this is a command" if executed
----------------------------------------------------------------------------
ATTEMPTING TO "RUN" this new program or command called tstcmd.
if you type "tstcmd" at the prompt, what happens?
--> tstcmd
bash: /home/john/tstcmd: Permission denied
what does Permission denied mean?
try it again telling the shell to run it here...
------------------------------------------------
--> sh ./tstcmd
this is a command
that worked...
let's try using command, which ignores the shell functions that we just used.
------------------------------------------------
--> command tstcmd
bash: /home/john/tstcmd: Permission denied
what does Permission denied mean? here it is again
we've created a program that echoes a string, but it's not working.
We mentioned the which command before to tell us where a command was located.
------------------------------------------------
--> which tstcmd
which: no tstcmd in (/home/john/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr...
which doesn't find it...
examining our environment variables, let's look at our path statement:
--> env | grep -i path
MANPATH=/usr/local/man:/usr/share/man
XNLSPATH=/usr/share/X11/nls
PATH=/home/john/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/usr/bin/:/...
ALSA_CONFIG_PATH=/etc/alsa-pulse.conf
WINDOWPATH=7
QT_PLUGIN_PATH=/home/john/.kde4/lib64/kde4/plugins/:/usr/lib64/kde4/plugins/
the . is in our path but it didn't find it.
we also see other items in our grep... we could clean this up with the following command now that we know the PATH variable is uppercase:
------------------------------------------------
--> env | grep ^PATH=
PATH=/home/john/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/usr/bin/:/home/john/bin:/...
the up caret before PATH and the equal sign after help clearly identify the pattern we're searching for.
The truncated path statement has redundancies, we'll address how to deal with that in a later session.
which didn't find it, how about "locate"
------------------------------------------------
--> locate tstcmd
If 'locate' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf locate
locate is not loaded on SuSe by default, the error message gives us a hint. using sudo we can install it:
------------------------------------------------
--> sudo cnf locate
The program 'locate' can be found in following packages:
* mlocate [ path: /usr/bin/locate, repository: zypp (openSUSE-13.2-0) ]
* mlocate [ path: /usr/bin/locate, repository: zypp (repo-oss) ]
Try installing with:
zypper install mlocate
------------------------------------------------
--> zypper install mlocate
Root privileges are required for installing or uninstalling packages.
------------------------------------------------
--> sudo zypper install mlocate
Retrieving repository 'openSUSE-13.2-Update' metadata ......................................................................[done]
Building repository 'openSUSE-13.2-Update' cache ...........................................................................[done]
Loading repository data...
Reading installed packages...
Resolving package dependencies...
The following 2 NEW packages are going to be installed:
mlocate mlocate-lang
The following recommended package was automatically selected:
mlocate-lang
2 new packages to install.
Overall download size: 111.7 KiB. Already cached: 0 B After the operation, additional 389.3 KiB will be used.
Continue? [y/n/? shows all options] (y): y
Retrieving package mlocate-0.26-15.1.2.x86_64 (1/2), 62.9 KiB (143.3 KiB unpacked)
Retrieving: mlocate-0.26-15.1.2.x86_64.rpm .................................................................................[done]
Retrieving package mlocate-lang-0.26-15.1.2.noarch (2/2), 48.8 KiB (246.0 KiB unpacked)
Retrieving: mlocate-lang-0.26-15.1.2.noarch.rpm ............................................................................[done]
Checking for file conflicts: ...............................................................................................[done]
(1/2) Installing: mlocate-0.26-15.1.2 ......................................................................................[done]
Additional rpm output:
Updating /etc/sysconfig/locate...
(2/2) Installing: mlocate-lang-0.26-15.1.2 .................................................................................[done]
------------------------------------------------
--> locate tstcmd
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
------------------------------------------------
--> tstcmd
bash: /home/john/tstcmd: Permission denied
clearly we can not execute this file as configured.
Maybe, the file needs to be owned by root, since we're seeing a Permission denied error
--> chown root tstcmd
chown: changing ownership of ‘tstcmd’: Operation not permitted
------------------------------------------------
--> sudo chown root tstcmd
------------------------------------------------
--> ls -l tstcmd
-rw-r--r-- 1 root users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> tstcmd
bash: /home/john/tstcmd: Permission denied
------------------------------------------------
--> sudo chown john tstcmd
changing ownership back to john
------------------------------------------------
--> ls -l tstcmd
-rw-r--r-- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
so after all that, and it still will not execute as a command unless we invoke it from the shell.
we briefly talked about how file permissions had a bearing on which, locate and command and how the file needs to be executable.
------------------------------------------------
--> ls -al tstcmd
-rw-r--r-- 1 john users 18 Dec 8 00:21 tstcmd
the file tstcmd has the following permissions:
owner: read write -
4 2 1
group: read - -
other: read - -
the numeric value for this is 644, where 4+2 =6 or read + write = 6
let's try some changes and see what happens to the file and if it executes
------------------------------------------------
--> chmod u+x tstcmd
------------------------------------------------
--> ls -al tstcmd
-rwxr--r-- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> which tstcmd
/home/john/tstcmd
------------------------------------------------
--> tstcmd
this is a command
------------------------------------------------
--> chmod 755 tstcmd
------------------------------------------------
--> ls -al tstcmd
-rwxr-xr-x 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod o-x tstcmd
------------------------------------------------
--> tstcmd
this is a command
------------------------------------------------
--> ls -al tstcmd
-rwxr-xr-- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod g-x tstcmd
------------------------------------------------
--> ls -l tstcmd
-rwxr--r-- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod 706 tstcmd
------------------------------------------------
--> ls -l tstcmd
-rwx---rw- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod 705 tstcmd
------------------------------------------------
--> ls -l tstcmd
-rwx---r-x 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod 755 tstcmd
------------------------------------------------
--> ls -al tstcmd
-rwxr-xr-x 1 john users 25 Dec 8 00:23 tstcmd
so we've seen that in order for a command to execute without using the shell function we need to make it executable
with the chmod command. The chown command changed ownership but did not allow it to execute unless the x bit was set.
---------------------------------------------
LINUX Commands - real world
for the following commands type the example provided, or execute man and look at the options, or type the command with --help
each of these commands has a real world use, or is of importance - if an example is not provided it'll be listed with (not used very often).
==============================================================================
alias Create an alias •
alias froglegs="ls -aRlt | more"
awk Find and Replace text, database sort/validate/index
awk -F: '($3 == "0") {print}' /etc/passwd
==============================================================================
bash GNU Bourne-Again SHell
no example needed
bg Send to background
this is used after using ^Z (ctrl Z) to halt a program, and then put in the background
==============================================================================
cal Display a calendar
cal -3 ; cal 1974
cat Concatenate and print (display) the content of files
cat /etc/passwd
cd Change Directory
cd /usr/bin ; cd
chmod Change access permissions
touch TESTTHIS ; chmod 777 ; ls -l TESTTHIS ; chmod (make up a number and look at the file)
chown Change file owner and group
not used very often, example above
chkconfig System services (runlevel)
this is a sysadmin thing, try it, with and without sudo
clear Clear terminal screen
clear
cmp Compare two files
echo 1234 > file1 ; echo 123 > file2 ; cmp file1 file2
command Run a command - ignoring shell functions •
shown above, rarely used
cp Copy one or more files to another location
cp file2 file3
cut Divide a file into several parts
not used very often, examples in scripts
==============================================================================
date Display or change the date & time
date
dd Convert and copy a file, write disk headers, boot records
this is an advanced command, do NOT attempt to use it
df Display free disk space
df -h
diff Display the differences between two files
diff file1 file2
dmesg Print kernel & driver messages
dmesg | more ( or if permission denied: ) sudo dmesg | more
du Estimate file space usage
du -sh /tmp
==============================================================================
echo Display message on screen •
shown above
eject Eject removable media
insert a CD or DVD then type eject
env Environment variables
env | more ; env | grep -i PS
exit Exit the shell
export Set an environment variable
used in .bashrc
==============================================================================
fdisk Partition table manipulator for Linux
fdisk -l (didn't work? try: ) sudo fdisk -l
fg Send job to foreground
related to jobs and bg not used much
file Determine file type
file /etc/passwd ; file /usr/bin/ls
find Search for files that meet a desired criteria
find /etc -type f -name passwd -print
free Display memory usage
free
fsck File system consistency check and repair
used on UNMOUNTED filesystems - advanced command don't use
fuser Identify/kill the process that is accessing a file
advanced command, not used very often, but if you try to kill a proc and you can't, fuser tells who is using it
==============================================================================
grep Search file(s) for lines that match a given pattern
grep PATH .bashrc
==============================================================================
head Output the first part of file(s)
head .bashrc
history Command History
history
hostname Print or set system name
hostname
==============================================================================
ifconfig Configure a network interface
/sbin/ifconfig -a
ifdown Stop a network interface
advanced command, don't attempt
ifup Start a network interface up
advanced command, don't attempt
==============================================================================
jobs List active jobs •
jobs (nothing should show up... but try: xeyes & ; jobs
==============================================================================
kill Stop a process from running
advanced... we'll cover later
killall Kill processes by name
advanced... we'll cover later
==============================================================================
less Display output one screen at a time
less is more
ln -s Create a symbolic link to a file
ln -s .bashrc MYBASHRC ; ls -al
locate Find files
locate ls
ls List information about file(s) (ls -al)
ls -alR /
lsof List open files
lsof
==============================================================================
man Help manual
man man
mkdir Create new folder(s)
mkdir bin
more Display output one screen at a time
more is less
mount Mount a file system
advanced we'll cover later
mv Move or rename files or directories
mv MYBASHRC CopyofBashrc
==============================================================================
netstat Networking information
netstat -a
nice Set the priority of a command or job
DO NOT USE THIS COMMAND - very advanced... can crash the system
nohup Run a command immune to hangups
not used very often - keeps command running if the session is disconnected, useful during dialup days
nslookup Query Internet name servers interactively
nslookup google.com (if you're behind a firewall may not work, try a known system on your network)
==============================================================================
o
==============================================================================
passwd Modify a user password
use to change your passwd
ping Test a network connection
ping (some device or server) (if you're behind a firewall or NAT you won't be able to hit outside IPs) (may need to use the -4 option)
ps Process status
ps -ef | more
pwd Print Working Directory
cd /etc/sysconfig ; pwd ; cd /tmp ; pwd ; cd ; pwd
==============================================================================
==============================================================================
read Read a line from standard input •
used in scripts - advanced
reboot Reboot the system
DO NOT DO THIS UNLESS YOU MEAN IT. :)
rm Remove files
touch nukethis ; rm nukethis (answer yes or say n and then: rm -f nukethis
rsync Remote file copy (Synchronize file trees)
we'll cover this soon... very nice, but complex tool
==============================================================================
scp Secure copy (remote file copy)
we'll cover this later... very nice tool, but requires two systems;
scp file someothersystem.xxx:/home/user/ or scp -r someothersystem.xxx:/home/user/dir .
sed Stream Editor
see: removing spaces and special characters
shutdown Shutdown or restart linux
DO NOT DO THIS UNLESS YOU MEAN IT. :)
sleep Delay for a specified time
date ; sleep 20 ; date
sort Sort text files
cat /etc/passwd | tee -a passwd-notsorted ; sort /etc/passwd | tee -a passwd-sorted ; diff passwd-notsorted passwd-sorted
ssh Secure Shell client (remote login program)
ssh someserver w
su Substitute user identity
su - billybob
sudo Execute a command as another user
sudo su -
==============================================================================
tail Output the last part of file
tail /etc/passwd
tar Store, list or extract files in an archive
tar cvf passwd.tar /etc/passd
tee Redirect output to multiple files
cat /etc/passwd | tee -a passwd-notsorted ; sort /etc/passwd | tee -a passwd-sorted ; diff passwd-notsorted passwd-sorted
test Evaluate a conditional expression
will cover later
time Measure Program running time
time ls -alR /etc (or / if you have time...)
touch Change file timestamps
touch this-is-an-empty-file ; ls -al
top List processes running on the system
top
traceroute Trace Route to Host
route ; trace route (gateway)
tr Translate, squeeze, and/or delete characters
see: removing spaces and special characters
==============================================================================
ulimit Limit user resources •
ulimit
umask Users file creation mask
umask
umount Unmount a device
ONLY USE TO unmount a device...
unalias Remove an alias •
unalias froglegs
uname Print system information
uname -a
uniq Uniquify files
examples provided in lab 1
uptime Show uptime
uptime ; w ; top
useradd Create new user account
advanced sys admin task
userdel Delete a user account
advanced sys admin task
usermod Modify user account
advanced sys admin task
==============================================================================
vi Text Editor
vi testthistool
vmstat Report virtual memory statistics
vmstat
==============================================================================
wc Print byte, word, and line counts
cat /etc/passwd | wc -l
whereis Search the user's $path, man pages and source files for a program
whereis .bashrc
which Search the user's $path for a program file
which ls
while Execute commands
used in scripts
who Print all usernames currently logged in
who
whoami Print the current user id and name (`id -un')
whoami ; who am i
wget Retrieve web pages or files via HTTP, HTTPS or FTP
wget http://someserver/somefile
==============================================================================
xargs Execute utility, passing constructed argument list(s)
see find examples
==============================================================================
==============================================================================
zip Package and compress (archive) files.
==============================================================================
. Run a command script in the current shell
!! Run the last command again
### Comment / Remark
==============================================================================
Script done on Mon 08 Dec 2014 12:34:16 AM PST
================================================================================
john meister, lab manager, mstm
IT systems design and integration specialist / Linux, UNIX and lesser OSes
================================================================================
|