How to Execute Inline Commands Over SSH: A Practical Guide for Developers


8 views

When working with remote servers, SSH provides several ways to execute commands. The basic syntax is:

ssh user@hostname "command"

For multiple commands, you can chain them using operators:

ssh user@hostname "cd /path/to/dir && rm *.tmp"

This executes sequentially - the second command only runs if the first succeeds.

Shell interpretation can cause issues. Consider these approaches:

# Using single quotes
ssh user@hostname 'cd /path && ls -la'

# Escaping special characters
ssh user@hostname "cd /path/with\\ spaces && touch file"

For complex operations, here are some useful patterns:

# Multi-line commands
ssh user@hostname <<'EOF'
cd /var/log
grep "error" syslog
EOF

# Environment variables
ssh user@hostname "export TMPDIR=/tmp && ./script.sh"

# Piping output locally
ssh user@hostname "cat /etc/passwd" | grep root

Always be cautious with remote commands:

  • Avoid passing sensitive data in command line
  • Use SSH keys instead of passwords
  • Consider command logging on remote systems

For regular operations, consider these alternatives:

# SSH config aliases
Host myserver
    HostName server.example.com
    User myuser
    IdentityFile ~/.ssh/id_rsa

# Then simply:
ssh myserver "command"

# Using SSH tunnels
ssh -L 8080:localhost:80 user@host

Many developers need to execute quick one-off commands on remote servers without going through the full interactive SSH login process. The standard approach would be:

ssh user@example.com
cd /path/to/directory
rm *.tmp
exit

This is inefficient when you just need to perform a simple operation. Here's the better way.

The proper syntax for inline SSH commands is:

ssh user@hostname "command1 && command2 || command3"

The key points:

  • Enclose the entire command sequence in quotes
  • Use standard shell operators (&&, ||, ;) between commands
  • Escape special characters when needed

Basic file operations:

ssh deploy@prod-server "cd /var/www/html && sudo rm -rf cache/*"

System monitoring:

ssh admin@backup-server "df -h | grep -v tmpfs && free -m"

Multi-step deployment:

ssh git@staging "cd /apps/main && git pull origin master && npm install && pm2 restart all"

With variables:

BRANCH="feature-x"
ssh dev@test-env "cd ~/projects/app && git checkout $BRANCH && make test"

Command chaining:

ssh root@db-primary "mysqldump -u admin -p db_production > backup.sql && gzip backup.sql && mv backup.sql.gz /backups/"
  • Use -t flag if you need pseudo-terminal allocation
  • Add -v for verbose output when debugging
  • For complex commands, consider using Here Documents or scripts
  • Always test destructive commands first with echo

When automating SSH commands:

  • Use SSH keys instead of passwords
  • Limit user permissions on remote systems
  • Consider using command= restrictions in authorized_keys
  • Never include plaintext passwords in commands