7

the command dmesg | grep console returns:

Kernel command line: console=ttys6,115200 root=/dev/mmcblk1p2 
rootwait rw
console [ttys6] enabled

I want to change this to ttys3 after booting. Is it possible?

3 Answers 3

5

The man page for tty_ioctl lists the ioctl TIOCCONS. When applied to an open file descriptor of a tty it will redirect future output intended for /dev/console to that tty.

You can use this in a simple perl script. Create and chmod +x a file mysetconsole holding the following:

#!/usr/bin/perl
# https://unix.stackexchange.com/a/397790/119298
# see man tty_ioctl for TIOCCONS
# and perldoc IO::Tty::Constant
require "sys/ioctl.ph";
use IO::Tty::Constant qw(TIOCCONS);
ioctl(STDIN,TIOCCONS,0) or die $!;

Assuming you can open the wanted device, use it simply as

sudo ./mysetconsole </dev/ttys3

You will not be able to use it again until you set the console back to /dev/console, with

sudo sh -c './mysetconsole </dev/console'

You may get perl warnings about _FORTIFY_SOURCE which can be ignored. You will need rpm package perl-IO-Tty or debian package libio-pty-perl. If you prefer you can just look for the definition of TIOCCONS in the system include files. I found mine in:

/usr/include/asm-generic/ioctls.h: #define TIOCCONS      0x541D

Your perl script can then just be

#!/usr/bin/perl
# https://unix.stackexchange.com/a/397790/119298
sub TIOCCONS{ return 0x541D; }
ioctl(STDIN,TIOCCONS(),0) or die $!;
3
  • 1
    This would be a great answer if it included a shell command to call that ioctl. I could have sworn dmesg could do this way back when, but the man page now says otherwise. I know there was some command I used to use to make an Xterm the console way back when, but can't remember it for the life of me.
    – psusi
    Commented Oct 13, 2017 at 0:04
  • @psusi I added a perl script. xterm -C will become console, but you need to be root these days, and it doesn't fit the questioners requirements.
    – meuh
    Commented Oct 13, 2017 at 6:27
  • Ahh, I thought it involved a -c... would make more sense if it was dmesg that did this rather than xterm though.
    – psusi
    Commented Oct 15, 2017 at 2:39
1

For ease of use, many Linux systems provide setconsole(8), that does the same underlying operations as the Perl snippets.

Checking the return is important, it might fail.

$ setconsole /dev/ttyS1 ; echo $?
0

Proof of the internals:

$ strace -ff setconsole /dev/ttyS1
...
openat(AT_FDCWD, "/dev/ttyS1", O_WRONLY|O_NOCTTY) = 3
ioctl(3, TCGETS, {B115200 opost isig icanon echo ...}) = 0
ioctl(3, TIOCCONS)                      = 0
...
-1

Short anwser: use chvt Long anwser: How Linux configures the TTYs at boot depends on distribution. It could be /etc/inittab or it could be /etc/rc.local On systemd systems it's automatic based on configurations in logind.conf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .