Tuesday, March 31, 2009

saving a file in VI as root

It often happens that you open up a file for editing using vi or vim and only when you come to save the file, you realise you did not open the file with root privileges and are unable to save the file.

when you type :wq!
you get E212: Can't open file for writing.

If that happens to you , simply type

:w !sudo tee %

you will be asked for your sudo password
enter your password and
press enter when asked

(vi/vim will detect that the file has been changed and ask whether you want
it to be reloaded.)

As a shortcut, you can define your own command.

Put the following in your /etc/vimrc

command W w !sudo tee % > /dev/null

and then in future simply type

:W

Friday, March 27, 2009

How to Stop yum from updating your Kernel

On a Server and also on your own machine, kernel Upgrades can break already installed 3rd party Applications, since the application would have been compiled for use on a specific kernel, I find this to be particularly a problem with vmware and Virtual box, every time my system upgrades its kernel there are a few packages that i need to recompile to get to work, this is a real pain, and on your server can lead to serious problems. To stop yum from updating your Kernel
You can tell yum to skip kernel updates by adding kernel-* to the 'exclude=' option in /etc/yum.conf. if the exclude option does not exist simply add it, add in a line that says exclude=kernel-*
you can do the same for any package that you do not want to update, for example, I run a patched version of postfix on one of my servers, since ive included a quota patch when I compiled it , I do not want postfix to upgrade to an unpatched version. So to stop that particular server from upgrading postfix I simply added postfix to the 'exclude=' option in /etc/yum.conf
you can add multiple exclude options by simply adding exclude=app_name_to_exclude under one another.

Tuesday, March 24, 2009

White Background

Test smtp server with required authentication

To test your smtp server, to see if it is an open relay or to
test if your authentication works on it. Use telnet to connect
to the server, this allows you to very quickly test different connection
scenarios to the server. this method is very useful if you
want to test how your smtp server behaves from different remote locations,
and all you have is an ssh account into the locations you want to test from.
You can simply ssh into the remote location you want to test from and then
follow these steps
In this example I wanted to test if basic outgoing smtp authentication was working on my server (everything in Bold is what you type everything not in bold is the servers response)



telnet smtp.mailserver.com 25


Trying 195.158.102.65...
Connected to mail.clivesblog.com.
Escape character is '^]'.
220 smtp.mailserver.com.com ESMTP ready
ehlo
250-smtp.mailserver.com ESMTP hello
250-PIPELINING
250-AUTH PLAIN LOGIN CRAM-MD5 DIGEST-MD5 GSSAPI
250-AUTH=PLAIN LOGIN CRAM-MD5 DIGEST-MD5 GSSAPI
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-SIZE 10485760
250-STARTTLS
250-HELP
250 OK
auth login
334 VXNlcm5hbWU6
Y2xpdmUuZ2VyYWRhQHJzMi5jb20=
334 UGFzc3dvcmQ6
cnMy
235 Authentication successful
mail from:clive.gerada@clivesblog.com
250 Sender accepted
rcpt to:anybody@anyaddress.com
250 Recipient accepted
data
354 Ready to receive data; remember .
subject: any subject

double space (ie hit the space bar twice)

then enter any text for the body of the email

.

250 Mail queued for delivery
quit
221-smtp.mailserver.com ESMTP is closing connection
221 Good bye

the Y2xpdmUuZ2VyYWRhQHJzMi5jb20=
and cnMy

is my username and password encoded in Basd64 cypher

you have to encode it in base64 for the server to understand it.
you can use the following online tool to encode and decode to and from base64


perl -MMIME::Base64 -e 'print encode_base64("username");'
perl -MMIME::Base64 -e 'print encode_base64("password");'

will encode base64 for you or alternatively you can use an online encoder like

http://ostermiller.org/calc/encode.html

How to Encrypt/decrypt files


If you want to protect files so that they can transverse across cyberspace safely, or you just want to protect them from
prying eyes with a password.

use the gpg command.

gpg -c filename

You will be asked for a pass phrase.

enter your pass phrase.

You will be asked to repeat your pass phrase
a file will be created with the .gpg extention

Caution: if you forget your pass phrase you will not be able to recover your file. the encryption used is that good.

to decrypt the file simply type
gpg filename.gpg

enter your pass phrase when asked.

To do multiple files I usually zip them into one zip file and then encrypt the zip file.
you can do this in one shot by typing the following
zip - file1 file2 file3 file4 file5 | gpg -eat > outfile.zip.asc

Friday, March 20, 2009

Alternating between directories in your Terminal

often, I am working in a directory which has a long path eg /home/cgerada/long/complicated/directory/structure/folder/

and then want to temporally change to another directory to do something and change back to the directory I was in


instead of using cd to change to the other directory rather use
pushd eg pushd /directory

that way when you are done in /directory and you want to return to /home/cgerada/long/complicated/directory/structure/folder/

simply type popd

and you will be returned to /home/cgerada/long/complicated/directory/structure/folder/

cd - is also very useful and will take you to your last working directory

and is great for switching between 2 directories .. just type cd - and then cd- again to alternate between 2 working directories

Thursday, March 19, 2009

Quick way to Rip a DVD

Insert your DVD into your DVD Drive of your PC/Laptop

and type the following in your terminal

dd if=/dev/cdrom of=/cdrom_image.iso
your DVD will be saved as an iso image in your current directory.


It doesn't even need to be that complicated.
These commands will work too (isn't Linux wonderful?)
cp /dev/cdrom cd.iso
cat /dev/cdrom > cd.iso

Saturday, March 14, 2009

.ssh/config

ssh reads configuration information from a config file
(if you have one) before connecting to a machine.

This way you can tell ssh how you want to connect toparticular machines. If you have a lot of different
machines that you connect to,  especially if youconnect to them on different ports and with different
user names then
it is a great idea to create the following file
.ssh/config

type vi .ssh/config

and popultate the config file with the information
of the machines that you connect to.
eg
Host *netaxiz.com
Port 2222
User clive
    
Host ipcop1
Port 222
User root
Host home
Hostname 192.168.0.10
User clive
Port 768

with the above information in the config file if you typessh machine.netaxiz.com
will be the equivalent of typing
ssh -p 2222 clive@machine.netaxiz.com

and typing scp file1 ipcop1:file1
will be the equavelent of typing 
scp -P222 file1 root@ipcop1

as you can see the config file can save you a lot of time.

The port and the username from the config file will
automatically be used every time you connect to the
machines listed in the config file.
Were this is especially usefull is when you want to
copy ssh keys to a remote machine using the
ssh-copy id command when the remote machine uses
a different port to 22.

Lets say you want to copy your public keys to your
machine called home but machine home only allows
 ssh connection on port 768. By adding in the information
above for machine home. I can now simply type
ssh-copy-id -i ~/.ssh/id_rsa.pub home
to transfer the keys.

if you get the following error when using ssh
Bad owner or permissions on .ssh/config
then you must change the permissions of the config file.
cd .ssh
chmod 600 config


Sunday, March 8, 2009

libdvdcss

To Play a commercial DVD in your media player
you need to install libdvdcss

you will also need libdvdcss to be able to backup your commercial DVD's ;)

yum install libdvdcss

or apt-get install libdvdcss depending on your distro