How to Completely Uninstall Elasticsearch and Fix Port 9200 Connection Issues in Ubuntu


2 views

When dealing with Elasticsearch on Ubuntu, simply running sudo apt-get --purge autoremove elasticsearch doesn't always clean up everything. The service might still linger in your system, especially when you've installed via .deb package. Here's what I discovered when facing the "Connection refused" error on port 9200 after reinstalling.

First, ensure all Elasticsearch components are removed:

sudo apt-get purge --auto-remove elasticsearch
sudo rm -rf /etc/elasticsearch/
sudo rm -rf /var/lib/elasticsearch/
sudo rm -rf /var/log/elasticsearch/
sudo userdel elasticsearch
sudo groupdel elasticsearch

After complete cleanup, install the .deb package properly:

sudo dpkg -i elasticsearch-1.6.0.deb
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service

Verify these critical settings in /etc/elasticsearch/elasticsearch.yml:

network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node

If you still get connection refused errors, check these:

# Verify service status
sudo systemctl status elasticsearch

# Check port listening
sudo netstat -tulnp | grep 9200

# Examine logs
sudo journalctl -u elasticsearch -f

Ubuntu's UFW might be blocking the port:

sudo ufw allow 9200/tcp
sudo ufw reload

For better version management, consider using the official Elasticsearch repository:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list
sudo apt-get update && sudo apt-get install elasticsearch

When dealing with Elasticsearch version changes on Ubuntu, a simple apt-get remove often leaves behind critical configuration files and service definitions. Here's what actually happens during a standard removal:

sudo apt-get --purge autoremove elasticsearch
# This removes:
# - Binary files (/usr/share/elasticsearch)
# - Configuration templates (/etc/elasticsearch)
# - Systemd service files (/lib/systemd/system/elasticsearch.service)

After installing version 1.6.0 via dpkg -i, the service appears to start but fails to bind to port 9200. This typically indicates either:

1. Residual configs from previous installation
2. Improper service initialization
3. Network binding restrictions

Before reinstalling, execute this comprehensive cleanup:

# Stop any running instances
sudo systemctl stop elasticsearch
sudo pkill -f elasticsearch

# Remove all package traces
sudo apt-get purge elasticsearch
sudo rm -rf /var/lib/elasticsearch/
sudo rm -rf /etc/elasticsearch/
sudo rm -rf /usr/share/elasticsearch/
sudo rm -rf /var/log/elasticsearch/

# Clean up systemd
sudo systemctl daemon-reload
sudo systemctl reset-failed

After complete cleanup, install with proper initialization:

sudo dpkg -i elasticsearch-1.6.0.deb

# Critical post-install steps
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service

# Verify service status
sudo systemctl status elasticsearch -l

If still getting connection refused, check these critical points:

# Check listening ports
sudo netstat -tulnp | grep 9200

# Verify Elasticsearch logs
sudo tail -n 50 /var/log/elasticsearch/elasticsearch.log

# Test network binding
curl -XGET 'http://127.0.0.1:9200/_nodes?settings=true&pretty=true'

Version 1.6 needs explicit network binding in /etc/elasticsearch/elasticsearch.yml:

network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

After configuration changes, always restart the service:

sudo systemctl restart elasticsearch