MCPcopy Create free account
hub / github.com/archlinux/archinstall / enable_sudo

Method enable_sudo

archinstall/lib/installer.py:1912–1940  ·  view source on GitHub ↗
(self, user: User, group: bool = False)

Source from the content-addressed store, hash-verified

1910 return self.pacman.strap(packages)
1911
1912 def enable_sudo(self, user: User, group: bool = False) -> None:
1913 info(f'Enabling sudo permissions for {user.username}')
1914
1915 sudoers_dir = self.target / 'etc/sudoers.d'
1916
1917 # Creates directory if not exists
1918 if not sudoers_dir.exists():
1919 sudoers_dir.mkdir(parents=True)
1920 # Guarantees sudoer confs directory recommended perms
1921 sudoers_dir.chmod(0o440)
1922 # Appends a reference to the sudoers file, because if we are here sudoers.d did not exist yet
1923 with open(self.target / 'etc/sudoers', 'a') as sudoers:
1924 sudoers.write('@includedir /etc/sudoers.d\n')
1925
1926 # We count how many files are there already so we know which number to prefix the file with
1927 num_of_rules_already = len(os.listdir(sudoers_dir))
1928 file_num_str = f'{num_of_rules_already:02d}' # We want 00_user1, 01_user2, etc
1929
1930 # Guarantees that username str does not contain invalid characters for a linux file name:
1931 # \ / : * ? " < > |
1932 safe_username_file_name = re.sub(r'(\\|\/|:|\*|\?|"|<|>|\|)', '', user.username)
1933
1934 rule_file = sudoers_dir / f'{file_num_str}_{safe_username_file_name}'
1935
1936 with rule_file.open('a') as sudoers:
1937 sudoers.write(f'{"%" if group else ""}{user.username} ALL=(ALL) ALL\n')
1938
1939 # Guarantees sudoer conf file recommended perms
1940 rule_file.chmod(0o440)
1941
1942 def create_users(self, users: User | list[User]) -> None:
1943 if not isinstance(users, list):

Callers 1

_create_userMethod · 0.95

Calls 3

infoFunction · 0.90
existsMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected