Pages

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

Monday, February 23, 2009

RPM Fusion Repository


RPM Fusion Repository provides software that the Fedora Project
or
Red Hat doesn't want to ship.
You will find software like VLC media player, Mencoder FFmpeg
and other
software that is not available in the stock Fedora or
Red hat repositories
.


to install the RPM Fusion repository on your system simply type
the following
.

rpm -ivh http://download1.rpmfusion.org/free/fedora/
rpmfusion-free-release-stable.noarch.rpm


then you can install software directly ie
yum install vlc

How to find out what Distro you are running

uname -a will give you information about your running Kernel. To find out what Distribution is installed on a Particular system try one the following:

cat /etc/issue

or

cat /proc/version

or

cat /etc/*release

Thursday, October 30, 2008

How to change the Time Zone on your Redhat / Centos system

cp /usr/share/zoneinfo/Africa/Johannesburg /etc/localtime

will set your time zone to SAST

you can simply use Tab auto completion to find the exact time zone you want by choosing the continent and city of your choice for example to change your timezone to London's time

cp /usr/share/zoneinfo/Europe/London /etc/localtime

to change to universal time type

cp /usr/share/zoneinfo/Universal /etc/localtime

to change to GMT time type

cp /usr/share/zoneinfo/GMT /etc/localtime

if asked if you want to overwrite the localtime file answer yes.

Tab auto completion is very useful in finding the exact time zone you want.

Tuesday, October 14, 2008

How to backup and restore a Mysql database

To backup your mysql database type the following.

mysqldump -u username -ppassword database_name > dump.sql

make sure not to leave a space between the -p and the password, otherwise mysql will prompt you for a password and will assume that your password is the database name.

the entire database will be backed up into the dump.sql file

then to restore the database type the following

'mysql -u username -ppassword database_name < dump.sql'

Thursday, September 25, 2008

How to reset a Forgotton Password in MYSQL

ever forgotton your root password for your mysql database, or any other users password for that matter, no problem here
simply stop mysql by typing
/etc/init.d/mysqld stop

then restart Mysql with the following command

mysqld_safe --skip-grant-tables

You should see mysqld start up successfully. Now you should be able to connect to mysql without a password.

mysql --user=root mysql

update user set Password=PASSWORD('new-password');
flush privileges;
exit;

once done restart mysql normally

/etc/init.d/mysqld restart

Shaaawiiing

Wednesday, September 17, 2008

How to add multiple users and passwords to your system

for names in user1 user2 user3 user4 user5
do
useradd $names
echo "anypassword" | passwd --stdin $names
done

or you can type it all in one line and hit enter like so

for names in user1 user2 user3 user4 user5;do useradd $names;echo "anypassword" |passwd --stdin $names;done



the above will add user1 , user2 ,user3, user4 and user5 to your system.
All users will have the password "their user name followed by anypassword"
You could have your list of users in a text file and enter the commands like so, presuming your text file is called users.txt and in your text file you just insert the user names underneath one another eg

user1
user2
user3

etc

for names in `cat users.txt`
do
echo $names
useradd $names
done

If you want to add usernames and passwords from a list, then make a text file like so

user1:password1
user2:password2
user3:password3

etc
save the file , in this example we'll save it as userlist.txt
then

for names in `cat userlist.txt`
do
user=`echo $names | cut -f1 -d:`
pwd=`echo $names | cut -f2 -d:`
useradd $user
echo $pwd | passwd --stdin $user
done

Monday, September 1, 2008

How to remove @#$ Annoying Console beeps

To Remove all console beeps whilst running X type
xset b off

this will disable the annoying console beeps for all programs.

To remove the console beep when running in run level 1 2 or 3 without X, or on one of the virtual consoles ( cntrl-Alt 1 to 6) type

setterm -blength 0


Thursday, August 28, 2008

Nog a sweet IP Tables rule

This one will force your users through your Squid proxy server, even if your users are configured to access the net directly

iptables -t nat - A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128

assuming that your squid proxy is configured to use port 3128 the above rule on your iptables firewall.

or if your squid proxy server is on a different server say 192.168.1.10 then
iptables -t nat - A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT -d 192.168.1.10 --to-ports 3128

if you need to insert the rule at line number 5 of an existing chain then
iptables -t nat - I PREROUTING 5 -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128


will forward all standard port 80 http traffic to your Squid Proxy server on port 3128 .......sweet

Tuesday, August 26, 2008

Making files undeletable, updatable only and unbackupable

Attributes on a file can help you control what people can do with different files you can make files
1: Undeletable (Even by root)
2: Updatable only (Even by root)
3: Unbackupable :) (Even by root)

1: to make a file undeletable type
chattr +i filename
even if root trys to delete the file they will not be allowed to, To make the file deletable again you will need to type
chattr -i filename

2: to make a file updateable only, which means that you will be able to append content to the file but you will not be allowed to delete it or remove content from it
type

chattr +a filename

3: to prevent a file from being backed up by admins using the dump command

type

chattr +d filename
to list the set attributes of a file type
lsattr filename

Tuesday, August 19, 2008

tip to extract configured settings only in a config file

some config files are huge, and have more comments "#" and blank space than they do actual configured settings.
sometimes you need to just have a look at the settings that are configured and not all the comments.
to do this try the following.

cat filename.conf |grep -v "^#" | grep -v "^$" | less

will list the config file without all the lines with comments and will also leave out all the blank space
giving you information on your configured settings only.

the -v switch in grep tells grep to list files that do not contain the following string
the ^ (caret) means the beginning of a line and the $ means the end of a line and then passing the output
to less, allows you to page through the file using your arrow keys