Category: Administering Linux

Using vi

By peter, September 21, 2009

VIM
Or “vi” (vim being the modern version of vi)


The advantage of VIM over other text editors is that it runs at the lower run levels.

To use “vi” you need to open up a “shel” prompt and type “vi” followed by the name and path of the document you want to edit and press enter.

Example of editing a file using vi

Example of editing a file using vi

Once in “vi” you can use the following to edit the document:

* Press the “Insert” key on the keyboard to edit the document.

Other insert keys are as follows:

a” Inserts after the cursor
A” Inserts at the end of the line
i” Inserts at cursor
I” Inserts at beginning of the line
o” new line below cursor
O” new line above cursor

Once editing is complete you need to press “Esc” then “:wq” to exit and save changes. Or once editing is complete you need to press “Esc” then “:q!” to exit without saving changes.
If you just “viewed” the document without changing anything you can press “Esc” then “:q” to exit.


Some other commands in “vi” are as follows:

NB To use the following commands you need to press “Esc” first.

yy
Yank (copy). Copy a few lines at once by putting the number of lines you want to copy before yy. Eg 4yy

dd
Delete or cut. Put the number of lines to cut or delete before dd.

p
Paste

/wordtosearch
Forward search

?wordtosearch
Backward search

~
Change case

u
Undo

:%s/originalword/newword

Change % to “start line”, “s” to “end line” to change in only certain lines

Basic Linux Commands

By peter, September 13, 2009

Log on, off, shutdown and reboot (command in italics)


exit
log off

shutdown -h now message
Other options -r, -c. Use time instead for “now”, eg 20 for 20 mins. Can leave out message.

reboot
Reboot

intit 0
Shutdown

init 6
Reboot

Basic commands (command in italics)
Here are some of the basic commands that will help you to navigate while working in the “shell”.
Please be aware that Linux is very case sensitive

pwd
Print working directory

ls
List files and folders

cd
Change directory (as with Dos)

cd ..
Back

cd ~
Change to home directory

cd /
Change to root directory

history
All resent commands used


TIP:: You can use the “tab” key to complete a command.
With SuSE you can type the first few letters of a command and then use the “page up” and “page down” key to give you the last command typed starting with those letters. This is nice because it will give you the whole syntax.
With most other distros you can use “Ctrl” + “r” to search for commands used.

The following are some of the basic “shell” commands you might be using:

less
Allows you to see the content of a file page by page. (even “zipped” files) eg “less /etc/hosts” you can also “pipe” an output to “less” for example, see the difference when you type “ls” on the “etc” directory and when you type “ls | less” , press “enter” to see more and type”q” to escape. The “|” or “pipe” is the thing on your keyboard that looks like two “dashes” above one another.

more
Similar to “less”

cd
Change directory eg “cd /home”

cd ..
Go back a directory

vi
Open the vi Text editor Eg “vi /etc/samba/smb.conf”

ls
List content (dir also works)

ls -al
List content and shows permissions

pwd
Shows you the working directory where you are currently working from

ifconfig
Shows the IP configuration (like �ipconfig� in Microsoft)

netstat
Displays the status of all open sockets

traceroute
Displays the route to a host (like “tracert” in Microsoft)

df
Shows you disk usage and where partitions are mounted

du
Shows disk usage by folders and files

free
Shows free memory

uptime
Tells you how long the system has been up

cp text.txt /folder/test.txt
Copy a text doc You can use the�*� as a wild card as you can in DOS

rm text.txt
Delete a text doc. To remove entire directories along with the files you need to use the “rm” command with the “-R” switch. To force you can use the “-f”. To require conformation when deleting use “-i”, although some distros have “rm” as an alias for “rm -i”

rmdir
(Delete an empty directory)

mv text.txt /foldername/backup.txt
(Moves a text doc)

ifconfig
Shows the IP configuration (like “ipconfig” in Microsoft)

netstat
Displays the status of all open sockets

traceroute
Displays the route to a host (like �tracert� in Microsoft)

df
Shows you disk usage and where partitions are mounted

du
Shows disk usage by folders and files

free
Shows free memory

uptime
Tells you how long the system has been up

vmstat 1
Shows activity memory

