Thursday, February 17, 2022

[SOLVED] centos su not autocomplete username

Issue

my question is username(which is zyxing) not autocomplete while type tab ,is here any way can solved this or just give me a suggestion for debug this .

ssh as user a
sudo -i
su z[tab]

about system and some helpful information as follow:

[root@VMS79253 ~]# cat /etc/system-release
CentOS Linux release 7.1.1503 (Core)
[root@VMS79253 ~]# rpm -qa| grep -i completion
bash-completion-2.1-6.el7.noarch
[root@VMS79253 ~]# cat /etc/passwd
...
zyxing:x:1005:1005::/home/zyxing:/bin/bash

[2018-11-21 comment]
is this question so easy? i have just tried the follow things,but not working.

  1. be sure of my account bash env is /bin/bash(most people say tab will not work because of bash env is /bin/sh)
  2. setup bash-completion and source script(also some people say must install this plugin and source script)
  3. setup bash-completion-extras(extras plugin under testing within centos7)

Solution

Problem was solved after edit follow file and re-login:
file path: /usr/share/bash-completion/completions/su
change 42 line from

COMPREPLY=( $(compgen -f -- $cur) )

to

COMPREPLY=( $(compgen -u -- $cur) )


The reason maybe cause su not auto-complete with username:

  • First,auto-complete worked while typing TAB because of the plugin named auto-complete
  • There are many difference between centos7 and centos6,because centos7 use auto-complete plugin with version 2.x but centos6 1.x.
    I found some strange logic after read the specify script worked for command su,the whole script content as follow:


    1 _su_module()
    2 {
    3         local cur prev OPTS
    4         COMPREPLY=()
    5         cur="${COMP_WORDS[COMP_CWORD]}"
    6         prev="${COMP_WORDS[COMP_CWORD-1]}"
    7         case $prev in
    8                 '-'|'-u'|'--user')
    9                         COMPREPLY=( $(compgen -u -- $cur) )
    10                         return 0
    11                         ;;
    12                 '-g'|'--group'|'-G'|'--supp-group')
    13                         COMPREPLY=( $(compgen -g -- $cur) )
    14                         return 0
    15                         ;;
    16                 '-s'|'--shell')
    17                         COMPREPLY=( $(compgen -W "$(chsh -l)" -- $cur) )
    18                         return 0
    19                         ;;
    20                 '-h'|'--help'|'-V'|'--version')
    21                         return 0
    22                         ;;
    23         esac
    24         case $cur in
    25                 -*)
    26                         OPTS="  --user
    27                                 --preserve-environment
    28                                 --group
    29                                 --supp-group
    30                                 --login
    31                                 --command
    32                                 --session-command
    33                                 --fast
    34                                 --shell
    35                                 --help
    36                                 --version"
    37                         COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
    38                         return 0
    39                         ;;
    40         esac
    41         compopt -o filenames
    42         COMPREPLY=( $(compgen -f -- $cur) )
    43         return 0
    44 }
    45 complete -F _su_module su
    46 complete -F _su_module runuser

It's easy to read this script,and we know line 42 was performed if we type su[space][TAB].
But there was return a list of current directory rather than a list of users what we want.So i just changed this line to list users and re-login,problem was solved!



Answered By - zyxing
Answer Checked By - Candace Johnson (WPSolving Volunteer)