0

I am attempting questions from a textbook to brush up on my linux skills.

The question is :

  1. Using /etc/passwd, extract the user and home directory fields for all users on your Kali machine for which the shell is set to /bin/false. Make sure you use a Bash one-liner to print the output to the screen. The output should look similar to Listing 53 below:
kali@kali:~$ YOUR COMMAND HERE...
The user mysql home directory is /nonexistent
The user Debian-snmp home directory is /var/lib/snmp
The user speech-dispatcher home directory is /var/run/speech-dispatcher
The user Debian-gdm home directory is /var/lib/gdm3
Listing 53 - Home directories for users with /bin/false shells

This is my current progress:

┌──(root💀kali)-[/home/kali]
└─# cat /etc/passwd | cut -f 1 -d ":" | grep -v '/bin/false' | awk '{print "The user " $0;}' 

Output:

The user root
The user daemon
The user games
The user mail

I can't find a way to pipe/chain commands such that i can add "home directory" at the back to format my output. Can anyone help?

1 Answer 1

1

You're discarding all the other fields of the file when you do cut -f 1 -d :.

There's no need to use cut, you can tell awk to use : as the field delimiter with the -F option.

You also don't need grep, since awk can do pattern matching. You also have that check backwards -- you're removing /bin/false instead of matching it.

awk -F: '$NF == "/bin/false" { printf("The user %s home directory is %s\n", $1, $6)}' /etc/passwd

Sample output:

The user bin home directory is /bin
The user daemon home directory is /sbin
The user adm home directory is /var/adm
The user lp home directory is /var/spool/lpd
The user news home directory is /var/spool/news
The user uucp home directory is /var/spool/uucp
The user portage home directory is /var/lib/portage/home
The user nobody home directory is /var/empty
The user systemd-journal-remote home directory is /dev/null
The user systemd-coredump home directory is /dev/null
The user systemd-network home directory is /dev/null
The user systemd-resolve home directory is /dev/null
The user systemd-timesync home directory is /dev/null
The user messagebus home directory is /dev/null
The user sshd home directory is /var/empty
The user polkitd home directory is /var/lib/polkit-1
The user u_app1 home directory is /srv/app/app1
The user nagios home directory is /dev/null
The user icinga home directory is /var/lib/icinga2
The user systemd-oom home directory is /dev/null
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.