Total visit on this blog

Sunday, 5 August 2012

top – Process Activity Command

The top command is useful for monitoring a Linux or Unix system continuously for processes that take more system resources like the CPU time and the memory. top periodically updates the display showing the high resource consuming processes at the top. top is an excellent aid in checking a system. If your Linux or Unix system is giving a slow response time, just run top and look for statistics like – load average, CPU utilization, memory and swap usage and the top CPU and memory intensive processes. The chances are that you will get a fair idea of what is happening in the system. The top command can be started simply by giving the command.
top
and the output is a screen, like

Understand Top command output:
Line1: Gives System present time, up time of the machine, number of users logged in, Load average on system at 5, 10, 15 min interval.
Line2: Gives total number of process on the machine, number of running process, number of sleeping process, number of stopped process, number of Zambie process.
Line3: Gives you CPU details
Line4 &  5: Gives RAM and SWAP details.
Line6: To execute top command shortcuts(See below for the list of top command shortcuts ).
From Line7: dynamically displayed top process results.
 top commands shortcuts:
Note: Press below shortcuts at the time of running top command.
l --To display or to hide load average line

t --To display or to hide task/cpu line

1 --To display or hide all other CPU's

m --to display or to hide RAM and SWAP details

s --To change the time interval for updating top results(value is in sec's)

R --To sort by PID number

u -- Press u then username to get only that user process details

P --To sort by CPU utilization 

M --To sort by RAM utilization 

c --To display or hide command full path

r --To renice a process, press r then the PID no then the renice value to renice a process.

k --To kill a process, press k then PID number then enter to kill a process

w --To save the modified configuration permanently.

q --To quit the top command. 
h --for getting help on top command

How to understand the output of TOP command:
1. Show Processes Sorted by any Top Output Column – Press O
By default top command displays the processes in the order of CPU usage. When the top command is running, press M (upper-case) to display processes sorted by memory. To sort top output by any column, Press O (upper-case O) , which will display all the possible columns that you can sort by as shown below.
Current Sort Field: P for window 1:Def
Select sort field via field letter, type any other key to return
a: PID = Process Id v: nDRT = Dirty Pages count
d: UID = User Id y: WCHAN = Sleeping in Function
e: USER = User Name z: Flags = Task Flags
When the linux top command is running, Press R, which does the sort in reverse order.

2. Kill a Task Without Exiting From Top – Press k
Once you’ve located a process that needs to be killed, press ‘k’ which will ask for the process id, and signal to send. If you have the privilege to kill that particular PID, it will get killed successfully.
PID to kill: 1309
Kill PID 1309 with signal [15]:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent
1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent
5136 root 16 0 38040 14m 9836 S 0 0.2 0:00.39 nautilus

3. Renice a Unix Process Without Exiting From Top – Press r
Press r, if you want to just change the priority of the process (and not kill the process). This will ask PID for renice, enter the PID and priority.
PID to renice: 1309
Renice PID 1309 to value:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent
1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent

4. Display Selected User in Top Output Using top -u
Use top -u to display a specific user processes only in the top command output.
$ top -u geek
While unix top command is running, press u which will ask for username as shown below.
Which user (blank for all): geek
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent
1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent
Display Only Specific Process with Given PIDs Using top -p
Use top -p as shown below to display specific PIDs.
$ top -p 1309, 1882
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent
1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent

5. Display All CPUs / Cores in the Top Output – Press 1 (one)
Top output by default shows CPU line for all the CPUs combined together as shown below.
top – 20:10:39 up 40 days, 23:02, 1 user, load average: 4.97, 2.01, 1.25
Tasks: 310 total, 1 running, 309 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.5%us, 0.7%sy, 0.0%ni, 92.3%id, 6.4%wa, 0.0%hi, 0.0%si, 0.0%st
Press 1 (one), when the top command is running, which will break the CPU down and show details for all the individual CPUs running on the system as shown below.
top – 20:10:07 up 40 days, 23:03, 1 user, load average: 5.32, 2.38, 1.39
Tasks: 341 total, 3 running, 337 sleeping, 0 stopped, 1 zombie
Cpu0 : 7.7%us, 1.7%sy, 0.0%ni, 79.5%id, 11.1%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 0.3%us, 0.0%sy, 0.0%ni, 94.9%id, 4.7%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 3.3%us, 0.7%sy, 0.0%ni, 55.7%id, 40.3%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 5.0%us, 1.0%sy, 0.0%ni, 86.2%id, 7.4%wa, 0.0%hi, 0.3%si, 0.0%st
Cpu4 : 38.5%us, 5.4%sy, 0.3%ni, 0.0%id, 54.8%wa, 0.0%hi, 1.0%si, 0.0%st
Cpu5 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu6 : 0.3%us, 0.7%sy, 0.0%ni, 97.3%id, 1.7%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu7 : 5.4%us, 4.4%sy, 0.0%ni, 82.6%id, 7.7%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu8 : 1.7%us, 1.7%sy, 0.0%ni, 72.8%id, 23.8%wa, 0.0%hi, 0.0%si, 0.0%st

6. Refresh Unix Top Command Output On demand (or) Change Refresh Interval
By default, linux top command updates the output every 3.0 seconds. When you want to update the output on-demand, press space bar.
To change the output update frequency, press d in interactive mode, and enter the time in seconds as shown below.
Change delay from 3.0 to: 10
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent
1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent

7. Highlight Running Processes in the Linux Top Command Output – Press z or b
Press z or b, which will highlight all running process as shown below.
Highlight Running Process on Ubuntu Linux Using Top Command
Fig: Ubuntu Linux – top command highlights running process

8. Display Absolute Path of the Command and its Arguments – Press c
Press c which will show / hide command absolute path, and arguments as shown below.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 /usr/sbin/gagent
1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 /usr/sbin/gagent -l 0 -u pre

9. Quit Top Command After a Specified Number of Iterations Using top -n
Until you press q, top continuously displays the output. If you would like to view only a certain iteration and want the top to exit automatically use -n option as shown below.
The following example will show 2 iterations of unix top command output and exit automatically
$ top -n 2

10. Executing Unix Top Command in Batch Mode
If you want to execute top command in the batch mode use option -b as shown below.
$ top -b -n 1
Note: This option is very helpful when you want to capture the unix top command output to a readable text file as we discussed earlier.

11. Split Top Output into Multiple Panels – Press A
To display multiple views of top command output on the terminal, press A. You can cycle through these windows using ‘a’. This is very helpful, when you can sort the output on multiple windows using different top output columns.

12. Get Top Command Help from Command Line and Interactively
Get a quick command line option help using top -h as shown below.
$ top -h
top: procps version 3.2.0
usage: top -hv | -bcisS -d delay -n iterations [-u user | -U user] -p pid [,pid ...]
Press h while top command is running, which will display help for interactive top commands.
Help for Interactive Commands – procps version 3.2.0
Window 1:Def: Cumulative mode Off. System: Delay 3.0 secs; Secure mode Off.
Z,B Global: ‘Z’ change color mappings; ‘B’ disable/enable bold
l,t,m Toggle Summaries: ‘l’ load avg; ‘t’ task/cpu stats; ‘m’ mem info
1,I Toggle SMP view: ’1′ single/separate states; ‘I’ Irix/Solaris mode
……….

13. Decrease Number of Processes Displayed in Top Output – Press n
Press n in the Interactive mode, which prompts for a number and shows only that. Following example will display only 2 process as a time.
Maximum tasks = 0, change to (0 is unlimited): 2
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent
1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent

14. Toggle Top Header to Increase Number of Processes Displayed
By default top displays total number process based on the window height. If you like to see additional process you might want to eliminate some of the top header information.
Following is the default header information provided by top.
top – 23:47:32 up 179 days, 3:36, 1 user, load average: 0.01, 0.03, 0.00
Tasks: 67 total, 1 running, 66 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.7% user, 1.2% system, 0.0% nice, 98.0% idle
Mem: 1017136k total, 954652k used, 62484k free, 138280k buffers
Swap: 3068404k total, 22352k used, 3046052k free, 586576k cached
Press l – to hide / show the load average. 1st header line.
Press t – to hide / show the CPU states. 2nd and 3rd header line.
Press m – to hide / show the memory information. 4th and 5th line.

15. Save Top Configuration Settings – Press W
If you’ve made any interactive top command configurations suggested in the above examples, you might want to save those for all future top command output. Once you’ve saved the top configuration, next time when you invoke the top command all your saved top configuration options will be used automatically.
To save the top configuration, press W, which will write the configuration files to ~/.toprc. This will display the write confirmation message as shown below.
top – 23:47:32 up 179 days, 3:36, 1 user, load average: 0.01, 0.03, 0.00
Tasks: 67 total, 1 running, 66 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.7% user, 1.2% system, 0.0% nice, 98.0% idle
Mem: 1017136k total, 954652k used, 62484k free, 138280k buffers
Swap: 3068404k total, 22352k used, 3046052k free, 586576k cached
Wrote configuration to ‘/home/ramesh/.toprc’

Thursday, 2 August 2012

Reset MySQL root password

Steps to reset MySQL root password
[root@nischal ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: YES)
Check if the Mysql process is running (its running here)
[root@nischal ~]# ps -ef | grep mysql
mysql 1348 1 0 19:44 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe –basedir=/usr
mysql 1616 1348 0 19:44 ? 00:00:09 /usr/libexec/mysqld –basedir=/usr –datadir=/var/lib/mysql –plugin-dir=/usr/lib/mysql/plugin –log-error=/var/log/mysqld.log –pid-file=/var/run/mysqld/mysqld.pid –socket=/var/lib/mysql/mysql.sock
root 4069 2569 0 22:23 pts/0 00:00:00 grep –color=auto mysql
Time to reset the Mysql root password !!
Stop the mysql service
———————————————————————————————————————————–
[root@nischal init.d]# mysqld_safe –skip-grant-tables &
[2] 4706
[root@nischal init.d]# 120802 22:36:21 mysqld_safe Logging to ‘/var/log/mysqld.log’.
120802 22:36:21 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
————————————————————————————————————————————
[root@nischal init.d]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.25a MySQL Community Server (GPL) by Remi
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
————————————————————————————————————————————-
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
————————————————————————————————————————————–
mysql> update user set password=PASSWORD(“Admin@123_”) where User=’root’;
Query OK, 3 rows affected (0.33 sec)
Rows matched: 3 Changed: 3 Warnings: 0
————————————————————————————————————————————-
[root@nischal init.d]# /etc/init.d/mysqld stop
[root@nischal init.d]# /etc/init.d/mysqld start
—————————————————————————————————————————————
Connect to Mysql DB using new password:
[root@nischal ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.25a MySQL Community Server (GPL) by Remi
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
—————————————————————————————————————————————–
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mydatabase |
| mysql |
| performance_schema |
+——————–+
4 rows in set (0.00 sec)
——————————————————————————————————————————————-
Enjoy! Now you have root access. :)

Wednesday, 1 August 2012

Finding Load average on Linux

Load Average of a Linux server can be find in various ways and they are shown below. The 3 values which it displays are the system load averages for the past 1, 5, and 15 minutes.

# uptime
07:18:07 up 57 days, 8:04, 8 users, load average: 1.82, 1.24, 0.97

# cat /proc/loadavg
1.67 1.22 0.97 1/283 24487

# w
07:18:19 up 57 days, 8:04, 8 users, load average: 1.56, 1.21, 0.97
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
adevaraj pts/0 gtlslhh.tcprod. 01:41 0.00s 0.10s 0.03s sshd: adevaraju [priv]
tkhalef pts/3 gtwash01.tcprod. Mon05 45:35 0.38s 0.03s sshd: tkhalef [priv]
nimmika pts/4 220.10.245.82 05:49 1:28m 0.02s 0.02s -bash


# top
top – 07:19:14 up 57 days, 8:05, 8 users, load average: 1.57, 1.28, 1.00
Tasks: 284 total, 1 running, 281 sleeping, 0 stopped, 2 zombie
Cpu(s): 2.2%us, 0.7%sy, 0.0%ni, 95.9%id, 1.2%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 16634296k total, 14157704k used, 2476592k free, 438604k buffers
Swap: 5406712k total, 56k used, 5406656k free, 12835252k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
21914 apache 15 0 0 0 0 Z 7.7 0.0 0:02.06 httpd
24774 root 15 0 2508 1072 720 R 3.8 0.0 0:00.02 top
20504 apache 15 0 50596 24m 4800 S 1.9 0.1 0:03.89 httpd
1 root 15 0 2072 616 532 S 0.0 0.0 0:08.33 init

Monday, 30 July 2012

compgen: An Awesome Command To List All Linux Commands

compgen is bash built-in command and it will show all available commands, aliases, and functions for you. The syntax is:
compgen option

compgen command examples

To list all the commands available to you, enter:
compgen -c
Sample outputs:
ls
if
then
else
elif
fi
....
mahjongg
sol
gtali
sl-h
gnobots2
gnotravex
iagno
fortune
gnect
gnome-sudoku
LS
glchess
gnuchess
gnuchessx

You can search or count the commands:
compgen -c | grep find
compgen -c | wc -l
echo "$USER user can run $(compgen -c | wc -l) commands on $HOSTNAME."
Sample outputs:
vivek user can run 3436 commands on wks01.

To list all the bash shell aliases available to you, enter:
compgen -a
Sample outputs:
..
...
....
.....
.4
.5
bc
cd..
chgrp
chmod
chown
cp
dnstop
egrep
ethtool
fastping
fgrep
grep
iftop
l.
ll
ln
ls
mcdflush
mcdshow
mcdstats
mount
mv
pscpu
pscpu10
psmem
psmem10
rm
tcpdump
update
updatey
vnstat
wget
which


Other options are as follows:
 
########################################
# Task: show all the bash built-ins
########################################
compgen -b
########################################
# Task: show all the bash keywords
########################################
compgen -k
########################################
# Task: show all the bash functions
########################################
compgen -A function

How To Masquerade On Linux (Internet Connection Sharing)

IP Masquerade is a networking function in Linux similar to the one-to-many (1:Many) NAT (Network Address Translation) servers found in many commercial firewalls and network routers. For example, if a Linux host is connected to the Internet via PPP, Ethernet, etc., the IP Masquerade feature allows other “internal” computers connected to this Linux box (via PPP, Ethernet, etc.) to also reach the Internet as well. Linux IP Masquerading allows for this functionality even though these internal machines don’t have an officially assigned IP address.
MASQ allows a set of machines to invisibly access the Internet via the MASQ gateway. To other machines on the Internet, the outgoing traffic will appear to be from the IP MASQ Linux server itself. In addition to the added functionality, IP Masquerade provides the foundation to create a HEAVILY secured networking environment. With a well built firewall, breaking the security of a well configured masquerading system and internal LAN should be considerably difficult to accomplish.
Follow the following steps for performing masquerade:

Pre-Requirement

Machine 1 which is connected to WLAN(or dial-up connection) and also connected to machine 2 via LAN cable.
Machine 2 which is connected to machine 1 Via Lan cable

Steps for Machine 1 (which is connected to internet using a wifi connection i.e wlan0 or dial-up connection)

1. Open System -> Administration -> Firewall, and under ‘Masquerading’ select wlan0 (scroll down, if it’s not there add it), then click ‘Apply’
2. Set eth0 to ip 192.168.1.2, either via Network Manager or from the command line with
Code:
ifconfig eth0 192.168.1.2/24
(Assuming your wifi connection is in 192.168.1.* range)

Machine 2  (Connected to Machine 1 via ethernet cable from eth0)

1. Stop NetworkManager service since it’s easier without:
Code:
service NetworkManager stop
2. Set eth0 ip address to  and default gateway to 192.168.0.2:
Code:
ifconfig eth0 192.168.0.2
route add default gw 192.168.1.2
Check you can ping 192.168.1.2.
3. Now just set a DNS address in /etc/resolv.conf
Code:
nameserver 8.8.8.8
or
nameserver in the resolve.conf of Machine 1
now check. you can ping google.com.

Now enjoy the internet sharing. :) 

