chttl

chttl

chttl — Temporarily override the system's default TTL.

What does it do?

It's a simple wrapper for the command necessary to change the TTL of network packets, which you might need to do when tethering Internet access from the cellular networks of some carriers.

Wouldn't an alias be shorter to do?

Yes, but the command changes across operative systems. You'd need an alias for each, which is a bit more complicated to sync unless you add some logic to your aliases file, but wouldn't that be just about the same? bash config files, whatever.

Is there something else?

- This is the first [intentionaly] POSIX-compliant script we've ever written.
- It works on macOS, Linux and FreeBSD.
- It can be used to set the value back to where it was as well, simply add it at the end.
- It's not a permanent modification.

  1. Depending on the OS, you might need to create a file in a specific location or issue a special command that sets preference files.

Make it a function

This was originally written as a bash function. It was changed early on to sh for portability reasons but nevertheless it was wrapped in a single unit to make it easy to change it back. All you need is to remove the shebang (first) line and the call out or invocation (last) line from the script. Then paste the remainder of the code in your bash functions file(s).

Using a functions file

As a login shell, bash process config files in the following order: - /etc/profile - ~/.bash_profile - ~/.bash_login, and - ~/.profile, Typically ~/.bash_profile would call ~/.bashrc which will be executed every single new CLI session. i.e;
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
(meaning "if ~/.bashrc exists, run it ".)

You may follow the bash's own example by invoking the functions file from ~/.bash_profile e.g; if [ -f "${HOME}/bin/bashfunctions" ]; then . "${HOME}/bin/bashfunctions"; fi where the file was created to live alongside your scritps on "${HOME}/bin" and it would be a much easier job syncing things across systems.

[Sun05@23:20:52][vuser@vpro:~] $〉ls
total n
 24 -rw-------    1 vuser  vgroup    12K Jan  5 23:04 .bash_history
  8 -rw-r-xr-x@   1 vuser  vgroup   1.7K Oct 25 16:51 .bash_profile*
########################################################################################################
#↑↑↑ Add 'if [ -f "${HOME}/bin/bashfunctions" ]; then . "${HOME}/bin/bashfunctions"; fi'            ↑↑↑#
########################################################################################################
  8 -rw-r--r--    1 vuser  vgroup   1.5K Jul  3  2024 .bash_profile.bak
  8 -rw-r-xr-x@   1 vuser  vgroup   1.1K Mar 27  2024 .bash_profile.pysave*
  0 drwx------   16 vuser  vgroup   512B Oct  7 16:09 .bash_sessions/
 16 -rw-r-xr-x@   1 vuser  vgroup   4.7K Oct 14 16:01 .bashrc*
  0 drwxr-xr-x  172 vuser  vgroup   5.4K Jan  5 16:18 bin/

[Sun05@23:50:41][vuser@vpro:~] $〉ls bin
total n
   0 drwxr-xr-x  12 vuser  vgroup   384B Mar 20  2024 .git/
   8 -rwxr-xr-x   1 vuser  vgroup    86B May  1  2022 .zshrc*
   0 drwxr-xr-x  17 vuser  vgroup   544B Sep 22  2020 argbash/
   0 drwxr-xr-x   2 vuser  vgroup    64B Feb  9  2023 assets/
   8 -rwxr-xr-x   1 vuser  vgroup   102B May  1  2022 backupbashp*
   8 -rwxr-xr-x   1 vuser  vgroup   2.6K Oct 18 13:02 bashaliases*
  16 -rwxr-xr-x@  1 vuser  vgroup   6.8K Jan  5 00:10 bashfunctions*
