find and replace in vi vim

Search and replace in vim

Find and replace text strings in vim file

It is one of the most frequent task you might do while working on vim.

Here are some of the most used ones

1. Find and search a single string and replace the first occurrence only
:s/search/replace/g

The above code would search for “search” and replaces it with “replace” and it would do it only once

2. Find all occurrences of the string and replace those
:%s/search/replace/g

In this case all strings in the file with “search” would be replaced by “replace”

3. Find and ask for confirmation before changing it
:%s/search/replace/gc

It would do the usual task but this time it would ask for confirmation before it changes it.

You can do search and replace in vi or vim using the above command being on vim. Don’t forget to be out of the update mode otherwise you would just be writing the above commands.

Allow or deny access

Allow or Restrict sudo access to Users in linux

On collaborative work, you would give access to others who would be helping you out on the code or system admin stuff.

The question would be, how limited can you go with granting access to the users?

The first thing would be adding the user to the sudoers list, lets say you have created a new user named tom and you want to grant sudo access to this user

You can edit the sudoers file with whatever editor you are using. But the most recommended way would be using visudo


sudo visudo 

Grant all the access to user

If you want to give the world access to the user, then add the following line and save the file


tom ALL=(ALL) ALL

tom is the user you want to grant permissions and the first ALL is for the host and the ALL in the bracket is referring to other accounts the user sudouser can act as and the final ALL would be for list of commands – in this case tom has every access provided.

Grant only some commands

Lets say you want to give tom an access of copy and renaming a file


tom ALL=/bin/cp,/bin/mv

The above command would tell the system the user tom has access to cp and mv only. You might need to check the correct binary to cp and mv using which command. That is which cp would tell you the right path to the binary of the cp.

Allow the user everything but installing new softwares using apt-get

Ok, so you might want to give every access to the user but you want to limit installing new softwares using apt-get


tom ALL=ALL,!/usr/bin/apt-get

Here tom can do everything but not apt-get. If you add the ! in front of the command, it means don’t allow.

So using the combinations of the above you can reach the level where you can grant and deny any command for the user of interest

airplane in he runway

Deploying Symfony on Production Server

Time to ship your symfony app to production? Here are some flight checks. Lets assume your symfony app is in folder symfony-app

1. Don’t put Symfony app in the public accessible folder on your server

If your servers public facing folder is, say, /var/www/html, then don’t put the whole symfony folder there.

Choose another deeper and non-public facing folder. Let say /apps/symfony-app. I will list how you would put the public facing folders later.
For this to happen run this command on your server

mkdir /apps

2. Pushing your code to production server

You can use FTP client like filezilla or cyberduck and put your code in /apps
If you are using git, then you can clone your app like this

cd /apps
git clone https://username@bitbucket.org/username/symfony-app.git symfony-app

Continue reading Deploying Symfony on Production Server

do you postfix?

Recently I was engaged on setting up mail client on the new redhat (rhel6) server for one of my clients. The server was on its default setup of sendmail. Since it is a bit outdated, I decided to use postfix relaying on gmail SMTP.

First thing first, make sure sendmail is not running by running this command

sudo service sendmail stop

Make sure if there are any lingering processes out there by issuing proper process listing commands on your server and kill those if you have to

Then check if you have postfix installed your machine

sudo service postfix start

If you got any complaint like service not found or something along that line, just install the postfix

sudo yum install postfix

Ok now lets go to the juicy part of integrating the postfix with gmail SMTP.
Lets assume your username is gopostfixgo@gmail.com and password is #nomoresendmail

Now lets proceed to the configuration part of the postfix.
go to postfix config folder

cd /etc/postfix

Here you will see a couple of files. Our interest would be on the main.cf file
First, before you start messing with the file, create a copy of it

sudo cp mail.cp main.cp.bak

Just go to the end of file and add the following snippet in there

smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/pki/tls/certs/self.pem
relayhost = [smtp.gmail.com]:587    

That last line right there would tell the postfix it has to relay the SMTP to other server.

Now lets create the sasl_passwd file

sudo touch sasl_passwd
echo "[smtp.gmail.com]:587 gopostfixgo@gmail.com:#nomoresendmail" > sasl_passwd

The above command would add the content to the sasl_passwd file.

Then to obscure the password information you placed on in the sasl_passwd file,

sudo postmap /etc/postfix/sasl_passwd

The other point would be creating a self certificate you would be using for your secure communication. If you have a certificate you are using already, then just point it to the right place and you should be fine

cd /etc/pki/tls/certs/
make self.pem

The above command would give you interactive wizard that you will follow and create the certificate.

After this, you need to tell the postfix to pick the changes you have been ramming. For that just restart postfix

sudo service postfix restart

Then open your logger to see if there are anything that are suspicious going or if mail is not working, you can tail it as

tail /var/log/maillog

Common Errors
1. Google might be sending some kind of error saying you have to log into the web browser bla bla bla.. then do the following
Go to https://www.google.com/settings/security/lesssecureapps and login with your google account and turn it on

Vim not syntax highlighting in sudo/root huh..

So I had to deal with the server issue and I ssh into it. Then opened the file looked at it and wanted to change the content, I had to be on root and I noticed the syntax highlighting was off..
Here are the steps I took to check what went wrong.
Basically when I am sudo-ing the user has changed a bit. So the configuration per that user is the area of focus.
Also, there could be multiple installations and vanilla versions for the vim as well, so check which one the sudo is using

sudo which vim

I was able to locate the right vim the sudo was using and I applied that one to fix the issue

:insert not working for pasting on Vim editor

I will not explain how useful vim is, if you a developer on the *nix systems, then that is number one tool to tuck into your toolbox.

Now you want to paste huge text into vim and it looks all wacky, and the insert mode is not handling it.. here is what you would do..

Open your vim with

vi filename.ext

then do

:set paste

This will allow your vim to be in the paste mode and you can dump all you have without any problem then change the mode using esc and save it as :w

Access MySQL from Host being on Vagrant

For this all you would need is tunneling/port forwarding from vagrant to mysql.

Being on vagrant machine, figure out what the host ip is, usually netstat would tell you that.
The default being 10.0.2.2 and assuming you have 3306 port on host machine do the following

ssh username@10.0.2.2 -L 3306:127.0.0.1:3306 -N

The above would forward the 3306 [ the one on the left ] to the hosts port. Mind you, you have to use 127.0.0.1 to access your vagrant machine in place of localhost which might not work.

Failed to Retrieve Share List from Server when connecting unix

I was getting the Failed to Retrieve Share List from Server when I was trying to move files from my mac to mint 13 pc.

I am trying using the existing samba protocol.

Here is how I fixed it.
I just added the ip address of the mac on my hosts file

sudo vi /etc/hosts

this will bring the hosts file. Then add the ip address and the host name of the computer you are trying to communicate.

Say the ip address of the computer you are trying to connect is 192.168.2.5

192.168.2.5 hostname

Then reboot your *nix

sudo reboot

Process Commands in Linux

To stop the current process and get back to the shell:

ctrl+z

To put the current process in the backgroup

bg

To bring the background process to front

fg

To send the the job to background on one line, put ambersand (&) at the end eg usingvi command:

vi somefile.txt &

To see all the background jobjs

jobs

Bringing specific job from background to foreground

fg %1 - this will bring the job number 1 to front

List all the running processes

ps aux

To filter some process, like to get the process id and stuff

ps aux | grep "process name"