Friday, 13 July 2012

rsync over ssh

rsync is used to perform the backup operation in UNIX / Linux. rsync is a great tool for backing up and restoring files. rsync is basically a synchronous tools which is used synchronize the files and directories from one location to another in an effective way. rsync behaves much same as of rcp but it has many more options.

Important features of rsync

Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
Security: rsync allows encryption of data using ssh protocol during transfer.
Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
Privileges: No special privileges are required to install and execute rsync
Some of the additional features of rsync are:
  • support for copying links, devices, owners, groups and permissions
  • exclude and exclude-from options similar to GNU tar
  • a CVS exclude mode for ignoring the same files that CVS would ignore
  • can use any transparent remote shell, including rsh or ssh
  • does not require root privileges
  • pipelining of file transfers to minimize latency costs
  • support for anonymous or authenticated rsync servers (ideal for mirroring)

GENERAL

There are six different ways of using rsync. They are:
  • for copying local files. This is invoked when neither source nor destination path contains a : separator
  • for copying from the local machine to a remote machine using a remote shell program as the transport (such as rsh or ssh). This is invoked when the destination path contains a single : separator.
  • for copying from a remote machine to the local machine using a remote shell program. This is invoked when the source contains a : separator.
  • for copying from a remote rsync server to the local machine. This is invoked when the source path contains a :: separator or a rsync:// URL.
  • for copying from the local machine to a remote rsync server. This is invoked when the destination path contains a :: separator.
  • for listing files on a remote machine. This is done the same way as rsync transfers except that you leave off the local destination.
