{"id":17949,"date":"2013-04-05T08:37:26","date_gmt":"2013-04-05T06:37:26","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=17949"},"modified":"2013-04-05T09:44:00","modified_gmt":"2013-04-05T07:44:00","slug":"accessing-current-username-sudo-scripts-centos","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/","title":{"rendered":"Accessing current username in sudo scripts on CentOS"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>I got a bit of a puzzle at work today. \u00a0I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. \u00a0<a href=\"http:\/\/www.sudo.ws\/sudo\/sudoers.man.html\">Sudoers manual<\/a> suggest working with &#8220;<em>Defaults env_keep<\/em>&#8220;. \u00a0Looking into the <em>\/etc\/sudoers<\/em>, I noticed that <em>$USERNAME<\/em> variable was whitelisted (in line #3 below):<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nDefaults env_reset\r\nDefaults env_keep = &quot;COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS&quot;\r\nDefaults env_keep += &quot;MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE&quot;\r\nDefaults env_keep += &quot;LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES&quot;\r\nDefaults env_keep += &quot;LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE&quot;\r\nDefaults env_keep += &quot;LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY&quot;\r\n<\/pre>\n<p>So, I tried to use the <em>$USERNAME<\/em> variable in my script but it was coming up with empty results. \u00a0That made me look deeper into default Bash initialization, and I found out that <em>$USERNAME<\/em> variable setup wasn&#8217;t a part of it. \u00a0However, <em>$LOGNAME<\/em> was (in <em>\/etc\/profile<\/em>). \u00a0I think, so few people actually use it that nobody noticed or bothered about it until now. \u00a0Anyway, the solution now was obvious &#8211; simply add <em>$LOGNAME<\/em> variable to the sudo white list. \u00a0Appending this line to the above env_keep ones did the job:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\nDefaults \u00a0 \u00a0env_keep += &quot;LOGNAME&quot;\r\n<\/pre>\n<p>There. In hopes it will help future generations&#8230;<\/p>\n<p>P.S.: All that happened on a more or less default installation of CentOS 6.3, but I&#8217;m sure other Red Hat based distributions have a similar issue.<\/p>\n<p>P.P.S.: If your script is ALWAYS invoked via sudo, also have a look at <em>$SUDO_UID<\/em>, <em>$SUDO_GID<\/em>, and <em>$SUDO_USER<\/em> variables.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>I got a bit of a puzzle at work today. \u00a0I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. \u00a0Sudoers manual suggest working with &#8220;Defaults env_keep&#8220;. \u00a0Looking into the \/etc\/sudoers, I noticed that $USERNAME variable &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Accessing current username in sudo scripts on CentOS<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false,"_links_to":"","_links_to_target":""},"categories":[1,6,133,62],"tags":[3098,1960,200],"keyring_services":[],"class_list":["post-17949","post","type-post","status-publish","format-standard","hentry","category-general","category-linux","category-sysadmin","category-technology","tag-bash","tag-command-line","tag-security"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"description\" content=\"I got a bit of a puzzle at work today. I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. Sudoers manual suggest working with &quot;Defaults env_keep&quot;. Looking into the \/etc\/sudoers, I noticed that $USERNAME variable\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Leonid Mamchenkov\"\/>\n\t<meta name=\"google-site-verification\" content=\"VHvdD0_usx1_4DzKy_QCVcICVgX2EgA2ybELT-wl7kQ\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Leonid Mamchenkov - Life, universe, and everything else\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov\" \/>\n\t\t<meta property=\"og:description\" content=\"I got a bit of a puzzle at work today. I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. Sudoers manual suggest working with &quot;Defaults env_keep&quot;. Looking into the \/etc\/sudoers, I noticed that $USERNAME variable\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2013-04-05T06:37:26+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2013-04-05T07:44:00+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/MamchenkovBlog\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@mamchenkov\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov\" \/>\n\t\t<meta name=\"twitter:description\" content=\"I got a bit of a puzzle at work today. I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. Sudoers manual suggest working with &quot;Defaults env_keep&quot;. Looking into the \/etc\/sudoers, I noticed that $USERNAME variable\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@mamchenkov\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#blogposting\",\"name\":\"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov\",\"headline\":\"Accessing current username in sudo scripts on CentOS\",\"author\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"},\"datePublished\":\"2013-04-05T08:37:26+02:00\",\"dateModified\":\"2013-04-05T09:44:00+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":10,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#webpage\"},\"articleSection\":\"All, Linux, Sysadmin, Technology, bash, command line, security\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/linux\\\/#listItem\",\"name\":\"Linux\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/linux\\\/#listItem\",\"position\":3,\"name\":\"Linux\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/linux\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#listItem\",\"name\":\"Accessing current username in sudo scripts on CentOS\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#listItem\",\"position\":4,\"name\":\"Accessing current username in sudo scripts on CentOS\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/linux\\\/#listItem\",\"name\":\"Linux\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\",\"name\":\"Leonid Mamchenkov\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/\",\"name\":\"Leonid Mamchenkov\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#webpage\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/\",\"name\":\"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov\",\"description\":\"I got a bit of a puzzle at work today. I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. Sudoers manual suggest working with \\\"Defaults env_keep\\\". Looking into the \\\/etc\\\/sudoers, I noticed that $USERNAME variable\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2013\\\/04\\\/05\\\/accessing-current-username-sudo-scripts-centos\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"datePublished\":\"2013-04-05T08:37:26+02:00\",\"dateModified\":\"2013-04-05T09:44:00+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#website\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/\",\"name\":\"Blog of Leonid Mamchenkov\",\"description\":\"Life, universe, and everything else\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov","description":"I got a bit of a puzzle at work today. I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. Sudoers manual suggest working with \"Defaults env_keep\". Looking into the \/etc\/sudoers, I noticed that $USERNAME variable","canonical_url":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"google-site-verification":"VHvdD0_usx1_4DzKy_QCVcICVgX2EgA2ybELT-wl7kQ","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#blogposting","name":"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov","headline":"Accessing current username in sudo scripts on CentOS","author":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"publisher":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#person"},"image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"},"datePublished":"2013-04-05T08:37:26+02:00","dateModified":"2013-04-05T09:44:00+02:00","inLanguage":"en-US","commentCount":10,"mainEntityOfPage":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#webpage"},"isPartOf":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#webpage"},"articleSection":"All, Linux, Sysadmin, Technology, bash, command line, security"},{"@type":"BreadcrumbList","@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress#listItem","position":1,"name":"Home","item":"https:\/\/mamchenkov.net\/wordpress","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/#listItem","name":"Linux"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/#listItem","position":3,"name":"Linux","item":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#listItem","name":"Accessing current username in sudo scripts on CentOS"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#listItem","position":4,"name":"Accessing current username in sudo scripts on CentOS","previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/#listItem","name":"Linux"}}]},{"@type":"Person","@id":"https:\/\/mamchenkov.net\/wordpress\/#person","name":"Leonid Mamchenkov","image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"}},{"@type":"Person","@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author","url":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/","name":"Leonid Mamchenkov","image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"}},{"@type":"WebPage","@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#webpage","url":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/","name":"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov","description":"I got a bit of a puzzle at work today. I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. Sudoers manual suggest working with \"Defaults env_keep\". Looking into the \/etc\/sudoers, I noticed that $USERNAME variable","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#website"},"breadcrumb":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/#breadcrumblist"},"author":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"creator":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"datePublished":"2013-04-05T08:37:26+02:00","dateModified":"2013-04-05T09:44:00+02:00"},{"@type":"WebSite","@id":"https:\/\/mamchenkov.net\/wordpress\/#website","url":"https:\/\/mamchenkov.net\/wordpress\/","name":"Blog of Leonid Mamchenkov","description":"Life, universe, and everything else","inLanguage":"en-US","publisher":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#person"}}]},"og:locale":"en_US","og:site_name":"Leonid Mamchenkov - Life, universe, and everything else","og:type":"article","og:title":"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov","og:description":"I got a bit of a puzzle at work today. I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. Sudoers manual suggest working with &quot;Defaults env_keep&quot;. Looking into the \/etc\/sudoers, I noticed that $USERNAME variable","og:url":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/","og:image":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg","og:image:secure_url":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg","og:image:width":1024,"og:image:height":1024,"article:published_time":"2013-04-05T06:37:26+00:00","article:modified_time":"2013-04-05T07:44:00+00:00","article:publisher":"https:\/\/www.facebook.com\/MamchenkovBlog","twitter:card":"summary_large_image","twitter:site":"@mamchenkov","twitter:title":"Accessing current username in sudo scripts on CentOS - Leonid Mamchenkov","twitter:description":"I got a bit of a puzzle at work today. I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. Sudoers manual suggest working with &quot;Defaults env_keep&quot;. Looking into the \/etc\/sudoers, I noticed that $USERNAME variable","twitter:creator":"@mamchenkov","twitter:image":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg"},"aioseo_meta_data":{"post_id":"17949","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2023-07-19 11:39:14","updated":"2026-01-15 08:36:52","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/\" title=\"Linux\">Linux<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tAccessing current username in sudo scripts on CentOS\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/mamchenkov.net\/wordpress"},{"label":"Technology","link":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/"},{"label":"Linux","link":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/"},{"label":"Accessing current username in sudo scripts on CentOS","link":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":25998,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/04\/05\/absolute-stupidity-of-include-directive-in-etc-sudoers-and-microsoft-azure\/","url_meta":{"origin":17949,"position":0},"title":"Absolute stupidity of include directive in \/etc\/sudoers, and Microsoft Azure","author":"Leonid Mamchenkov","date":"April 5, 2016","format":false,"excerpt":"I've just spent three hours (!!!) trying to troubleshoot why sudo was misbehaving on a brand new CentOS 7 server. \u00a0I was doing the setup of two identical servers in parallel (for two different clients). \u00a0 One server worked as expected, the other one didn't. The thing I was trying\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"ASCII skull","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/04\/ascii-skull.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":8893,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/04\/26\/nucleus2wordpress\/","url_meta":{"origin":17949,"position":1},"title":"nucleus2wordpress","author":"Leonid Mamchenkov","date":"April 26, 2005","format":false,"excerpt":"Once I promised to release the script that I wrote while migrating from Nucleus CMS to WordPress. Well, here it is. But I doubt that you should start any serious celebrations, since this one is pretty messy and based very much on my setup. Let me try to explain a\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9017,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/05\/28\/how-to-switch-the-go-around-desktops-thingy-in-kde\/","url_meta":{"origin":17949,"position":2},"title":"How to switch the go-around-desktops thingy in KDE","author":"Leonid Mamchenkov","date":"May 28, 2005","format":false,"excerpt":"KDE is an excellent desktop with reasonable defaults. One of the defaults does not work for me though. It annoys me beyond limits that switching desktops wraps around. That is when I am at the left-most desktop and I press Ctrl+Left arrow, which is my key for switching one dekstop\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":22389,"url":"https:\/\/mamchenkov.net\/wordpress\/2014\/08\/15\/tek-security-groups-password-repository\/","url_meta":{"origin":17949,"position":3},"title":"Tek Security Group&#8217;s Password Repository","author":"Leonid Mamchenkov","date":"August 15, 2014","format":"link","excerpt":"Tek Security Group's Password Repository In this repository you will find helpful authentication brute forcing files. These files include known password defaults, usernames, common and specialized dictionaries, etc.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10588,"url":"https:\/\/mamchenkov.net\/wordpress\/2006\/11\/23\/things-that-i-love-about-google-calendar\/","url_meta":{"origin":17949,"position":4},"title":"Things that I love about Google Calendar","author":"Leonid Mamchenkov","date":"November 23, 2006","format":false,"excerpt":"This post started as a comment to Andrey's post about self organization and time management. I just wanted to list a few things that I particularly love about Google Calendar, and then decided to do to it here instead, for the benefit of greater public. As I mentioned some time\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9073,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/06\/14\/sending-bulk-html-emails-with-perl\/","url_meta":{"origin":17949,"position":5},"title":"Sending bulk HTML emails with perl","author":"Leonid Mamchenkov","date":"June 14, 2005","format":false,"excerpt":"First of all, I have to warn you that using HTML in emails is almost always inappropriate. Use plain text. If you have to send HTML, than send it as an attachment. For those cases, where HTML message has to be sent and there is no choice in the matter,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/17949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=17949"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/17949\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=17949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=17949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=17949"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=17949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}