Patch: Custom hostname

Rating: 
5
Your rating: None Average: 5 (1 vote)

Simple changer of default hostname "Sailfish" to your own one.
It use 2 commands:
echo YOUR_NAME > /etc/hostname
hostname YOUR_NAME
or this one:
hostnamectl set-hostname YOUR_NAME

Uninstallation will restore default hostname "Sailfish".
If you wrote the name with spaces, my script will replace them to this symbol "-".

v0.1-8
- For v2.1.2.3 and above.
- Added icon for z1.75.
- QML tuning.

v0.1-7
- For v2.1.1.12
- Now it use only hostnamectl option (uppercase letters allowed).

v0.1-6
- Changes in auto-filter. Now the same for both methods. Added auto-removing non-ascii letters.
- Fixed empty hostname, user will get notification.

v0.1-5
- Changes in auto-filter. Added switcher when you can choose to remove all symbols or just replace them by "-".

v0.1-4
- Added auto-filter to allow only a-z, A-Z, 0-9 and space will be replaced by "-".

v0.1-3
- Added option to use hostnamectl command instead simple edit.

v0.1-2
- Removed dependencies of active dev mode..

v0.1-1
- Initial release (Requires: Developer mode).

Beware, use it on your own risk !!!

Donation are welcome :)

Screenshots: 
Application versions: 
AttachmentSizeDate
sailfishos-customhostname-0.1-6.i486.rpm16.37 KB01/07/2017 - 21:19
sailfishos-customhostname-0.1-6.armv7hl.rpm21.74 KB01/07/2017 - 21:19
sailfishos-customhostname-0.1-7.i486.rpm16.2 KB03/07/2017 - 21:52
sailfishos-customhostname-0.1-7.armv7hl.rpm21.56 KB03/07/2017 - 21:52
sailfishos-customhostname-0.1-8.i486.rpm16.35 KB04/10/2017 - 21:04
sailfishos-customhostname-0.1-8.armv7hl.rpm24.41 KB04/10/2017 - 21:04
Changelog: 

- For v2.1.2.3 and above.
- Added icon for z1.75.
- QML tuning.

Comments

olf's picture

Nice, thank you.
But are all these dependencies (which are also pulled in, when installing "Custom hostname") really necessary?
At least for setting the hostname via systemd (e.g. per hostnamectl -P set-hostname your-name) they should be superfluous.

P.S.: Capital letters in hostnames are very unusual (or even problematic; I have not investigated that), thus you may consider converting the whole user provided string to lowercase (just as you already convert space characters to underline characters). *Edit:* This is bullshit, see newer comment (referencing RFC819, appendix A).

P.P.S: Not requiring Developer Mode to be active (if technically possible in a Patch; just as "Filecase root", "CDock root", "FBRoot" and "Tiny root" from Warehouse do) would be a great enhancement of this Patch.

Schturman's picture

Added new version without dependencies of active dev mode, try it...

Schturman's picture

hmmm... this packages: sailfish-utilities, cutes-js, cutes-json-js, cutes-narwhal, python, dbus-python I need for running script and notification from qml.
This 2: jolla-developer-mode, jolla-developer-mode-profile... hmmm why needed to change hostname if dev mode not activated and fingerterm not installed ?

olf's picture

- WRT dependencies: Sorry, I did not capture the two or three RPMs, which were pulled in as dependencies, when I originally installed "Custom hostname", but all the RPMs you mention above would not have surprised me (actually sailfish-utilities, python and dbus-python were definitely installed before, fingerterm as well, but developer mode inactive).
But never mind, I just wanted make sure this was correct: You double checked, so the dependencies should be O.K.

- Reasons to change the hostname:

a. Changing the usual shell prompt is rather a side effect of setting a different hostname, as the shell prompt can be set to contain arbitrary strings much more easily by setting the shell environment variable PS1. (For bash, user specific changes to the shell environment should be set in ~/.bash_profile.)
IMHO, setting the hostname is primarily about setting the network host name (I still have to check though, if that works properly. *Edit:* Done, works, may take a restart of the network services per sailfish-utilities or a reboot to become effective on the network side.).

b. One does not need to have the Developer Mode activated to utilise a shell. I always had fingerterm installed, but devel-su disabled for a while, because I primarily use the shell as a regular user for file operations, downloading with curl, editing files with vi, etc. Becoming root is only occasionally necessary for me.
And yes, it is a visually nice side effect of changing the hostname alters the shell prompt in every user's shell on a device (though there are only two interactive ones under SFOS, currently: nemo and root).

P.S.: On Linux distributions with systemd (as SFOS), I still believe using its own tools (here: hostnamectl) is the proper way to alter system settings, not by "classic UNIX style" editing of files in /etc and using classic system configuration commands (in this case: the hostname command).

Schturman's picture