Note that in all cases (other than listing) at least one of the source and destination paths must be local.

Syntax

$ rsync options source destination
Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.

Examples:

sync example for backing up/copying from remote server to local Linux computer:
rsync -arv user01@server.example.com:/home/user01/ /home/testuser/user01backup/
(/home/testuser/user01backup/ is a local Linux folder path)
Here is what the “-arv” option does:
a = archive – means it preserves permissions (owners, groups), times, symbolic links, and devices.
r = recursive – means it copies directories and sub directories
v = verbose – means that it prints on the screen what is being copied

rsync -rv user01@server.example.com:/home/user01/ /home/testuser/user01backup/
This example will copy folders and sub-folder but will not preserve permissions, times and symbolic links during the transfer

sync -arv –exclude ‘logs’ user01@serve.example.com:/home/user01/ /Users/testuser/user01backup/
This example will copy everything (folders, sub-folders, etc), will preserver permissions, times, links, but will exclude the folder /home/user01/logs/ from being copied

Use of “/” at the end of path:
When using “/” at the end of source, rsync will copy the content of the last folder.
When not using “/” at the end of source, rsync will copy the last folder and the content of the folder.
When using “/” at the end of destination, rsync will paste the data inside the last folder.
When not using “/” at the end of destination, rsync will create a folder with the last destination folder name and paste the data inside that folder.

