Wednesday, June 26, 2013

Add one file's contents in other file on Remote host using command line

Add one file's text in another file on remote host :

If you have a file A and you want to add this whole file's text in file B but the problem is file B is on remote host.

You may be think to download the file B, update it and upload it again.

But you can do this using command line in a single command.

Suppose file A which is on your system is in  /root directory and file B which is on Remote host is in /root/Documents.

Now run following commands to add file A's text in file B.
# cat /root/fileA.txt | ssh 192.168.xx.xx "cat >> /root/Documents/fileB.txt"
or
# ssh 192.168.xx.xx "cat >> /root/Documents/fileB.txt" </root/fileA.txt
or
# scp /root/fileA.txt 192.168.xx.xx: /root/fileA.txt && ssh 192.168.xx.xx "cat /root/fileA.txt >>  /root/Documents/fileB.txt"
where:
192.168.xx.xx is IP of Remote Host

The commands do not remove the text of fileB instead they add the text at bottom of the fileB.

ssh Error : Could not open a connection to your authentication agent

Could not open a connection to your authentication agent :

If you are trying to execute ssh-add -l or ssh-add and it gives following error.
 Could not open a connection to your authentication agent
To remove this error, Run following command.
# eval `ssh-agent`
Now try to tun ssh-add again.

Change hostname of your Linux machine without Reboot

Change hostname of your Linux machine without Reboot :

You must fill hostname while installation. It is your machine name.

If you want a definition of host-name or you want to understand what exactly is it, you should click here .

But if you know about it, you should read ahead.

You must have seen your host name in terminal after symbol '@'.
But if you want to know your host-name using command line, you should run
# hostname
If you want to change it, follow this.

If you are using CentOS, Fedora or Redhat

1) Open file /etc/sysconfig/network
2) Now change HOSTNAME=old-host-name to HOSTNAME=new-host-name
3) Save the file
4) Restart network using
# service network restart
Now open terminal, you will see the host name has been changed after symbol '@'
If you are not satisfied enough. Run command
# hostname

If you are using Debian or Ubuntu

1) Open file /etc/hostname
2) Change the old hostname into new hostname.
3) Save the file.
OR
Run the following command
# echo new-hostname > /etc/hostname
Now open terminal, you will see the host name has been changed after symbol '@'
If you are not satisfied enough. Run command
# hostname