diff --git a/src/borg/helpers.py b/src/borg/helpers.py
index a93ba710..3fc22e7c 100644
--- a/src/borg/helpers.py
+++ b/src/borg/helpers.py
@@ -769,7 +769,7 @@ def bin_to_hex(binary):
 class Location:
     """Object representing a repository / archive location
     """
-    proto = user = host = port = path = archive = None
+    proto = user = _host = port = path = archive = None
 
     # user must not contain "@", ":" or "/".
     # Quoting adduser error message:
@@ -814,7 +814,7 @@ class Location:
     ssh_re = re.compile(r"""
         (?P<proto>ssh)://                                   # ssh://
         """ + optional_user_re + r"""                       # user@  (optional)
-        (?P<host>[^:/]+)(?::(?P<port>\d+))?                 # host or host:port
+        (?P<host>([^:/]+|\[[0-9a-fA-F:.]+\]))(?::(?P<port>\d+))?  # host or host:port or [ipv6] or [ipv6]:port
         """ + abs_path_re + optional_archive_re, re.VERBOSE)  # path or path::archive
 
     file_re = re.compile(r"""
@@ -825,7 +825,7 @@ class Location:
     scp_re = re.compile(r"""
         (
             """ + optional_user_re + r"""                   # user@  (optional)
-            (?P<host>[^:/]+):                               # host: (don't match / in host to disambiguate from file:)
+            (?P<host>([^:/]+|\[[0-9a-fA-F:.]+\])):          # host: (don't match / or [ipv6] in host to disambiguate from file:)
         )?                                                  # user@host: part is optional
         """ + scp_path_re + optional_archive_re, re.VERBOSE)  # path with optional archive
 
@@ -841,7 +841,7 @@ class Location:
     def __init__(self, text=''):
         self.orig = text
         if not self.parse(self.orig):
-            raise ValueError
+            raise ValueError('Location: parse failed: %s' % self.orig)
 
     def parse(self, text):
         text = replace_placeholders(text)
@@ -871,7 +871,7 @@ def normpath_special(p):
         if m:
             self.proto = m.group('proto')
             self.user = m.group('user')
-            self.host = m.group('host')
+            self._host = m.group('host')
             self.port = m.group('port') and int(m.group('port')) or None
             self.path = normpath_special(m.group('path'))
             self.archive = m.group('archive')
@@ -885,10 +885,10 @@ def normpath_special(p):
         m = self.scp_re.match(text)
         if m:
             self.user = m.group('user')
-            self.host = m.group('host')
+            self._host = m.group('host')
             self.path = normpath_special(m.group('path'))
             self.archive = m.group('archive')
-            self.proto = self.host and 'ssh' or 'file'
+            self.proto = self._host and 'ssh' or 'file'
             return True
         return False
 
@@ -912,6 +912,12 @@ def to_key_filename(self):
     def __repr__(self):
         return "Location(%s)" % self
 
+    @property
+    def host(self):
+        # strip square brackets used for IPv6 addrs
+        if self._host is not None:
+            return self._host.lstrip('[').rstrip(']')
+
     def canonical_path(self):
         if self.proto == 'file':
             return self.path
@@ -923,7 +929,7 @@ def canonical_path(self):
             else:
                 path = self.path
             return 'ssh://{}{}{}{}'.format('{}@'.format(self.user) if self.user else '',
-                                           self.host,
+                                           self._host,  # needed for ipv6 addrs
                                            ':{}'.format(self.port) if self.port else '',
                                            path)
 

Please enable IPv6 addresses for remote repositories
Hello,

I was trying to specify a remote borg repo as something like that
ssh://user@[ipv6_address_string]:/path/to/repo
(and tried with leaving out the ssh:// in front, as well)

borg then somehow tried to read the ipv6 address as a hostname, and the hostname lookup naturally failed.

ssh supports ipv6 address string, by putting the address into a square bracket.

I have a use case where I want to backup to a remote repository via ssh onto a machine by ipv6. However, I do not want to expose machines IPv6 address to DNS, that's why I would like to use an address literal instead of a dns domain name in the remote repo url.

Regards,
wararjey
diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py
index ff6b5efe..7ce22dc2 100644
--- a/src/borg/testsuite/helpers.py
+++ b/src/borg/testsuite/helpers.py
@@ -58,6 +58,30 @@ def test_ssh(self, monkeypatch):
             "Location(proto='ssh', user='user', host='host', port=1234, path='/some/path', archive=None)"
         assert repr(Location('ssh://user@host/some/path')) == \
             "Location(proto='ssh', user='user', host='host', port=None, path='/some/path', archive=None)"
+        assert repr(Location('ssh://user@[::]:1234/some/path::archive')) == \
+            "Location(proto='ssh', user='user', host='::', port=1234, path='/some/path', archive='archive')"
+        assert repr(Location('ssh://user@[::]:1234/some/path')) == \
+            "Location(proto='ssh', user='user', host='::', port=1234, path='/some/path', archive=None)"
+        assert repr(Location('ssh://user@[::]/some/path')) == \
+            "Location(proto='ssh', user='user', host='::', port=None, path='/some/path', archive=None)"
+        assert repr(Location('ssh://user@[2001:db8::]:1234/some/path::archive')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::', port=1234, path='/some/path', archive='archive')"
+        assert repr(Location('ssh://user@[2001:db8::]:1234/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::', port=1234, path='/some/path', archive=None)"
+        assert repr(Location('ssh://user@[2001:db8::]/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::', port=None, path='/some/path', archive=None)"
+        assert repr(Location('ssh://user@[2001:db8::c0:ffee]:1234/some/path::archive')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::c0:ffee', port=1234, path='/some/path', archive='archive')"
+        assert repr(Location('ssh://user@[2001:db8::c0:ffee]:1234/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::c0:ffee', port=1234, path='/some/path', archive=None)"
+        assert repr(Location('ssh://user@[2001:db8::c0:ffee]/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::c0:ffee', port=None, path='/some/path', archive=None)"
+        assert repr(Location('ssh://user@[2001:db8::192.0.2.1]:1234/some/path::archive')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::192.0.2.1', port=1234, path='/some/path', archive='archive')"
+        assert repr(Location('ssh://user@[2001:db8::192.0.2.1]:1234/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::192.0.2.1', port=1234, path='/some/path', archive=None)"
+        assert repr(Location('ssh://user@[2001:db8::192.0.2.1]/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::192.0.2.1', port=None, path='/some/path', archive=None)"
 
     def test_file(self, monkeypatch):
         monkeypatch.delenv('BORG_REPO', raising=False)
@@ -72,6 +96,22 @@ def test_scp(self, monkeypatch):
             "Location(proto='ssh', user='user', host='host', port=None, path='/some/path', archive='archive')"
         assert repr(Location('user@host:/some/path')) == \
             "Location(proto='ssh', user='user', host='host', port=None, path='/some/path', archive=None)"
+        assert repr(Location('user@[::]:/some/path::archive')) == \
+            "Location(proto='ssh', user='user', host='::', port=None, path='/some/path', archive='archive')"
+        assert repr(Location('user@[::]:/some/path')) == \
+            "Location(proto='ssh', user='user', host='::', port=None, path='/some/path', archive=None)"
+        assert repr(Location('user@[2001:db8::]:/some/path::archive')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::', port=None, path='/some/path', archive='archive')"
+        assert repr(Location('user@[2001:db8::]:/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::', port=None, path='/some/path', archive=None)"
+        assert repr(Location('user@[2001:db8::c0:ffee]:/some/path::archive')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::c0:ffee', port=None, path='/some/path', archive='archive')"
+        assert repr(Location('user@[2001:db8::c0:ffee]:/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::c0:ffee', port=None, path='/some/path', archive=None)"
+        assert repr(Location('user@[2001:db8::192.0.2.1]:/some/path::archive')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::192.0.2.1', port=None, path='/some/path', archive='archive')"
+        assert repr(Location('user@[2001:db8::192.0.2.1]:/some/path')) == \
+            "Location(proto='ssh', user='user', host='2001:db8::192.0.2.1', port=None, path='/some/path', archive=None)"
 
     def test_smb(self, monkeypatch):
         monkeypatch.delenv('BORG_REPO', raising=False)

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-xdist pytest-cov pytest-benchmark
apt-get update apt-get install -y gcc libssl-dev liblz4-dev libacl1-dev libfuse-dev
3.9
requirements.d/development.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
-e git+https://github.com/borgbackup/borg.git@5a667d6f6aad54ba1246fee4dbf54a67f75b80f4#egg=borgbackup
cachetools==5.5.2
chardet==5.2.0
colorama==0.4.6
coverage==7.8.0
Cython==3.0.12
distlib==0.3.9
exceptiongroup==1.2.2
execnet==2.1.1
filelock==3.18.0
iniconfig==2.1.0
msgpack-python==0.5.6
packaging==24.2
platformdirs==4.3.7
pluggy==1.5.0
py-cpuinfo==9.0.0
pyproject-api==1.9.0
pytest==8.3.5
pytest-benchmark==5.1.0
pytest-cov==6.0.0
pytest-xdist==3.6.1
setuptools-scm==8.2.0
tomli==2.2.1
tox==4.25.0
typing_extensions==4.13.0
virtualenv==20.29.3

name: borg
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - cachetools==5.5.2
      - chardet==5.2.0
      - colorama==0.4.6
      - coverage==7.8.0
      - cython==3.0.12
      - distlib==0.3.9
      - exceptiongroup==1.2.2
      - execnet==2.1.1
      - filelock==3.18.0
      - iniconfig==2.1.0
      - msgpack-python==0.5.6
      - packaging==24.2
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - py-cpuinfo==9.0.0
      - pyproject-api==1.9.0
      - pytest==8.3.5
      - pytest-benchmark==5.1.0
      - pytest-cov==6.0.0
      - pytest-xdist==3.6.1
      - setuptools-scm==8.2.0
      - tomli==2.2.1
      - tox==4.25.0
      - typing-extensions==4.13.0
      - virtualenv==20.29.3
prefix: /opt/conda/envs/borg

src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_ssh src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_scp
src/borg/testsuite/helpers.py::test_is_slow_msgpack
src/borg/testsuite/helpers.py::BigIntTestCase::test_bigint src/borg/testsuite/helpers.py::test_bin_to_hex src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_file src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_smb src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_folder src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_abspath src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_relpath src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_with_colons src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_user_parsing src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_underspecified src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_no_slashes src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_canonical_path src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_format_path src/borg/testsuite/helpers.py::TestLocationWithoutEnv::test_bad_syntax src/borg/testsuite/helpers.py::TestLocationWithEnv::test_ssh src/borg/testsuite/helpers.py::TestLocationWithEnv::test_file src/borg/testsuite/helpers.py::TestLocationWithEnv::test_scp src/borg/testsuite/helpers.py::TestLocationWithEnv::test_folder src/borg/testsuite/helpers.py::TestLocationWithEnv::test_abspath src/borg/testsuite/helpers.py::TestLocationWithEnv::test_relpath src/borg/testsuite/helpers.py::TestLocationWithEnv::test_with_colons src/borg/testsuite/helpers.py::TestLocationWithEnv::test_no_slashes src/borg/testsuite/helpers.py::FormatTimedeltaTestCase::test src/borg/testsuite/helpers.py::test_chunkerparams src/borg/testsuite/helpers.py::MakePathSafeTestCase::test src/borg/testsuite/helpers.py::PruneSplitTestCase::test src/borg/testsuite/helpers.py::PruneWithinTestCase::test src/borg/testsuite/helpers.py::StableDictTestCase::test src/borg/testsuite/helpers.py::TestParseTimestamp::test src/borg/testsuite/helpers.py::test_get_cache_dir src/borg/testsuite/helpers.py::test_get_keys_dir src/borg/testsuite/helpers.py::test_get_security_dir src/borg/testsuite/helpers.py::test_file_size src/borg/testsuite/helpers.py::test_file_size_precision src/borg/testsuite/helpers.py::test_file_size_sign src/borg/testsuite/helpers.py::test_parse_file_size[1-1] src/borg/testsuite/helpers.py::test_parse_file_size[20-20] src/borg/testsuite/helpers.py::test_parse_file_size[5K-5000] src/borg/testsuite/helpers.py::test_parse_file_size[1.75M-1750000] src/borg/testsuite/helpers.py::test_parse_file_size[1e+9-1000000000.0] src/borg/testsuite/helpers.py::test_parse_file_size[-1T--1000000000000.0] src/borg/testsuite/helpers.py::test_parse_file_size_invalid[] src/borg/testsuite/helpers.py::test_parse_file_size_invalid[5 src/borg/testsuite/helpers.py::test_parse_file_size_invalid[4E] src/borg/testsuite/helpers.py::test_parse_file_size_invalid[2229 src/borg/testsuite/helpers.py::test_parse_file_size_invalid[1B] src/borg/testsuite/helpers.py::TestBuffer::test_type src/borg/testsuite/helpers.py::TestBuffer::test_len src/borg/testsuite/helpers.py::TestBuffer::test_resize src/borg/testsuite/helpers.py::TestBuffer::test_limit src/borg/testsuite/helpers.py::TestBuffer::test_get src/borg/testsuite/helpers.py::test_yes_input src/borg/testsuite/helpers.py::test_yes_input_defaults src/borg/testsuite/helpers.py::test_yes_input_custom src/borg/testsuite/helpers.py::test_yes_env src/borg/testsuite/helpers.py::test_yes_env_default src/borg/testsuite/helpers.py::test_yes_defaults src/borg/testsuite/helpers.py::test_yes_retry src/borg/testsuite/helpers.py::test_yes_no_retry src/borg/testsuite/helpers.py::test_yes_output src/borg/testsuite/helpers.py::test_yes_env_output src/borg/testsuite/helpers.py::test_progress_percentage_sameline src/borg/testsuite/helpers.py::test_progress_percentage_step src/borg/testsuite/helpers.py::test_progress_percentage_quiet src/borg/testsuite/helpers.py::test_progress_endless src/borg/testsuite/helpers.py::test_progress_endless_step src/borg/testsuite/helpers.py::test_partial_format src/borg/testsuite/helpers.py::test_chunk_file_wrapper src/borg/testsuite/helpers.py::test_chunkit src/borg/testsuite/helpers.py::test_clean_lines src/borg/testsuite/helpers.py::test_format_line src/borg/testsuite/helpers.py::test_format_line_erroneous src/borg/testsuite/helpers.py::test_replace_placeholders src/borg/testsuite/helpers.py::test_swidth_slice src/borg/testsuite/helpers.py::test_swidth_slice_mixed_characters src/borg/testsuite/helpers.py::test_safe_timestamps src/borg/testsuite/helpers.py::TestPopenWithErrorHandling::test_simple src/borg/testsuite/helpers.py::TestPopenWithErrorHandling::test_not_found src/borg/testsuite/helpers.py::TestPopenWithErrorHandling::test_bad_syntax[mismatched src/borg/testsuite/helpers.py::TestPopenWithErrorHandling::test_bad_syntax[foo src/borg/testsuite/helpers.py::TestPopenWithErrorHandling::test_bad_syntax[] src/borg/testsuite/helpers.py::TestPopenWithErrorHandling::test_shell
src/borg/helpers.py
src/borg/helpers.py