Join fields from files with duplicate lines

I have two files,

file1.txt:

1 abc
2 def
2 dgh
3 ijk
4 lmn

file2.txt

1 opq
2 rst
3 uvw

My desired output is:

1 abc opq
2 def rst
2 dgh rst
3 ijk uvw

I have tried using the 'join' command (ie. 'join file1.txt file2.txt') but it returns the error that file1.txt is not sorted because of the repeated value (ie. 2).

Does anyone know how to solve this? Thanks!

I am getting your desired output running:-

join file1.txt file2.txt
1 abc opq
2 def rst
2 dgh rst
3 ijk uvw

Re-try specifying the field number:-

join -1 1 -2 1 file1.txt file2.txt
1 abc opq
2 def rst
2 dgh rst
3 ijk uvw

for unsorted file1.txt try:

awk 'NR==FNR{a[$1]=$2;next;}a[$1]{print $0,a[$1]}' file2.txt file1.tx

for all lines out try:

awk 'NR==FNR{a[$1]=$2;next;}{print $0,a[$1]}' file2.txt file1.txt