Different results for ls when accessing Ubuntu via SSH from different clients (Windows-PuTTy, Mac-Terminal)

Consider the following directory structure, printed to the screen using ls -l "$dir" ($dir is a bash variable standing for some folder on the same server, a few levels up), on a remote Ubuntu server (more precisely, Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-116-generic x86_64)):

-rw-r--r-- 1 user group     12 Apr  2 12:34 FILE1
-rwxr--r-- 1 user group    287 Apr 23  2017 File_
-rw-r--r-- 1 user group      0 Apr 21  2017 file_1
-rw-r--r-- 1 user group    272 Apr  3 22:16 myFile
-rw-r--r-- 1 user group  47633 Apr 11 14:01 myFile.txt
-rw-r--r-- 1 user group  37030 Apr 11 14:16 myfile1
-rw-r--r-- 1 user group      0 Apr 11 21:23 nothing_here
-rw-r--r-- 1 user group      2 Apr 22  2017 small_file
-rwxr-x--- 1 user group 262529 Apr  8 15:31 story

This is coming from a MacBook, and using the built-in terminal and the out-of-the-box installation of ssh (OS X Version 10.13.3, as the terminal tells me).

Consider, in comparison, the same server accessed from a Windows machine running PuTTy release 0.70:

-rwxr--r-- 1 user group    287 Apr 23  2017 File_
-rw-r--r-- 1 user group      0 Apr 21  2017 file_1
-rw-r--r-- 1 user group     12 Apr  2 12:34 FILE1
-rw-r--r-- 1 user group    272 Apr  3 22:16 myFile
-rw-r--r-- 1 user group  37030 Apr 11 14:16 myfile1
-rw-r--r-- 1 user group  47633 Apr 11 14:01 myFile.txt
-rw-r--r-- 1 user group      0 Apr 11 21:23 nothing_here
-rw-r--r-- 1 user group      2 Apr 22  2017 small_file
-rwxr-x--- 1 user group 262529 Apr  8 15:31 story

Notice the different sorting of the first 6 files in each code block.

Considering that the commands are running on the server, and not on the client computers, one would expect no difference between the two outputs. What's the reason they're not identical, then?

Edit 1: As mentioned in the comments, I've checked and the Mac's terminal is set to xterm-256color while the Windows one is set to xterm. A quick change of the Mac's setting to xterm had no effect.

Edit 2: As mentioned in the comments, I've tried running whereis ls, which gives the reasonable location you'd expect to find ls - i.e. bin/ls.

Edit 3: Got rid of the terminal images, traded them for properly-formatted code as per this meta post.

Edit 4: Added quotes around "$dir" to prevent problems with spaces.


Solution 1:

Sorting order is defined by the locale variables LC_ALL, LC_COLLATE and LANG (in this order, the first being the strongest). These are also some of the few variables that typically can be set by the ssh client. Check and compare these variables. You might want to set them to a well-defined value on the server (e.g. in .bash_profile), or make sure that your ssh clients set the same consistent value.

Solution 2:

What eventually worked was the "Stop Forwarding Locale From The Client" sub-section in this answer. Just comment out out the SendEnv LANG LC_* line under Host in the local /etc/ssh/ssh_config file. Some explanation about what it does can be found here.