Added new version with hostnamectl option, try it :) One intresting thing, in my Jolla 1 with v2.1.1.12 the hostnamectl can save names with Uppercase latters without any problem :) After official sys update I will update package with using only hostnamesctl :)

olf's picture

a. Thank you. Installed, applied and activated this new option: I will report, if i perceive anything unusual.

b. WRT allowed characters in hostnames: See other comment (referencing RFC819, appendix A).

c. Sounds like a good plan. :)

Schturman's picture

b. you mean to switch from "_" to "-" ? If yes, in the next version :)

olf's picture

Yes, as I wrote in https://openrepos.net/comment/19133#comment-1913 , appendix A of RFC819 states:

The *only* allowed characters in a hostname are [a-z,A-Z,0-9], and (but not as first character) the hyphen ("-").

Hence I suggest converting spaces (" "), underscores ("_"), dots (".") commas (","), colons (":"), semicolons (";") and *every* other character, which is not [a-z,A-Z,0-9,-], of the user input to a hyphen ("-"). After this conversion, eliminate all leading hyphens, so the hostname string definitely starts with a [a-z,A-Z,0-9].

Example:
1. User input:
+-My.Hostname=666,1-3;Üç#(me@here?)!
2. Convert every non-[a-z,A-Z,0-9,-] to "-":
--My-Hostname-666-1-3-----me-here---
3. Eliminate leading hyphens (i.e. one or more "-" at the start of the string):
My-Hostname-666-1-3-----me-here---

Or (if that can be done easily) prohibit to input anything but [a-z,A-Z,0-9,-], and then eliminate all leading hyphens.

Schturman's picture

Added new version with auto-filter, test it and report. Thanks

olf's picture

Thank you for releasing a new version of this Patch quickly, but unfortunately there are two significant issues:

1. There is no user feedback after filtering. So the user expect the string in the input field to be the actual hostname after hitting "Apply" button.

The easiest solution may be to write the filtered string back to the input field, when applying the hostname string.
Another (supposedly more complicated, but nicer) solution would be to disallow typing forbidden characters into the hostname field.

2. Tested the actual filtering in v0.0.1-4 with the hostname example string from above.

2a. When using the classic method:
[nemo@MyHostname66613Üçmehere ~]$
- "@Üç" are *not* in [a-z,A-Z,0-9,-], so they should be discarded or substituted.
- "-" is discarded, but should not.
Expected result is
(substituting forbidden characters with "-") "My-Hostname-666-1-3-----me-here---"
or (discarding forbidden characters) "MyHostname6661-3mehere".

2b. When using the hostnamectl method:
[nemo@myhostname66613mehere ~]$
- The same two issues as described under 2a.
- Expected result under SFOS 2.1.0.11 is
(substituting) "my-hostname-666-1-3-----me-here---"
or (discarding) "myhostname6661-3mehere".

HTH & kind regards

Schturman's picture

Ok, I think I implemented all what you asked :) Please, try new version and report here.

Schturman's picture

1. Done

2a-2b. Done. But I have a question - it's not better just remove all special characters instead to convert them to "-" ? and convert only spaces to "-" ? In this case if you want this symbol in your hostname just write the name with spaces, for example:

My name Baba -> you will get My-name-Baba

3. Do you know what the maximum allowed length for hostname?
P.S. found it should be 64 characters.. but unicode (non-latin) characters should be less and for now I don't know how to implement this checking, probably I will left it with 64 for now.

olf's picture

Much better, thanks.
Specifically, WRT ...

1. Nice.

2. Sorry, that was a misunderstanding: I merely meant to *suggest* either discarding or substituting forbidden characters. As my own hostnames only contain [a-z], your Patch was doing what I wanted starting with its first version. Still I am glad you added the hostnamectl method, and plan to completely switch to it, when SFOS 2.1.1-final is released.
Yes, you are absolutely right, that substituting some forbidden characters (e.g. space " " and underscore "_" with a hyphen "-"), while simply removing others (all other special characters and all hyphens "-" at the start of the hostname string) is even nicer. But there is a point at which one has to stop such considerations, as the next logical step would be to think about substituting "ç" with "c", "Ç"-->"C", [őøöôòó]-->"o", and so on. I would have stopped one step earlier and chosen either "always substitute" or "always remove". But hey, this Patch is "your baby", so I can only suggest what I believe is best for the average user, and it is up to you what you decide for and finally implement. Stating "Won't fix" / " Won't implement that" is also always a possible option as a Free Software coder, as we are all doing this voluntarily, and primarily to satisfy our own needs.

But I actually think you should better *not* provide the user with an option to switch off the filtering: Just choose a filter scheme, which is *always* applied when hitting the "Apply" button. Then the string "Name without special characters" could be removed, also.

