-1

My uname -r (and as is my real problem, Java system property os.version) returns a version number that has dashes in it. It looks like: 5.9.12-gentoo-blah-blah.

This breaks an app I'm using which doesn't like the dashes in the version number (it expects just a number, no dashes allowed).

How can I change the version number of my operating system? I just want to return the kernel version and don't worry about the rest.

14
  • 5
    This breaks an app I'm using which doesn't like the dashes in the version number (it expects just a number, no dashes allowed). That app is the problem, so I'd go ahead and fix the app. You're also running it, it seems, so you can basically preload and modify system libraries. Either you patch os.version, or you patch that application. I'd have a clear favourite. Commented Jan 6, 2022 at 13:25
  • 1
    @binki: Java doesn't call uname executable, it uses the uname(2) system call. (Found this using your suggestion of strace. I don't care if I change my OS version system wide, I just need to know how to do it at all, not just for this one application. Commented Jan 6, 2022 at 23:13
  • 2
    But people are right when they say that the package is broken. The Java package is parsing the version string in a way which will break almost everywhere. Also, it is a Java package—why is it even checking the kernel version? The developers need to fix that. However, as people said, you can just use LD_PRELOAD to intercept its call to uname(2) once you figure out how java resolves that through glibc and then you can fool just java about the kernel version. Commented Jan 7, 2022 at 3:37
  • 2
    @dave I hacked up the LD_PRELOAD approach for fun: github.com/binki/binki-fool-java-os-version Commented Jan 7, 2022 at 4:45
  • 3
    You can override system calls in a statically linked executable with ptrace. It's a bit more complicated than LD_PRELOAD. See e.g. stackoverflow.com/questions/13426561/…, nullprogram.com/blog/2018/06/23 Commented Jan 7, 2022 at 8:38

4 Answers 4

0

The OS release is returned from the kernel by the uname system call (man 2 uname for limited information about it). The information can also be found under /proc/sys/kernel/osrelease. That file is read-only and there's no way to change the OS release of a kernel after it has been built.

The OS release is set in the Makefile of the kernel. The first 4 lines are:

VERSION =
PATCHLEVEL =
SUBLEVEL =
EXTRAVERSION =

It's also possible that extra information is added during the build if you build from a version control repository.

The kernel from Gentoo adds an EXTRAVERSION which has dashes in it. Before building the kernel modify the Makefile to set the version to what you want.

1
  • 1
    For the record, see setarch --uname-2.6 uname -rs (doesn't change the contents of /proc/sys/kernel/osrelease, and doesn't remove those -s though). Commented Jan 7, 2022 at 15:30
0

One possible solution could be using LD_PRELOAD to intercept the uname system call. I found a gist doing this.

1
-3

I hope it help:

Create bash file, e.g my_uname.sh:

#!/usr/bin/env bash

REPLACE_ON="~"
flag=$1
os_uname=$(/usr/bin/uname $flag)
pat="(a|r)"
if [[ "$flag" =~ $pat ]]; then
    echo $os_uname | sed "s/-/$REPLACE_ON/g"
else
    /usr/bin/uname $flag
fi

Watch your PATH variable for new script place

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/13/bin

Set execution flag, and copy shell-script to the search PATH

chmod +x my_uname.sh
cp my_uname.sh /usr/local/bin/

After that create alias:

alias uname=my_uname.sh

Also you can put alias in .bashrc or .bash_profile

1
  • Except most programs don't call uname but use the syscall interface to get the information displayed there. And even if they did then the alias only overrides uname when called from a shell where aliases are replaced and not from a program that calls uname Commented Jan 7, 2022 at 2:42
-5

If you just need the output of uname -r without the dashes and feed that as input to another process, then I would use sed:

uname -r | sed 's/-//g'

EDIT: if you think this answer is not useful, please explain why, so I can learn from your answer. We're all here to learn, not just to bash (if you pardon the pun).

4
  • As mentioned in my question the problem is the OS version as returned by the OS (specifically to Java, but more generally to uname). Commented Jan 6, 2022 at 15:16
  • Thanks. So, why not use ´sed´ to just return the version number using a regular expression? Get rid of all characters except numbers and dots. Unless you cannot use an external command in-between the output of ´uname -r´ and the app, in which case I misread your initial question. Commented Jan 6, 2022 at 15:32
  • I don't run uname -r, I run an app which gets the OS version information, the same information returned by uname -r. I need that to be different inside the app. So I want to change my environment to return a different version string. Commented Jan 6, 2022 at 15:42
  • Gotcha! Apologies, learning every day... Commented Jan 6, 2022 at 18:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.