Tuesday, May 13, 2008

Replacing words in vi

To replace word in vi, the below steps can be used(replace OLD with NEW). Please make sure you are in command(normal) mode:

  1. to replace first occurrence of OLD to NEW on current line
    • :s/OLD/NEW
  2. to replace all occurrence of OLD to NEW on current line
    • :s/OLD/NEW/g
  3. to replace all occurrence of OLD to NEW between two line numbers (# are the line numbers)
    • :#,#s/OLD/NEW/g
  4. to replace every occurrence of OLD to NEW on current file
    • :%s/OLD/NEW/g

Sunday, May 11, 2008

Creating ssh reverse tunnel

Imagine you are out of the office, but you have an important document that you have to get from your personal computer in your office. Unfortunately your computer is protected behind a firewall, making it impossible to access. But you have a server that you can access and your personal computer also can access this server. This is where ssh reverse tunnel come into action. For easy explanation, we will call your current computer as current, your server as middle and your personal computer at the office as target.

Pre-condition for ssh reverse tunnel

  1. The current computer that you have can connect to port 12000 (or any other) on the middle server.
  2. The middle is running an ssh daemon willing to do port-forwarding (enabled by default in OpenSSH) and the GatewayPorts feature is enabled
  3. You can open an ssh connection from target to the middle in advance and leave it open.
  4. The SSH daemon is running on target on port 22. In fact the port can be arbitrary and the daemon does not have to allow port forwarding. You can even establish your own (not root) ssh daemon.
Below are the steps:
  1. Create a tunnel from middle to target and leave it open when you are still at the office. You cn also ask your colleague at the office to do this. The below command will open port 12000 on middle for listening and forward all request on port 12000 on middle to port 22 of target
    • user@target $ ssh -R 12000:localhost:22 middleuser@middle
  2. Now you can access to port 12000 on middle from current and you will be forwarded to port 22 on target
    • user@current $ ssh targetuser@middle -p 12000
  3. If somehow you cannot access, access middle first, then connect to port 12000 of localhost
    • user@current $ ssh middleuser@middle
    • user@middle $ ssh targetuser@localhost -p 12000
  4. You are now in the target server

Friday, April 25, 2008

Ubuntu 8.04 is already available

Ubuntu 8.04 is already available!!! Get your own copy at ubuntu.com

Friday, April 11, 2008

Setting up samba with password protection

To easily share your files to linux and windows clients, samba is still the preferred choice. In this guide I will show how to setup a samba server on centos 5 machine, that can be accessed only by certain people protected by password.

  1. Install samba on the server
    • # yum install samba
  2. Create the group that all the samba users will be contained in, for example 'samba'
    • # groupadd samba
  3. Create samba users and add it to the above group, which is in this example is 'samba'. Below is the example to create a user named 'user1' and add it to group 'samba'. Set the password for user1
    • # useradd user1 -g samba
    • # passwd user1
  4. Create the directory to be shared. In this example, i will use /home/shared. Change the ownership to root and group ownership to the 'samba' group. Change permission so that only user and group can read write and execute
    • # mkdir /home/shared
    • # chown -R root.samba /home/shared
    • # chmod -R 775 /home/shared
  5. Below is a simple setting of samba
    • [global] workgroup = samba
      server string = Samba Server
      security = user [shared_folder]
      comment = Sharing place
      path = /home/shared
      public = no
      writable = yes
      printable = no
      write list = @samba
      create mask = 0755
      force create mode = 0755
      directory mask = 0775
      force directory mode = 0775
    • What the above setting does basically is to setup /home/shared as samba shared directory but can only be accessed by user from group samba
  6. Add user/users to samba
    • # smbpasswd -a user1
  7. Start smb service, restart if it has already been started
    • # /etc/init.d/smb start
  8. 'user1' can now access the samba server using address 'smb://samba_server_ip_address/shared_folder' at any nautilus address bar. For windows client, you can see at your 'My Network Places' and find a workgroup named 'samba'

Friday, April 4, 2008

'Watch'ing your commands running

Sometimes when you run a command, you need to see the progress of the command, yet you do not want to keep refreshing the command by repeating it a few times. There is a useful command in linux that can do the job of refreshing for you which is watch. According to the watch man page: "watch - execute a program periodically, showing output fullscreen". By default watch will refresh the command in the interval of 2 seconds but this can be changed according to your needs.
For example, you want to see the memory usage of your computer every 2 seconds:

$ watch free -m

This kind of output will show:


(Click on the image for clearer view)

To set the interval(15 seconds) which watch will refresh, use -n option:

$ watch -n 15 free -m

The output:


(Click on the image for clearer view)

To see the differences of each refresh session, use -d. Watch will highlight the changes that happening on that particular moment:

$ watch -d free -m


(Click on image for clearer view)

To exit from watch, just use your trusty Ctrl-c