Thursday, 12 July 2012

rsync – Great Backup Tool

sync is used to perform the backup operation in UNIX / Linux. rsync is a great tool for backing up and restoring files. rsync is basically a synchronous tools which is used synchronize the files and directories from one location to another in an effective way. rsync behaves much same as of rcp but it has many more options.

Important features of rsync

Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
Security: rsync allows encryption of data using ssh protocol during transfer.
Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
Privileges: No special privileges are required to install and execute rsync
Some of the additional features of rsync are:
  • support for copying links, devices, owners, groups and permissions
  • exclude and exclude-from options similar to GNU tar
  • a CVS exclude mode for ignoring the same files that CVS would ignore
  • can use any transparent remote shell, including rsh or ssh
  • does not require root privileges
  • pipelining of file transfers to minimize latency costs
  • support for anonymous or authenticated rsync servers (ideal for mirroring)

GENERAL

There are six different ways of using rsync. They are:
  • for copying local files. This is invoked when neither source nor destination path contains a : separator
  • for copying from the local machine to a remote machine using a remote shell program as the transport (such as rsh or ssh). This is invoked when the destination path contains a single : separator.
  • for copying from a remote machine to the local machine using a remote shell program. This is invoked when the source contains a : separator.
  • for copying from a remote rsync server to the local machine. This is invoked when the source path contains a :: separator or a rsync:// URL.
  • for copying from the local machine to a remote rsync server. This is invoked when the destination path contains a :: separator.
  • for listing files on a remote machine. This is done the same way as rsync transfers except that you leave off the local destination.
Note that in all cases (other than listing) at least one of the source and destination paths must be local.

Syntax

$ rsync options source destination
Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.

Examples:

sync example for backing up/copying from remote server to local Linux computer:
rsync -arv user01@server.example.com:/home/user01/ /home/testuser/user01backup/
(/home/testuser/user01backup/ is a local Linux folder path)
Here is what the “-arv” option does:
a = archive – means it preserves permissions (owners, groups), times, symbolic links, and devices.
r = recursive – means it copies directories and sub directories
v = verbose – means that it prints on the screen what is being copied

rsync -rv user01@server.example.com:/home/user01/ /home/testuser/user01backup/
This example will copy folders and sub-folder but will not preserve permissions, times and symbolic links during the transfer

sync -arv –exclude ‘logs’ user01@serve.example.com:/home/user01/ /Users/testuser/user01backup/
This example will copy everything (folders, sub-folders, etc), will preserver permissions, times, links, but will exclude the folder /home/user01/logs/ from being copied

Use of “/” at the end of path:
When using “/” at the end of source, rsync will copy the content of the last folder.
When not using “/” at the end of source, rsync will copy the last folder and the content of the folder.
When using “/” at the end of destination, rsync will paste the data inside the last folder.
When not using “/” at the end of destination, rsync will create a folder with the last destination folder name and paste the data inside that folder.