top
Shows list of processes ( you can change the sorting columns by typing “F” then “n” (or what ever other letter representing a column. You can choose from a list)

touch

Creates a file

cat
Concatenates a file or files

ping
Same as Microsoft pinc

mail -v -s “Backup done” -a /scripts/text.txt < /scripts/bkp.txt peterh@lw.co.za

to e-mail from the shell. Great for including in scripts.

Backing up in Linux using tar, gzip an rsync

By peter, September 12, 2009

About TAR and GZIP


Linux has combined the following programs:
TAR
GZIP
The “tar” utility archives but doesn’t compress very well,so we use the “gzip” along with “tar” to compress.

Zipping

The command to zip and compress is “tar -vzcf”
The command to zip only is “tar -vcf”
Exercise:
Back up the /etc directory as follows;
“tar -vcf etc.tar /etc”
Have a look at the size.Now back up using GZIP with TAR as follows;
“tar -vzcf etc.tar.gz /etc” (v=verbose; z=gzip; c=create; f=file) notice the difference in size

Extracting
The command to restore a zipped and compressed file is “tar -vzxf zipfile.tar.gz” The command to restore a zipped only file is
“tar -vxf zipfile.tar.gz”
To extract to a different location you would include the “-C” switch followed by the location, as follows:

tar -vzxf home.tar.gz -C /home/extracted/

To extract selected files use the “tar -vzxf zipfile.tar.gz location/filetoextract.txt” command.


Viewing the content of a tar.gz file
You can view the content of a zipped file with the following command (In this example I have piped the output to “more”)

tar -tzvf /backup/etc.tar.zg | more

How to do an incremental backup:
For an incremental backup, tar needs to generate a file which tells tar which files have been backed up and when. To generate this file you need to use the “-g” switch as follows:
First you need to do a full backup which will generate the “snapshot_file” if it doesn”t exist

tar -cz -g /backup/snapshot_file -f /backup/backup_monday.tar.gz /home

Next you need to do the incremental backup, making use of the “snapshot_file” file

tar -cz -g /backup/snapshot_file -f /backup/backup_tuesday.tar.gz /home

Mirroring directories with the “rsync” command:
The rsync command makes a mirror image of the directory you select. You can mirror to a local folder, or a folder on a remote computer.

Using rsync to mirror locally:

rsync -a /home /mirror

if you wanted to mirror the contents of the home directory, including it”s file structure, but not the home directory its self, type the following:

rsync -a /home/. /mirror

When you run the command again, only files that have changed will be updated

Here are some of the switches you can use:
-a Archive mode

-x Does not follow links etc to other locations
-v Verbose mode

-z Compresses during transfer (suited for remote transfer)

–delete Deletes files on the mirror that no longer exist in the original directory

–exclude-from Does not backup files included in the exclude file

Use the “exclude” option as follows:

rsync -a -exclude-from /home/exclude /home/. /mirror

Using rsync to mirror to a remote location:

You will need “rsync” installed on both the local and remote computers
Example 1

rsync -ave ssh root@remotepc:/data/. /databackup/

This example copies the files from a remote computer to the local computer into a folder called “databackup”

Example 2

rsync -ave ssh /databackup/ root@remotepc:/data/

This example restores the data from the “databackup” folder on the local folder to the remote computer

How to setup a cron

By peter, September 12, 2009

Cron jobs allows you to schedule tasks regularly.


There are two types of cron job. System jobs and individual user jobs

System jobs

These are controlled with the file /etc/crontab. You won’t find the actual scripts that run here, you will find these under the following folders:

/etc/cron.hourly/

/etc/cron.daily/

/etc/cron.weekly/

/etc/cron.monthly/

Jobs of individual users

These are stored in a the /var/spool/cron/tabs/ folder. These can be edited with the crontab command

crontab -e Creates or edits a cron job

crontab file Specifies a file containing a list of jobs

crontab -l Displays current jobs

crontab -r Deletes all jobs
Scheduling

Write a script and save to a directory

Now use the following command:

crontab [-u user] [-] [-e | -r] [file to run]

Or log on as that user and do the following:

1. Type “crontab -e” at the command prompt

2. Edit the file as needed (it uses “vi”)

You will need at least 6 fields for a simple cron job.

——————————————————————————————————————

Minutes     Hours      Day of the month     Month    Weekday      Command

30             13                    *                           *              1              command

——————————————————————————————————————-


Examples:

To get a job to run at 13:30 every Monday type the following

30  13  *  *  1  /root/command.sh

To get the command to run every 1st of the month type the following:

30  13  1-12   * /root/command.sh

To get a job to run every 10 minutes from 8 to 5, 5 days a week, type the following:

*/10   8-17  *  *  1-5  /root/conmmand.sh

You can also set a script or command to run when the computer boots up

@bootup /scripts/firewall.sh

Using the AT command

By peter, September 11, 2009



at will allow you to run a task once at a specified time If the at service is not running type “rcatd start”. Once the “AT” service is running the, Type the command at followed by the time at the command prompt as follows:

at 14:30

You will then be taken into the “at” command to type in the command you want to execute. Once you have added in the commands you want to execute, type “Ctrl d” to quit. To remove an “at” job, type “atrm” followed by the job number. To view scheduled jobs type “atq” at the command prompt.

OfficeFolders theme by Themocracy