Accueil > Système > Running Commands on a Remote Linux / UNIX Host

Running Commands on a Remote Linux / UNIX Host

08/10/2023 Categories: Système Tags: , ,
Print Friendly, PDF & Email

Remote Linux Commands

commands remote linuxYou would like to execute a command on a remote Linux/FreeBSD/Solaris/UNIX host and have the result displayed locally. Once result obtained it can be used by local script or program. A few examples:
=> File system and disk information

=> Get user information

=> Find out all running process

=> Find out if particular service is running or not etc

You can use rsh or ssh for this purpose. However, for security reason you should always use the ssh and NOT rsh. Please note that remote system must run the OpenSSH server.

Syntax for running command on a remote host:
ssh [USER-NAME]@[REMOTE-HOST] [command or script]

Where,

  • ssh: ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine.
  • USER-NAME: Remote host user name.
  • REMOTE-HOST: Remote host ip-address or host name, such as my-site.com.
  • command or script: Command or shell script is executed on the remote host instead of a login shell.

Examples

(A) Get disk information from a server called my-site.com:
$ ssh user@my-site.com df -h

(B) List what ports are open on remote host
$ ssh user@my-site.com netstat -vatn

(C) Reboot remote host:
$ ssh root@my-site.com reboot

(D) Restart mysql server (please note enclosed multiple command line arguments using a single or double quotes)
$ ssh root@my-site.com '/etc/init.d/mysql restart'

(E) Get memory information and store result/output to local file /tmp/memory.status:
$ ssh user@my-site.com 'free -m' > /tmp/memory.status

(G) You can also run multiple command or use the pipes, following command displays memory in format of « available memory = used free memory » :
$ ssh user@debian.test.com free -m | grep "Mem:" | awk '{ print "Total memory (used free): " $3 " " $4 " = " $2 }'

See how to configure ssh for password less login using public key based authentication.

Lire aussi:  Where to Set Environment Variables in Mac OS X

=> Related: shell script to get uptime, disk usage, cpu usage, RAM usage, system load, etc. from multiple Linux servers and output the information on a single server in a html format.

Source: NixCraft

Categories: Système Tags: , ,
Les commentaires sont fermés.