Setting Environment Variable Globally Without Restarting Ubuntu

Setting environment variable globally without restarting Ubuntu

This perl program uses gdb to change the USER variable in all currently running bash shells to whatever is given as the program arg. To set a new variable the internal bash call "set_if_not" could be used

#!/usr/bin/perl

use strict;
use warnings;

my @pids = qx(ps -C bash -o pid=);
my $foo = $ARGV[0];
print "changing user to $foo";
print @pids;

open( my $gdb, "|gdb" ) || die "$! gdb";
select($gdb);
$|++;
for my $pid ( @pids ) {
print "attach $pid\n";
sleep 1;
print 'call bind_variable("USER","' . $foo . '",0)' . "\n";
sleep 1;
print "detach\n";
}

This only works with bash ( I only tested it with version 4.1 on Ubuntu 10.04 LTS) and does not alter the environment for arbitrary already running programs. Obviously it must be run as root.

How to set environment variable for everyone under my linux system?

As well as /etc/profile which others have mentioned, some Linux systems now use a directory /etc/profile.d/; any .sh files in there will be sourced by /etc/profile. It's slightly neater to keep your custom environment stuff in these files than to just edit /etc/profile.

In Ubuntu WSL, how can you store permanent environment variables?

There are a few ways to go about this.

  1. First, and the normal method (as you've discovered), is by setting the environment variables in your shell startup scripts. For bash, this would be ~/.bash_profile, for zsh it would be ~/.zprofile, and for fish it would be ~/.config/fish/config.fish.

  2. But let me also suggest that you consider trying out the fish shell, as it has a great feature in its ability to set "universal variables", which are automatically propagated to all other instances of the shell, both present and in the future.

    E.g. set -Ux myvariable 42 will create a universal (and exported) variable that will persist even after you close and reopen the shell, without having to create config files.

    Fish has a number of other great features that make it my go-to shell (after 15 years on zsh).

That said, I'm not sure what your goal is with WSL, but if it's to "learn Linux", then you might be better off starting with bash or zsh since they are more "traditional" shells, with bash, of course, being the defacto starndard.

How to get systemd variables to survive a reboot?

There are different ways you can approach this problem.

1. Set the environment variable using the systemd config

You can edit the /lib/systemd/system/system.conf and add the content like below

[Manager]
DefaultEnvironment=A=B C=D

2. Set the environment variable using another systemd service

[Unit]
Description=Example systemd service init

[Service]
Type=simple
ExecStart=/bin/systemctl set-environment VAR_NAME=some_value

[Install]
WantedBy=sysinit.target

The import point is using WantedBy=sysinit.target so this is loaded early

and now we can create a simple service to test this

[Unit]
Description=Example systemd service.

[Service]
Type=simple
ExecStart=/usr/bin/env

[Install]
WantedBy=multi-user.target

and the result

root@vagrant:/lib/systemd/system# systemctl status tarun
● tarun.service - Example systemd service.
Loaded: loaded (/lib/systemd/system/tarun.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2019-06-15 11:31:17 UTC; 5s ago
Process: 1712 ExecStart=/usr/bin/env (code=exited, status=0/SUCCESS)
Main PID: 1712 (code=exited, status=0/SUCCESS)

Jun 15 11:31:17 vagrant systemd[1]: Started Example systemd service..
Jun 15 11:31:17 vagrant env[1712]: A=B
Jun 15 11:31:17 vagrant env[1712]: C=D
Jun 15 11:31:17 vagrant env[1712]: LANG=en_US.UTF-8
Jun 15 11:31:17 vagrant env[1712]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Jun 15 11:31:17 vagrant env[1712]: VAR_NAME=some_value


Related Topics



Leave a reply



Submit