3. I believe the character set for hostnames is still (classic 7-bit) ASCII, at least on the network side. I don't know what a modern shell as bash uses for its environment variables and shell prompt, but busybox is likely to use extended (8-bit) ASCII. Mmh, if you really want to be sure about this, I can try to clarify the situation for SFOS 2 (just let me know), but ...
a. All allowed characters ([a-z, A-Z, 0-9,-]) are within the 7-bit ASCII range, so they will be encoded to a single byte in ASCII encoding and variable length Unicode encodings.
b. If something (e.g. a shell or systemd) uses UCS-2 encoding, every character is encoded in two bytes.
x. Other Unicode encodings (than UCS-2 and variable length encodings) are not practically relevant, IMO.
Note that, while the quick answer is "hostname has to consist of 1 to 63 (not 64!) ASCII characters" (see e.g. https://technet.microsoft.com/en-us/library/cc959867.aspx), this answer has become much more complicated (see e.g. https://stackoverflow.com/a/28918017 for a concise *and* additionally a detailed answer). In short, the network hostname should be at most between 19 and 31 characters long, depending on the use case, but must be at least one character long.

So when taking all this into account, I suggest limiting the input in the hostname field to 31 characters to be on the safe side for network name and shell prompt.

4. Testing v0.0.1-5 with filtering on.
a. Input (again): +-My.Hostname=666,1-3;Üç#(me@here?)!
- Classic method: --> MyHostname6661-3Üçmehere
Not O.K., e.g. "Ü" and "ç" are not removed.
- hostnamectl method: --> myhostname6661-3mehere
O.K., works nicely.
b. Empty input string: Notification "Nothing to change!". Very good!
c. Played with some strings containing only spaces " " and hyphens "-", plus some strings as "- +_":
- Empty hostname (forbidden) set with classic method: Not O.K.!
- Hostname set to "resetname" with hostnamectl method: O.K. and nice.
I have not looked at the code, but you seem to use a different filtering for the classic and the hostnamectl method. The filtering for the hostnamectl method seems to work well, while the filtering for the classic method is buggy. Wouldn't it be easier to filter the input string first (with a single filtering scheme / code section), and then converting to lowercase for the hostnamectl method?

HTH & best regards.

Schturman's picture

And.. Try new version :)

Schturman's picture

2. I will add filter that will remove all non-ascii charcters and it will remove letters like "Üç". Just now noticed that hostnamectl not allow non-ascii charcters, it automatically remove them. That mean in this case user with different languages (hebrew, russian etc..) CAN'T change the hostname to his languages. That mean only latin letters available to change hostname. 

3. Because hostnamectl automatically remove non-ascii letters I can left 63 characters long.

4a. Both methods will get the same result about Üç letters = will be removed.
4c. Added checking command and if empty, both methods will get notification like in 4b.

The filter always applied and is the same for both methods and run before the actual changing command, in new version (0.1-6) it should set the same result. User only choose if to use "Remove all characters" = remove special charcters or if not checked = special characters will be replaced by hyphen automatically.

Text "Name without special characters" changed to "Custom name".

Emil

olf's picture

Cool, filtering (both, substituting and removing space characters) and length limiting are working perfectly for both methods (classic and hostnamectl).

Cheers!

Schturman's picture

Good! Thanks for reporting :)

Schturman's picture

Yes, I will do this. Maybe tomorrow ;)

Schturman's picture

a. Ok.
b. Yes it nice to see different names, especially if you use a few devices at the same time (1, C and tablet).
c. I use this more than one week and don't see any ptoblem of changing hostname by editing file instead hostnamectl... I like the capital letters :) Also default hostname is "Sailfish", not "sailfish"...

P.S. you know what, I will add option for user to choose how edit by hostnamectl or by regular edit :D

olf's picture

b. Exactly, but this is also very useful in the network (use case a.).

c. Sorry, I was too slow:
Yes, capital letters are absolutely legitimate in hostnames, they are explicitly allowed (see section 2.3.3 in https://tools.ietf.org/html/rfc1035).
Now I did the job thoroughly ( :) ): The original reference seems to be appendix A in https://tools.ietf.org/html/rfc819 (back in the really early days of the internet).
Allowed characters are [a-z,A-Z,0-9], and (but not as first character) the hyphen ("-").
So spaces (" "), underscores ("_"), dots (".") commas (","), colons (":"), semicolons (";") and *every* other character, which is not [a-z,A-Z,0-9], should be converted to a hyphen ("-");
after this conversion, eliminate all leading hyphens, so the hostname string starts with [a-z,A-Z,0-9].

giskard's picture

systemd offers a command that does both operations (edit /etc/hostname and set hostname): hostnamectl

Schturman's picture

P.S. it have one small problem, it can't save name with capital letters :)

Schturman's picture

Thanks, good to know :)