########################################################################################################
#↑↑↑         Functions file example                                                                 ↑↑↑#
########################################################################################################
   8 -rwxr-xr-x   1 vuser  vgroup   3.5K Nov  1 17:20 bashvars*
   8 -rwxr-xr-x   1 vuser  vgroup    30B May  1  2022 catprivkey*
   8 -rwxr-xr-x   1 vuser  vgroup    34B May  1  2022 catpubkey*
  40 -rwxr-xr-x   1 vuser  vgroup    20K Feb 25  2023 ciscokvm*
   8 -rwxr-xr-x   1 vuser  vgroup   119B Oct  3 02:04 codehere*
   8 -rwxr-xr-x   1 vuser  vgroup   235B May  1  2022 fixsmb*
   8 -rwxr-xr-x   1 vuser  vgroup    55B May  1  2022 fixsshperms*
   8 -rwxr-xr-x@  1 vuser  vgroup   1.0K Jul 11 07:30 flushdns*
   8 -rwxr-xr-x   1 vuser  vgroup   128B May 28  2022 getcerts*
   8 -rwxr-xr-x   1 vuser  vgroup   262B May 28  2022 getmstr*
   8 -rwxr-xr-x   1 vuser  vgroup    51B May  1  2022 hapchk*
   8 -rwxr-xr-x   1 vuser  vgroup   243B May  1  2022 hapcmd*
   8 -rwxr-xr-x   1 vuser  vgroup    52B May  1  2022 hapinfo*
   8 -rwxr-xr-x   1 vuser  vgroup    40B May  1  2022 hapjrnl*
  64 -rwxr-xr-x   1 vuser  vgroup    30K Oct 23  2022 hapreset*
   8 -rwxr-xr-x   1 vuser  vgroup    51B May  1  2022 hapstart*
   8 -rwxr-xr-x   1 vuser  vgroup    50B May  1  2022 hapstop*
   8 -rwxr-xr-x@  1 vuser  vgroup   2.6K Nov 11 17:49 heupdate*
   8 -rwxr-xr-x   1 vuser  vgroup   1.8K Oct 31 17:59 lopkg*
   8 -rwxr-xr-x   1 vuser  vgroup   1.9K Oct 23  2022 lports*
   8 -rwxr-xr-x   1 vuser  vgroup   272B May  1  2022 lsvch*
  40 -rwxr-xr-x   1 vuser  vgroup    17K Apr 22  2024 pppkg*
  48 -rwxr-xr-x   1 vuser  vgroup    21K Jan  1  2023 ppull*
  32 -rwxr-xr-x@  1 vuser  vgroup    15K Apr  9  2024 ppush*
  24 -rwxr-xr-x   1 vuser  vgroup    11K Nov  3  2022 printMESSAGE*
   8 -rwxr-xr-x   1 vuser  vgroup    80B May  1  2022 rmkh*
  16 -rwxr-xr-x   1 vuser  vgroup   5.6K Jan 21  2023 rmkhk*
   8 -rwxr-xr-x   1 vuser  vgroup    41B Dec  7  2022 vimhosts*
   8 -rwxr-xr-x   1 vuser  vgroup    30B May  1  2022 vimsshconfig*
  40 -rwxr-xr-x@  1 vuser  vgroup    19K Nov 18 09:42 vncadeploy*
 176 -rwxr-xr-x   1 vuser  vgroup    84K Jan 23  2023 vprep*
   8 -rwxr-xr-x   1 vuser  vgroup   3.8K Oct 20  2023 vsphere2unraid*
   8 -rwxr-xr-x   1 vuser  vgroup   505B Jan 22  2024 xdex*
   8 -rwxr-xr-x   1 vuser  vgroup   403B Dec 17  2022 zfsarctune*

Syntax

The script takes one or no arguments; any additional argument(s) after the first one should be ignored. When it's run without arguments, it defaults to setting a TTL of 65. e.g;

chttl

If you add any value other than -h, –help or -? it will attempt to set that value as the TTL. There's no validation of the input, but naturally if it's not a numerical value within bounds, it will fail e.g;

Reset to the common value:

chttl 64

or change it to a TTL of 128:

chttl 128

or to 1:

chttl 1

or just add the value anyway:

chttl 65

If the argument does match -h, –help or -?, it'll show a quick short guide and exit e.g;

chttl -h

If you add both types of arguments, it will only consider the first one, thus:

chttl -h 22

should show the quick guide and:

chttl 22 -h

should set the TTL to 22.

Code download

chttl
#!/usr/bin/env sh
chttl() {
	chttlhelp(){
		cat <<- _help
chttl — Temporarily override the system's default TTL.
Copyright (C) 2025  Gustavo Domínguez  (GPLv3)
# $0                Set TTL to 65.
# $0 n              Set TTL to n.
# $0 -h|--help|-?   Print this message.
 
Superuser(root)-level privileges are required.
		_help
	}
	chttldo(){
		if [ "$(uname)" = Darwin ]; then
			sysctl net.inet.ip.ttl="${1:-65}"
		elif [ "$(uname)" = Linux ] ; then
			sysctl -w net.ipv4.ip_default_ttl="${1:-65}"
		elif [ "$(uname)" = FreeBSD ] ; then
			sysctl net.inet.ip.ttl="${1:-65}"
		elif uname | grep -i "bsd"; then
			#sysctl net.inet.ip.ttl=65
			echo "Run 'sysctl net.inet.ip.ttl=65' if you think it's safe"
			echo "for this BSD version (only FreeBSD has been tested.)"
		fi
	}
	case "$1" in
		-h|--help|-?) chttlhelp ;;
		*) chttldo "$1" ;;
	esac
}
chttl "$1"

On Linux systems, you can drop it in the bin subdirectory of your home folder (~/bin or $HOME/bin) to make it available anywhere on the CLI. Don't forget to make it executable first (e.g; chmod +x $HOME/bin/chttl).

A note about privileges

This a task that requires superadmin privileges. The script does not attempt to elevate your permissions at any time; you will need to do that yourself by prepending sudo, being logged in as root, or however you wish. That is your call.

Anti-corporate takeover measure (licensing)

chttl — Temporarily override the system's default TTL.
Copyright (C) 2025 Gustavo Domínguez

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/

en/utility-scripts/chttl.txt · Last modified: 2025/01/06 02:46